[root@master ~]# vi /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin #myslq服务器id server-id=1 #开启binlog log-bin=mysql-bin #开启半同步复制 rpl_semi_sync_master_enabled=1 #跳过名称反向解析 skip_name_resolve=1 # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
保存退出,先不要重启mysql。
2.2 master节点安装半同步插件
192.168.1.161节点上执行:
1 2 3 4 5 6 7 8 9 10 11 12 13
[root@master ~]# mysql -u root -p Enter password: 此处输入mysql密码 #执行安装插件 mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; #退出 mysql> quit #重启mysql [root@master ~]# systemctl restart mysqld [root@master ~]# mysql -u root -p Enter password: 此处输入mysql密码 #查看主节点状态 mysql> show master status \G;
[root@slave1 ~]# vi /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin #myslq服务器id server-id=2 #开启binlog log-bin=mysql-bin # relay_log=mysql-relay-bin #配置为只读模式,mha故障转移主节点到此台机器会自动关闭只读模式 read_only=1 #开启半同步复制 rpl_semi_sync_slave_enabled=1 #跳过名称反向解析 skip_name_resolve=1 #不清空中继日志 relay_log_purge=0 # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M
保存退出,暂时不要重启mysql
2.4 slave节点安装半同步复制插件
192.168.1.162和192.168.1.163节点上执行:
1 2 3 4 5 6 7 8 9 10 11 12
[root@master ~]# mysql -u root -p Enter password: 此处输入mysql密码 #执行安装插件 mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; #退出 mysql> quit #重启mysql [root@master ~]# systemctl restart mysqld [root@master ~]# mysql -u root -p Enter password: 此处输入mysql密码 #查看主节点状态 mysql> show master status \G;
2.5 开启主从同步
2.5.1 master节点操作
192.168.1.161节点上执行:
1 2 3 4 5 6 7 8 9
[root@master ~]# mysql -u root -p Enter password: 此处输入mysql密码 #启同步账户和授权 mysql> grant REPLICATION SLAVE on *.* to 'root'@'%' identified by 'Mysql@123'; mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Mysql@123' WITH GRANT OPTION; #查看主节点状态 mysql> show master status \G;
### 检查mha的ssh免密登录状态 [root@mha ~]# masterha_check_ssh --conf=/etc/mha/cluster01.conf Sun Oct 11 10:07:16 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Sun Oct 11 10:07:16 2020 - [info] Reading application default configuration from /etc/mha/cluster01.conf.. Sun Oct 11 10:07:16 2020 - [info] Reading server configuration from /etc/mha/cluster01.conf.. Sun Oct 11 10:07:16 2020 - [info] Starting SSH connection tests.. Sun Oct 11 10:07:18 2020 - [debug] Sun Oct 11 10:07:16 2020 - [debug] Connecting via SSH from root@192.168.1.162(192.168.1.162:22) to root@192.168.1.161(192.168.1.161:22).. Sun Oct 11 10:07:17 2020 - [debug] ok. Sun Oct 11 10:07:17 2020 - [debug] Connecting via SSH from root@192.168.1.162(192.168.1.162:22) to root@192.168.1.163(192.168.1.163:22).. Sun Oct 11 10:07:18 2020 - [debug] ok. Sun Oct 11 10:07:18 2020 - [debug] Sun Oct 11 10:07:16 2020 - [debug] Connecting via SSH from root@192.168.1.161(192.168.1.161:22) to root@192.168.1.162(192.168.1.162:22).. Sun Oct 11 10:07:16 2020 - [debug] ok. Sun Oct 11 10:07:16 2020 - [debug] Connecting via SSH from root@192.168.1.161(192.168.1.161:22) to root@192.168.1.163(192.168.1.163:22).. Sun Oct 11 10:07:17 2020 - [debug] ok. Sun Oct 11 10:07:19 2020 - [debug] Sun Oct 11 10:07:17 2020 - [debug] Connecting via SSH from root@192.168.1.163(192.168.1.163:22) to root@192.168.1.161(192.168.1.161:22).. Sun Oct 11 10:07:17 2020 - [debug] ok. Sun Oct 11 10:07:17 2020 - [debug] Connecting via SSH from root@192.168.1.163(192.168.1.163:22) to root@192.168.1.162(192.168.1.162:22).. Sun Oct 11 10:07:18 2020 - [debug] ok. Sun Oct 11 10:07:19 2020 - [info] All SSH connection tests passed successfully.
[root@mha ~]# masterha_check_repl --conf=/etc/mha/cluster01.conf Sun Oct 11 10:08:50 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Sun Oct 11 10:08:50 2020 - [info] Reading application default configuration from /etc/mha/cluster01.conf.. Sun Oct 11 10:08:50 2020 - [info] Reading server configuration from /etc/mha/cluster01.conf.. Sun Oct 11 10:08:50 2020 - [info] MHA::MasterMonitor version 0.58. Sun Oct 11 10:08:51 2020 - [info] Multi-master configuration is detected. Current primary(writable) master is 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:08:51 2020 - [info] Master configurations are as below: Master 192.168.1.162(192.168.1.162:3306), replicating from 192.168.1.161(192.168.1.161:3306), read-only Master 192.168.1.161(192.168.1.161:3306), replicating from 192.168.1.162(192.168.1.162:3306)
Sun Oct 11 10:08:51 2020 - [info] GTID failover mode = 0 Sun Oct 11 10:08:51 2020 - [info] Dead Servers: Sun Oct 11 10:08:51 2020 - [info] Alive Servers: Sun Oct 11 10:08:51 2020 - [info] 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:08:51 2020 - [info] 192.168.1.162(192.168.1.162:3306) Sun Oct 11 10:08:51 2020 - [info] 192.168.1.163(192.168.1.163:3306) Sun Oct 11 10:08:51 2020 - [info] Alive Slaves: Sun Oct 11 10:08:51 2020 - [info] 192.168.1.162(192.168.1.162:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:08:51 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:08:51 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:08:51 2020 - [info] 192.168.1.163(192.168.1.163:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:08:51 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:08:51 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:08:51 2020 - [info] Current Alive Master: 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:08:51 2020 - [info] Checking slave configurations.. Sun Oct 11 10:08:51 2020 - [info] Checking replication filtering settings.. Sun Oct 11 10:08:51 2020 - [info] binlog_do_db= , binlog_ignore_db= Sun Oct 11 10:08:51 2020 - [info] Replication filtering check ok. Sun Oct 11 10:08:51 2020 - [info] GTID (with auto-pos) is not supported Sun Oct 11 10:08:51 2020 - [info] Starting SSH connection tests.. Sun Oct 11 10:08:55 2020 - [info] All SSH connection tests passed successfully. Sun Oct 11 10:08:55 2020 - [info] Checking MHA Node version.. Sun Oct 11 10:08:55 2020 - [info] Version check ok. Sun Oct 11 10:08:55 2020 - [info] Checking SSH publickey authentication settings on the current master.. Sun Oct 11 10:08:56 2020 - [info] HealthCheck: SSH to 192.168.1.161 is reachable. Sun Oct 11 10:08:56 2020 - [info] Master MHA Node version is 0.58. Sun Oct 11 10:08:56 2020 - [info] Checking recovery script configurations on 192.168.1.161(192.168.1.161:3306).. Sun Oct 11 10:08:56 2020 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/data/mastermha/cluster01//save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000011 Sun Oct 11 10:08:56 2020 - [info] Connecting to root@192.168.1.161(192.168.1.161:22).. Creating /data/mastermha/cluster01 if not exists.. ok. Checking output directory is accessible or not.. ok. Binlog found at /var/lib/mysql, up to mysql-bin.000011 Sun Oct 11 10:08:57 2020 - [info] Binlog setting check done. Sun Oct 11 10:08:57 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers.. Sun Oct 11 10:08:57 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.1.162 --slave_ip=192.168.1.162 --slave_port=3306 --workdir=/data/mastermha/cluster01/ --target_version=5.7.28-log --manager_version=0.58 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx Sun Oct 11 10:08:57 2020 - [info] Connecting to root@192.168.1.162(192.168.1.162:22).. Checking slave recovery environment settings.. Opening /var/lib/mysql/relay-log.info ... ok. Relay log found at /var/lib/mysql, up to mysql-relay-bin.000002 Temporary relay log file is /var/lib/mysql/mysql-relay-bin.000002 Checking if super_read_only is defined and turned on.. not present or turned off, ignoring. Testing mysql connection and privileges.. mysql: [Warning] Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Sun Oct 11 10:08:57 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.1.163 --slave_ip=192.168.1.163 --slave_port=3306 --workdir=/data/mastermha/cluster01/ --target_version=5.7.28-log --manager_version=0.58 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx Sun Oct 11 10:08:57 2020 - [info] Connecting to root@192.168.1.163(192.168.1.163:22).. Checking slave recovery environment settings.. Opening /var/lib/mysql/relay-log.info ... ok. Relay log found at /var/lib/mysql, up to mysql-relay-bin.000002 Temporary relay log file is /var/lib/mysql/mysql-relay-bin.000002 Checking if super_read_only is defined and turned on.. not present or turned off, ignoring. Testing mysql connection and privileges.. mysql: [Warning] Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Sun Oct 11 10:08:58 2020 - [info] Slaves settings check done. Sun Oct 11 10:08:58 2020 - [info] 192.168.1.161(192.168.1.161:3306) (current master) +--192.168.1.162(192.168.1.162:3306) +--192.168.1.163(192.168.1.163:3306)
Sun Oct 11 10:08:58 2020 - [info] Checking replication health on 192.168.1.162.. Sun Oct 11 10:08:58 2020 - [info] ok. Sun Oct 11 10:08:58 2020 - [info] Checking replication health on 192.168.1.163.. Sun Oct 11 10:08:58 2020 - [info] ok. Sun Oct 11 10:08:58 2020 - [warning] master_ip_failover_script is not defined. Sun Oct 11 10:08:58 2020 - [warning] shutdown_script is not defined. Sun Oct 11 10:08:58 2020 - [info] Got exit code 0 (Not master dead).
Sun Oct 11 10:10:33 2020 - [warning] master_ip_failover_script is not defined. Sun Oct 11 10:10:33 2020 - [warning] shutdown_script is not defined. Sun Oct 11 10:10:33 2020 - [info] Set master ping interval 1 seconds. Sun Oct 11 10:10:33 2020 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes. Sun Oct 11 10:10:33 2020 - [info] Starting ping health check on 192.168.1.161(192.168.1.161:3306).. Sun Oct 11 10:10:33 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond.. Sun Oct 11 10:16:46 2020 - [warning] Got error on MySQL select ping: 2013 (Lost connection to MySQL server during query) Sun Oct 11 10:16:46 2020 - [info] Executing SSH check script: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/data/mastermha/cluster01//save_binary_logs_test --manager_version=0.58 --binlog_prefix=mysql-bin Sun Oct 11 10:16:46 2020 - [warning] HealthCheck: SSH to 192.168.1.161 is NOT reachable. Sun Oct 11 10:16:47 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.1.161' (111)) Sun Oct 11 10:16:47 2020 - [warning] Connection failed 2 time(s).. Sun Oct 11 10:16:48 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.1.161' (111)) Sun Oct 11 10:16:48 2020 - [warning] Connection failed 3 time(s).. Sun Oct 11 10:16:49 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.1.161' (111)) Sun Oct 11 10:16:49 2020 - [warning] Connection failed 4 time(s).. Sun Oct 11 10:16:49 2020 - [warning] Master is not reachable from health checker! Sun Oct 11 10:16:49 2020 - [warning] Master 192.168.1.161(192.168.1.161:3306) is not reachable! Sun Oct 11 10:16:49 2020 - [warning] SSH is NOT reachable. Sun Oct 11 10:16:49 2020 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/mha/cluster01.conf again, and trying to connect to all servers to check server status.. Sun Oct 11 10:16:49 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Sun Oct 11 10:16:49 2020 - [info] Reading application default configuration from /etc/mha/cluster01.conf.. Sun Oct 11 10:16:49 2020 - [info] Reading server configuration from /etc/mha/cluster01.conf.. Sun Oct 11 10:16:50 2020 - [info] GTID failover mode = 0 Sun Oct 11 10:16:50 2020 - [info] Dead Servers: Sun Oct 11 10:16:50 2020 - [info] 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:50 2020 - [info] Alive Servers: Sun Oct 11 10:16:50 2020 - [info] 192.168.1.162(192.168.1.162:3306) Sun Oct 11 10:16:50 2020 - [info] 192.168.1.163(192.168.1.163:3306) Sun Oct 11 10:16:50 2020 - [info] Alive Slaves: Sun Oct 11 10:16:50 2020 - [info] 192.168.1.162(192.168.1.162:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:50 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:50 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:50 2020 - [info] 192.168.1.163(192.168.1.163:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:50 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:50 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:50 2020 - [info] Checking slave configurations.. Sun Oct 11 10:16:50 2020 - [info] Checking replication filtering settings.. Sun Oct 11 10:16:50 2020 - [info] Replication filtering check ok. Sun Oct 11 10:16:50 2020 - [info] Master is down! Sun Oct 11 10:16:50 2020 - [info] Terminating monitoring script. Sun Oct 11 10:16:50 2020 - [info] Got exit code 20 (Master dead). Sun Oct 11 10:16:50 2020 - [info] MHA::MasterFailover version 0.58. Sun Oct 11 10:16:50 2020 - [info] Starting master failover. Sun Oct 11 10:16:50 2020 - [info] Sun Oct 11 10:16:50 2020 - [info] * Phase 1: Configuration Check Phase.. Sun Oct 11 10:16:50 2020 - [info] Sun Oct 11 10:16:51 2020 - [info] GTID failover mode = 0 Sun Oct 11 10:16:51 2020 - [info] Dead Servers: Sun Oct 11 10:16:51 2020 - [info] 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:51 2020 - [info] Checking master reachability via MySQL(double check)... Sun Oct 11 10:16:51 2020 - [info] ok. Sun Oct 11 10:16:51 2020 - [info] Alive Servers: Sun Oct 11 10:16:51 2020 - [info] 192.168.1.162(192.168.1.162:3306) Sun Oct 11 10:16:51 2020 - [info] 192.168.1.163(192.168.1.163:3306) Sun Oct 11 10:16:51 2020 - [info] Alive Slaves: Sun Oct 11 10:16:51 2020 - [info] 192.168.1.162(192.168.1.162:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:51 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:51 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:51 2020 - [info] 192.168.1.163(192.168.1.163:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:51 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:51 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:51 2020 - [info] Starting Non-GTID based failover. Sun Oct 11 10:16:51 2020 - [info] Sun Oct 11 10:16:51 2020 - [info] ** Phase 1: Configuration Check Phase completed. Sun Oct 11 10:16:51 2020 - [info] Sun Oct 11 10:16:51 2020 - [info] * Phase 2: Dead Master Shutdown Phase.. Sun Oct 11 10:16:51 2020 - [info] Sun Oct 11 10:16:51 2020 - [info] Forcing shutdown so that applications never connect to the current master.. Sun Oct 11 10:16:51 2020 - [warning] master_ip_failover_script is not set. Skipping invalidating dead master IP address. Sun Oct 11 10:16:51 2020 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master. Sun Oct 11 10:16:52 2020 - [info] * Phase 2: Dead Master Shutdown Phase completed. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 3: Master Recovery Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 3.1: Getting Latest Slaves Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] The latest binary log file/position on all slaves is mysql-bin.000011:1403 Sun Oct 11 10:16:52 2020 - [info] Latest slaves (Slaves that received relay log files to the latest): Sun Oct 11 10:16:52 2020 - [info] 192.168.1.162(192.168.1.162:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:52 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:52 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:52 2020 - [info] 192.168.1.163(192.168.1.163:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:52 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:52 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:52 2020 - [info] The oldest binary log file/position on all slaves is mysql-bin.000011:1403 Sun Oct 11 10:16:52 2020 - [info] Oldest slaves: Sun Oct 11 10:16:52 2020 - [info] 192.168.1.162(192.168.1.162:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:52 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:52 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:52 2020 - [info] 192.168.1.163(192.168.1.163:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:52 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:52 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [warning] Dead Master is not SSH reachable. Could not save it's binlogs. Transactions that were not sent to the latest slave (Read_Master_Log_Pos to the tail of the dead master's binlog) were lost. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 3.3: Determining New Master Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] Finding the latest slave that has all relay logs for recovering other slaves.. Sun Oct 11 10:16:52 2020 - [info] All slaves received relay logs to the same position. No need to resync each other. Sun Oct 11 10:16:52 2020 - [info] Searching new master from slaves.. Sun Oct 11 10:16:52 2020 - [info] Candidate masters from the configuration file: Sun Oct 11 10:16:52 2020 - [info] 192.168.1.162(192.168.1.162:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:52 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:52 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:52 2020 - [info] 192.168.1.163(192.168.1.163:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled Sun Oct 11 10:16:52 2020 - [info] Replicating from 192.168.1.161(192.168.1.161:3306) Sun Oct 11 10:16:52 2020 - [info] Primary candidate for the new Master (candidate_master is set) Sun Oct 11 10:16:52 2020 - [info] Non-candidate masters: Sun Oct 11 10:16:52 2020 - [info] Searching from candidate_master slaves which have received the latest relay log events.. Sun Oct 11 10:16:52 2020 - [info] New master is 192.168.1.162(192.168.1.162:3306) Sun Oct 11 10:16:52 2020 - [info] Starting master failover.. Sun Oct 11 10:16:52 2020 - [info] From: 192.168.1.161(192.168.1.161:3306) (current master) +--192.168.1.162(192.168.1.162:3306) +--192.168.1.163(192.168.1.163:3306)
To: 192.168.1.162(192.168.1.162:3306) (new master) +--192.168.1.163(192.168.1.163:3306) Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 3.4: New Master Diff Log Generation Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] This server has all relay logs. No need to generate diff files from the latest slave. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 3.5: Master Log Apply Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed. Sun Oct 11 10:16:52 2020 - [info] Starting recovery on 192.168.1.162(192.168.1.162:3306).. Sun Oct 11 10:16:52 2020 - [info] This server has all relay logs. Waiting all logs to be applied.. Sun Oct 11 10:16:52 2020 - [info] done. Sun Oct 11 10:16:52 2020 - [info] All relay logs were successfully applied. Sun Oct 11 10:16:52 2020 - [info] Getting new master's binlog name and position.. Sun Oct 11 10:16:52 2020 - [info] mysql-bin.000011:297 Sun Oct 11 10:16:52 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.162', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=297, MASTER_USER='root', MASTER_PASSWORD='xxx'; Sun Oct 11 10:16:52 2020 - [warning] master_ip_failover_script is not set. Skipping taking over new master IP address. Sun Oct 11 10:16:52 2020 - [info] Setting read_only=0 on 192.168.1.162(192.168.1.162:3306).. Sun Oct 11 10:16:52 2020 - [info] ok. Sun Oct 11 10:16:52 2020 - [info] ** Finished master recovery successfully. Sun Oct 11 10:16:52 2020 - [info] * Phase 3: Master Recovery Phase completed. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 4: Slaves Recovery Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase.. Sun Oct 11 10:16:52 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] -- Slave diff file generation on host 192.168.1.163(192.168.1.163:3306) started, pid: 123426. Check tmp log /data/mastermha/cluster01//192.168.1.163_3306_20201011101650.log if it takes time.. Sun Oct 11 10:16:53 2020 - [info] Sun Oct 11 10:16:53 2020 - [info] Log messages from 192.168.1.163 ... Sun Oct 11 10:16:53 2020 - [info] Sun Oct 11 10:16:52 2020 - [info] This server has all relay logs. No need to generate diff files from the latest slave. Sun Oct 11 10:16:53 2020 - [info] End of log messages from 192.168.1.163. Sun Oct 11 10:16:53 2020 - [info] -- 192.168.1.163(192.168.1.163:3306) has the latest relay log events. Sun Oct 11 10:16:53 2020 - [info] Generating relay diff files from the latest slave succeeded. Sun Oct 11 10:16:53 2020 - [info] Sun Oct 11 10:16:53 2020 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase.. Sun Oct 11 10:16:53 2020 - [info] Sun Oct 11 10:16:53 2020 - [info] -- Slave recovery on host 192.168.1.163(192.168.1.163:3306) started, pid: 123458. Check tmp log /data/mastermha/cluster01//192.168.1.163_3306_20201011101650.log if it takes time.. Sun Oct 11 10:16:54 2020 - [info] Sun Oct 11 10:16:54 2020 - [info] Log messages from 192.168.1.163 ... Sun Oct 11 10:16:54 2020 - [info] Sun Oct 11 10:16:53 2020 - [info] Starting recovery on 192.168.1.163(192.168.1.163:3306).. Sun Oct 11 10:16:53 2020 - [info] This server has all relay logs. Waiting all logs to be applied.. Sun Oct 11 10:16:53 2020 - [info] done. Sun Oct 11 10:16:53 2020 - [info] All relay logs were successfully applied. Sun Oct 11 10:16:53 2020 - [info] Resetting slave 192.168.1.163(192.168.1.163:3306) and starting replication from the new master 192.168.1.162(192.168.1.162:3306).. Sun Oct 11 10:16:53 2020 - [info] Executed CHANGE MASTER. Sun Oct 11 10:16:54 2020 - [info] Slave started. Sun Oct 11 10:16:54 2020 - [info] End of log messages from 192.168.1.163. Sun Oct 11 10:16:54 2020 - [info] -- Slave recovery on host 192.168.1.163(192.168.1.163:3306) succeeded. Sun Oct 11 10:16:54 2020 - [info] All new slave servers recovered successfully. Sun Oct 11 10:16:54 2020 - [info] Sun Oct 11 10:16:54 2020 - [info] * Phase 5: New master cleanup phase.. Sun Oct 11 10:16:54 2020 - [info] Sun Oct 11 10:16:54 2020 - [info] Resetting slave info on the new master.. Sun Oct 11 10:16:54 2020 - [info] 192.168.1.162: Resetting slave info succeeded. Sun Oct 11 10:16:54 2020 - [info] Master failover to 192.168.1.162(192.168.1.162:3306) completed successfully. Sun Oct 11 10:16:54 2020 - [info]
----- Failover Report -----
cluster01: MySQL Master failover 192.168.1.161(192.168.1.161:3306) to 192.168.1.162(192.168.1.162:3306) succeeded
Master 192.168.1.161(192.168.1.161:3306) is down!
Check MHA Manager logs at mha:/data/mastermha/cluster01/manager.log for details.
Started automated(non-interactive) failover. The latest slave 192.168.1.162(192.168.1.162:3306) has all relay logs for recovery. Selected 192.168.1.162(192.168.1.162:3306) as a new master. 192.168.1.162(192.168.1.162:3306): OK: Applying all logs succeeded. 192.168.1.163(192.168.1.163:3306): This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. 192.168.1.163(192.168.1.163:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.1.162(192.168.1.162:3306) 192.168.1.162(192.168.1.162:3306): Resetting slave info succeeded. Master failover to 192.168.1.162(192.168.1.162:3306) completed successfully.
可以看到162节点切成功切换为新的master。接下来说明下161如何恢复为新的slave:
在192.168.1.164MHA manager上查看恢复语句。
1 2 3 4
[root@mha cluster01]# cd /data/mastermha/cluster01 [root@mha cluster01]# grep "CHANGE MASTER TO MASTER" manager.log | tail -1 Sun Oct 11 10:16:52 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.162', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=297, MASTER_USER='root', MASTER_PASSWORD='xxx'; [root@mha cluster01]#
记录下 CHANGE MASTER TO MASTER_HOST=’192.168.1.162’, MASTER_PORT=3306, MASTER_LOG_FILE=’mysql-bin.000011’, MASTER_LOG_POS=297, MASTER_USER=’root’, MASTER_PASSWORD=’xxx’;
##因为161之前为master,需要先将Mysql设置为只读模式 [root@master ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.28-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set global read_only=1; Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.162', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=297, MASTER_USER='root', MASTER_PASSWORD='Mysql@123'; ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first. mysql> stop slave; Query OK, 0 rows affected (0.00 sec)