# 创建密钥对,生成私钥和公钥文件,创建密钥时可以不设置密码,一路回车就好 [root@ip-10-250-0-91 .ssh]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file inwhich to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:C8JdDpbsQP+NeLSPFvHz3BkLigak3fQusApbrPNL9tw root@ip-10-250-0-91.cn-northwest-1.compute.internal The key's randomart image is: +---[RSA 2048]----+ | . | | . o . | | . B = | | . O X B | | + O S = . . | | . . * B = o + | | . = . B + o + | | .B + + . | | oo+.o E | +----[SHA256]-----+
此时会在.ssh目录下生成密钥对文件id_rsa和id_rsa.pub
1 2
[root@ip-10-250-0-91 .ssh]# ls authorized_keys id_rsa id_rsa.pub
[root@ip-10-250-0-91 .ssh]# ssh-copy-id -i id_rsa.pub root@node4 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub" The authenticity of host 'node4 (10.250.0.102)' can't be established. ECDSA key fingerprint is SHA256:aXIII+S5nOVy6pqP1fuaW6fYFsVIN9TBFVP/Xaf8Pds. ECDSA key fingerprint is MD5:1f:07:2f:04:75:77:68:8d:f0:20:96:f1:0b:90:ac:61. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@node4's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@node4'" and check to make sure that only the key(s) you wanted were added.
# 停止同步 mysql> stop slave; # '10.250.%.%' 表示只能局域网段内的ip才能访问,如果不限制ip,可以配置为'%' mysql> CREATE USER 'vagrant'@'10.250.%.%' IDENTIFIED BY 'vagrant'; mysql> ALTER USER 'vagrant'@'10.250.%.%' IDENTIFIED WITH mysql_native_password BY 'vagrant'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'vagrant'@'10.250.%.%'; mysql> FLUSH PRIVILEGES; # 开启同步 mysql> start slave;
mysql> CREATE USER 'mhaadmin'@'10.250.%.%' IDENTIFIED BY 'mhapass'; mysql> ALTER USER 'mhaadmin'@'10.250.%.%' IDENTIFIED WITH mysql_native_password BY 'mhapass'; mysql> GRANT ALL ON *.* TO 'mhaadmin'@'10.250.%.%'; mysql> FLUSH PRIVILEGES;
[root@ip-10-250-0-91 mha_master]# tail -f manager.log ……………………………… Started automated(non-interactive) failover. Selected 10.250.0.186(10.250.0.186:3306) as a new master. 10.250.0.186(10.250.0.186:3306): OK: Applying all logs succeeded. 10.250.0.102(10.250.0.102:3306): ERROR: Failed on waiting gtid execset on master. Master failover to 10.250.0.186(10.250.0.186:3306) done, but recovery on slave partially failed.
mysql> show variables like "%read_only%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_read_only | OFF | | read_only | OFF | | super_read_only | OFF | | transaction_read_only | OFF | +-----------------------+-------+
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting forsource to send event Master_Host: 10.250.0.186 Master_User: vagrant Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000013 Read_Master_Log_Pos: 2466 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 420 Relay_Master_Log_File: mysql-bin.000013 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1396 Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '6e9a571e-330e-11ed-a3f8-0a53e7cced43:1' at master log mysql-bin.000013, end_log_pos 1465. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
………… Started automated(non-interactive) failover. Selected 10.250.0.186(10.250.0.186:3306) as a new master. 10.250.0.186(10.250.0.186:3306): OK: Applying all logs succeeded. 10.250.0.102(10.250.0.102:3306): OK: Slave started, replicating from 10.250.0.186(10.250.0.186:3306) 10.250.0.186(10.250.0.186:3306): Resetting slave info succeeded. Master failover to 10.250.0.186(10.250.0.186:3306) completed successfully.
[root@ip-10-250-0-91 mha_master]# masterha_check_repl --conf=/etc/mha_master/mha.cnf ……………… Thu Sep 15 10:16:36 2022 - [info] Checking replication health on 10.250.0.186.. Thu Sep 15 10:16:36 2022 - [info] ok. Thu Sep 15 10:16:36 2022 - [info] Checking replication health on 10.250.0.102.. Thu Sep 15 10:16:36 2022 - [info] ok. Thu Sep 15 10:16:36 2022 - [warning] master_ip_failover_script is not defined. Thu Sep 15 10:16:36 2022 - [warning] shutdown_script is not defined. Thu Sep 15 10:16:36 2022 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
这里有两个警告,master_ip_failover_script is not defined.和shutdown_script is not defined. shutdown_script脚本用于指定故障发生后关闭故障主机的脚本,而master_ip_failover_script脚本用于指定故障转移时需要做的操作。
Checking the Status of the script.. OK Fri Sep 16 07:06:02 2022 - [info] OK. Fri Sep 16 07:06:02 2022 - [warning] shutdown_script is not defined. Fri Sep 16 07:06:02 2022 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
重新启动MHA
停止mysql-master服务
查看MHA日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
----- Failover Report -----
mha: MySQL Master failover 10.250.0.118(10.250.0.118:3306) to 10.250.0.186(10.250.0.186:3306) succeeded
Master 10.250.0.118(10.250.0.118:3306) is down!
Check MHA Manager logs at ip-10-250-0-91.cn-northwest-1.compute.internal:/etc/mha_master/manager.log for details.
Started automated(non-interactive) failover. Invalidated master IP address on 10.250.0.118(10.250.0.118:3306) Selected 10.250.0.186(10.250.0.186:3306) as a new master. 10.250.0.186(10.250.0.186:3306): OK: Applying all logs succeeded. 10.250.0.186(10.250.0.186:3306): OK: Activated master IP address. 10.250.0.102(10.250.0.102:3306): OK: Slave started, replicating from 10.250.0.186(10.250.0.186:3306) 10.250.0.186(10.250.0.186:3306): Resetting slave info succeeded. Master failover to 10.250.0.186(10.250.0.186:3306) completed successfully.