当前位置:   article > 正文

云上 docker 安装 MySQL8+keepalived双主热备高可用_云服务器mysql主从热备

云服务器mysql主从热备

环境准备

两台云服务器:IP1:10.0.1.143,IP2: 10.0.1.237

申请虚拟IP:10.0.1.204

流程说明

VIP地址为10.0.1.204,如果主机10.0.1.143挂了,会自动切换到备机10.0.1.237上,由于是双主,数据可以进行同步恢复

部署流程大致为

  1. 在10.0.1.143服务器上部署docker、mysql一主一丛
  2. 在10.0.1.237服务器上部署docker、mysql一主一丛
  3. 10.0.1.143的Master1数据库和10.0.1.237的数据库互为主从
  4. 在两台服务器上安装keeplived形成高可用

配置第一台一主一丛

创建主配置文件和目录

  1. mkdir -p /data/mysql/master/config
  2. mkdir -p /data/mysql/master/data
  3. mkdir -p /data/mysql/master/logs
  4. touch /data/mysql/master/config/my.cnf
  5. chmod +x /data/mysql/master/config/my.cnf

 创建从配置文件和目录

  1. mkdir -p /data/mysql/slave/config
  2. mkdir -p /data/mysql/slave/data
  3. mkdir -p /data/mysql/slave/logs
  4. touch /data/mysql/slave/config/my.cnf
  5. chmod +x /data/mysql/slave/config/my.cnf

主my.cnf配置文件 

  1. [mysqld]
  2. server-id=101
  3. max_connections=500
  4. max_allowed_packet=32M
  5. wait_timeout=600
  6. slow_query_log=ON
  7. slow_query_log_file=/var/lib/mysql/slow.log
  8. long_query_time=1
  9. log_queries_not_using_indexes=ON
  10. log_throttle_queries_not_using_indexes=10
  11. max_execution_time=600
  12. log-bin=mysql-bin
  13. binlog_format=ROW
  14. max_binlog_size=500M
  15. max_binlog_cache_size=1G
  16. max_binlog_stmt_cache_size=1G
  17. innodb_flush_method=O_DIRECT
  18. innodb_log_buffer_size=1024M
  19. innodb_buffer_pool_size=512M
  20. innodb_print_all_deadlocks=1
  21. innodb_rollback_on_timeout=ON
  22. innodb_deadlock_detect=ON
  23. max_relay_log_size=500M
  24. relay_log_purge=ON
  25. relay_log_recovery=ON
  26. log_replica_updates=ON
  27. host_cache_size=0
  28. skip-name-resolve
  29. datadir=/var/lib/mysql
  30. socket=/var/run/mysqld/mysqld.sock
  31. secure-file-priv=/var/lib/mysql-files
  32. user=mysql
  33. pid-file=/var/run/mysqld/mysqld.pid
  34. [client]
  35. socket=/var/run/mysqld/mysqld.sock

 从 my.cnf配置文件 

  1. [mysqld]
  2. server-id=102
  3. max_connections=500
  4. max_allowed_packet=32M
  5. wait_timeout=600
  6. slow_query_log=ON
  7. slow_query_log_file=/var/lib/mysql/slow.log
  8. long_query_time=1
  9. log_queries_not_using_indexes=ON
  10. log_throttle_queries_not_using_indexes=10
  11. max_execution_time=600
  12. log-bin=mysql-bin
  13. binlog_format=ROW
  14. max_binlog_size=500M
  15. max_binlog_cache_size=1G
  16. max_binlog_stmt_cache_size=1G
  17. innodb_flush_method=O_DIRECT
  18. innodb_log_buffer_size=1024M
  19. innodb_buffer_pool_size=512M
  20. innodb_print_all_deadlocks=1
  21. innodb_rollback_on_timeout=ON
  22. innodb_deadlock_detect=ON
  23. max_relay_log_size=500M
  24. relay_log_purge=ON
  25. relay_log_recovery=ON
  26. log_replica_updates=ON
  27. host_cache_size=0
  28. skip-name-resolve
  29. datadir=/var/lib/mysql
  30. socket=/var/run/mysqld/mysqld.sock
  31. secure-file-priv=/var/lib/mysql-files
  32. user=mysql
  33. pid-file=/var/run/mysqld/mysqld.pid
  34. [client]
  35. socket=/var/run/mysqld/mysqld.sock

使用docker-compose.yml安装Mysql8,执行如下命令 

  1. version: '3'
  2. services:
  3. mysql_master:
  4. image: mysql:8.0.30
  5. restart: always
  6. container_name: mysql_master
  7. volumes:
  8. - /data/mysql/master/data:/var/lib/mysql
  9. - /data/mysql/master/config/my.cnf:/etc/mysql/conf.d/my.cnf
  10. - /data/mysql/master/logs:/var/log/mysql
  11. # 数据库还原目录 可将需要还原的sql文件放在这里
  12. - /data/mysql/init/mysql:/docker-entrypoint-initdb.d
  13. environment:
  14. - "MYSQL_ROOT_PASSWORD=yx@123456"
  15. - "MYSQL_DATABASE=test"
  16. - "TZ=Asia/Shanghai"
  17. ports:
  18. - 3306:3306
  19. privileged: true
  20. mysql_slave:
  21. image: mysql:8.0.30
  22. restart: always
  23. container_name: mysql_slave
  24. volumes:
  25. - /data/mysql/slave/data:/var/lib/mysql
  26. - /data/mysql/slave/config/my.cnf:/etc/mysql/conf.d/my.cnf
  27. - /data/mysql/slave/logs:/var/log/mysql
  28. # 数据库还原目录 可将需要还原的sql文件放在这里
  29. - /data/mysql/init/mysql:/docker-entrypoint-initdb.d
  30. environment:
  31. - "MYSQL_ROOT_PASSWORD=yx@123456"
  32. - "MYSQL_DATABASE=test"
  33. - "TZ=Asia/Shanghai"
  34. ports:
  35. - 4306:3306
  36. privileged: true

在主机上安装mysql客户端软件

  1. wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-client-8.0.32-1.el7.x86_64.rpm
  2. wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm
  3. wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-libs-8.0.32-1.el7.x86_64.rpm
  4. wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-common-8.0.32-1.el7.x86_64.rpm

检查 rpm -qa | grep mariadb 并卸载

  1. [root@wy9jygsgl0t9rcw5-0002 client]# rpm -qa | grep mariadb
  2. mariadb-libs-5.5.68-1.el7.x86_64
  3. [root@wy9jygsgl0t9rcw5-0002 client]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
  1. rpm -ivh mysql-community-common-8.0.32-1.el7.x86_64.rpm
  2. rpm -ivh mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm
  3. rpm -ivh mysql-community-libs-8.0.32-1.el7.x86_64.rpm
  4. rpm -ivh mysql-community-client-8.0.32-1.el7.x86_64.rpm

 登陆主数据库

mysql -h10.0.1.143 -P3306 -uroot -p

  创建账号 

  1. CREATE USER r_yxwg identified with mysql_native_password by 'r_yx@123456';
  2. GRANT REPLICATION SLAVE ON *.* to 'r_yxwg'@'%';
  3. FLUSH PRIVILEGES;

查询主库文件和标识 

SHOW MASTER STATUS;

 登陆从数据库

mysql -h10.0.1.143 -P4306 -uroot -p

文件和标识为主库的 

  1. change master to master_host='10.0.1.143',master_port=3306,master_user='r_yxwg',master_password='r_yx@123456',master_log_file='mysql-bin.000003',master_log_pos=823;
  2. start slave;
  3. show slave status\G;

 

配置第二台一主一丛 

创建流程第一台一样(同上)

主主配置

登陆A服务器mysql -h10.0.1.143 -P3306 -uroot -p

  1. CREATE USER repl identified with mysql_native_password by 'repl@123456';
  2. GRANT REPLICATION SLAVE ON *.* to 'repl'@'%';
  3. flush privileges;
  4. -- 添加写锁,防止设置过程中,写入数据,导致binlog和pos发生变化
  5. flush tables with read lock;
  6. -- 查看当前MySQL的master_log_file和master_log_position的值
  7. show master status ;
  8. unlock tables ;

登陆B服务器执行如下操作 

  1. stop slave ;
  2. change master to master_host='10.0.1.237',master_port=3306,master_user='repl',master_password='repl@123456',master_log_file='mysql-bin.000003',master_log_pos=1485;
  3. start slave ;
  4. show slave status\G;

 

登陆B服务器mysql -h10.0.1.237 -P3306 -uroot -p

  1. CREATE USER repl identified with mysql_native_password by 'repl@123456';
  2. GRANT REPLICATION SLAVE ON *.* to 'repl'@'%';
  3. flush privileges;
  4. -- 添加写锁,防止设置过程中,写入数据,导致binlog和pos发生变化
  5. flush tables with read lock;
  6. -- 查看当前MySQL的master_log_file和master_log_position的值
  7. show master status ;
  8. unlock tables ;

 登陆A服务器执行如下操作

  1. stop slave ;
  2. change master to master_host='10.0.1.143',master_port=3306,master_user='repl',master_password='repl_yx21SA@WSX',master_log_file='mysql-bin.000004',master_log_pos=819;
  3. start slave ;
  4. show slave status\G;

 安装keepalived

两台服务器安装keepalived

yum -y install keepalived

修改主keepalived配置文件(/etc/keepalived/keepalived.conf)

  1. global_defs {
  2. router_id mysql_master
  3. script_user root
  4. enable_script_security
  5. }
  6. vrrp_script check_mysql {
  7. script "/etc/keepalived/chk_mysql.sh"
  8. interval 5
  9. weight -10
  10. fall 2
  11. rise 1
  12. }
  13. vrrp_instance mysql {
  14. state MASTER
  15. interface enp0s8
  16. virtual_router_id 51
  17. priority 150
  18. advert_int 1
  19. authentication {
  20. auth_type PASS
  21. auth_pass 1111
  22. }
  23. virtual_ipaddress {
  24. 10.0.1.204
  25. }
  26. track_script {
  27. chk_mysql
  28. }
  29. }

上面修改VIP、网络名称enp0s8、检查脚本chk_mysql.sh

  1. #!/bin/bash
  2. counter=$(netstat -na | grep "LISTEN" | grep "3306" | wc -l)
  3. if [ ${counter} -eq 0 ]; then
  4. killall keepalived
  5. fi

修改备keepalived配置文件(/etc/keepalived/keepalived.conf)

  1. global_defs {
  2. router_id mysql_master
  3. script_user root
  4. enable_script_security
  5. }
  6. vrrp_script check_mysql {
  7. script "/etc/keepalived/chk_mysql.sh"
  8. interval 5
  9. weight -10
  10. fall 2
  11. rise 1
  12. }
  13. vrrp_instance mysql {
  14. state BACKUP
  15. interface enp0s8
  16. virtual_router_id 51
  17. priority 130
  18. advert_int 1
  19. authentication {
  20. auth_type PASS
  21. auth_pass 1111
  22. }
  23. virtual_ipaddress {
  24. 10.0.1.204
  25. }
  26. track_script {
  27. chk_mysql
  28. }
  29. }

上面修改VIP、网络名称enp0s8、检查脚本chk_mysql.sh

  1. #!/bin/bash
  2. counter=$(netstat -na | grep "LISTEN" | grep "3306" | wc -l)
  3. if [ ${counter} -eq 0 ]; then
  4. killall keepalived
  5. fi

两边启动并加入开机自启动

  1. [root@mysql1 ~]# systemctl start keepalived
  2. [root@mysql1 ~]# systemctl enable keepalived

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/587383
推荐阅读
相关标签
  

闽ICP备14008679号