http://www.cnblogs.com/gomysql/p/3675429.html
- 环境准备
- server 1
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
- server n
mha4mysql-node-0.56-0.el6.noarch.rpm
vim /etc/init.d/mysqld
112 [ $ret -ne 0 ] && return $ret
113 #initfile="$(install_validate_password_sql_file)"
114 #action $"Installing validate password plugin: " /usr/sbin/mysqld --datadir="$datadir" - -user=mysql --init-file="$initfile"
115 #ret=$?
116 #rm -f "$initfile"
117 chown -R mysql:mysql "$datadir"
1:搭建主从复制
/etc/my.cnf(三个一样)
关闭密钥强壮度检测
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
log_slave_updates=ON
log_bin=binlog
- master
grant replication slave on *.* to repl@'%' identified by 'Zeinima748!!';
flush privileges;
- slave
change master to master_host='172.25.200.1',master_user='repl',master_password='Zeinima748!!',master_auto_position=1;
use mysql;
select * from plugin;
delete from plugin;
start slave;
show slave status\G;(两个yes)
2:写配置文件
[root@server1 conf]# mkdir /usr/local/mastermha
[root@server1 mastermha]# pwd
/usr/local/mastermha
[root@server1 mastermha]# cat app1.cnf
[server default]
user=root
password=Zeinima748!!
ssh_user=root
repl_password=Zeinima748!!
repl_user=repl
master_binlog_dir= /var/lib/mysql
remote_workdir=/tmp
ping_interval=1
manager_workdir=/usr/local/masterha/
manager_log=/usr/local/masterha/manager.log
# master_ip_failover_script= /script/masterha/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
# master_ip_online_change_script= /script/masterha/master_ip_online_change
[server1]
hostname=172.25.200.1
[server2]
hostname=172.25.200.2
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.200.4
no_master=1
3:制作免密认证
[root@server1 mastermha]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which 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:
b5:2c:70:91:83:ae:c5:0c:30:f8:39:35:3e:b6:38:19 root@server1
The key's randomart image is:
+--[ RSA 2048]----+
| .o. ... |
|. .+ . o. |
| . + *. ... |
| E + =o o . |
| * = S o |
| + o . |
| . |
| |
| |
+-----------------+
[root@server1 mastermha]# ssh-copy-id 172.25.200.2
root@172.25.200.2's password:
Now try logging into the machine, with "ssh '172.25.200.2'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@server1 mastermha]# ssh-copy-id 172.25.200.4
root@172.25.200.4's password:
Now try logging into the machine, with "ssh '172.25.200.4'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
- 检测免密
[root@server1 mastermha]# masterha_check_ssh --conf=/usr/local/mastermha/app1.cnf
Wed Jan 31 15:59:20 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jan 31 15:59:20 2018 - [info] Reading application default configuration from /usr/local/mastermha/app1.cnf..
Wed Jan 31 15:59:20 2018 - [info] Reading server configuration from /usr/local/mastermha/app1.cnf..
Wed Jan 31 15:59:20 2018 - [info] Starting SSH connection tests..
Wed Jan 31 15:59:21 2018 - [debug]
Wed Jan 31 15:59:20 2018 - [debug] Connecting via SSH from root@172.25.200.1(172.25.200.1:22) to root@172.25.200.2(172.25.200.2:22)..
Wed Jan 31 15:59:20 2018 - [debug] ok.
Wed Jan 31 15:59:20 2018 - [debug] Connecting via SSH from root@172.25.200.1(172.25.200.1:22) to root@172.25.200.4(172.25.200.4:22)..
Wed Jan 31 15:59:21 2018 - [debug] ok.
Wed Jan 31 15:59:21 2018 - [debug]
Wed Jan 31 15:59:21 2018 - [debug] Connecting via SSH from root@172.25.200.2(172.25.200.2:22) to root@172.25.200.1(172.25.200.1:22)..
Wed Jan 31 15:59:21 2018 - [debug] ok.
Wed Jan 31 15:59:21 2018 - [debug] Connecting via SSH from root@172.25.200.2(172.25.200.2:22) to root@172.25.200.4(172.25.200.4:22)..
Wed Jan 31 15:59:21 2018 - [debug] ok.
Wed Jan 31 15:59:22 2018 - [debug]
Wed Jan 31 15:59:21 2018 - [debug] Connecting via SSH from root@172.25.200.4(172.25.200.4:22) to root@172.25.200.1(172.25.200.1:22)..
Wed Jan 31 15:59:21 2018 - [debug] ok.
Wed Jan 31 15:59:21 2018 - [debug] Connecting via SSH from root@172.25.200.4(172.25.200.4:22) to root@172.25.200.2(172.25.200.2:22)..
Wed Jan 31 15:59:22 2018 - [debug] ok.
Wed Jan 31 15:59:22 2018 - [info] All SSH connection tests passed successfully.
- 数据库中配置远程登陆
mysql> grant all on *.* to root@'%' identified by 'Zeinima748!!';
Query OK, 0 rows affected, 1 warning (0.15 sec)
- 检测repl
[root@server1 mastermha]# masterha_check_repl --conf=/usr/local/mastermha/app1.cnf
Wed Jan 31 16:04:06 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jan 31 16:04:06 2018 - [info] Reading application default configuration from /usr/local/mastermha/app1.cnf..
Wed Jan 31 16:04:06 2018 - [info] Reading server configuration from /usr/local/mastermha/app1.cnf..
Wed Jan 31 16:04:06 2018 - [info] MHA::MasterMonitor version 0.56.
Creating directory /usr/local/masterha/.. done.
Wed Jan 31 16:04:06 2018 - [info] GTID failover mode = 1
Wed Jan 31 16:04:06 2018 - [info] Dead Servers:
Wed Jan 31 16:04:06 2018 - [info] Alive Servers:
Wed Jan 31 16:04:06 2018 - [info] 172.25.200.1(172.25.200.1:3306)
Wed Jan 31 16:04:06 2018 - [info] 172.25.200.2(172.25.200.2:3306)
Wed Jan 31 16:04:06 2018 - [info] 172.25.200.4(172.25.200.4:3306)
Wed Jan 31 16:04:06 2018 - [info] Alive Slaves:
Wed Jan 31 16:04:06 2018 - [info] 172.25.200.2(172.25.200.2:3306) Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Wed Jan 31 16:04:06 2018 - [info] GTID ON
Wed Jan 31 16:04:06 2018 - [info] Replicating from 172.25.200.1(172.25.200.1:3306)
Wed Jan 31 16:04:06 2018 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jan 31 16:04:06 2018 - [info] 172.25.200.4(172.25.200.4:3306) Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Wed Jan 31 16:04:06 2018 - [info] GTID ON
Wed Jan 31 16:04:06 2018 - [info] Replicating from 172.25.200.1(172.25.200.1:3306)
Wed Jan 31 16:04:06 2018 - [info] Not candidate for the new Master (no_master is set)
Wed Jan 31 16:04:06 2018 - [info] Current Alive Master: 172.25.200.1(172.25.200.1:3306)
Wed Jan 31 16:04:06 2018 - [info] Checking slave configurations..
Wed Jan 31 16:04:06 2018 - [info] read_only=1 is not set on slave 172.25.200.2(172.25.200.2:3306).
Wed Jan 31 16:04:06 2018 - [info] read_only=1 is not set on slave 172.25.200.4(172.25.200.4:3306).
Wed Jan 31 16:04:06 2018 - [info] Checking replication filtering settings..
Wed Jan 31 16:04:06 2018 - [info] binlog_do_db= , binlog_ignore_db=
Wed Jan 31 16:04:06 2018 - [info] Replication filtering check ok.
Wed Jan 31 16:04:06 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Wed Jan 31 16:04:06 2018 - [info] Checking SSH publickey authentication settings on the current master..
Wed Jan 31 16:04:06 2018 - [info] HealthCheck: SSH to 172.25.200.1 is reachable.
Wed Jan 31 16:04:06 2018 - [info]
172.25.200.1(172.25.200.1:3306) (current master)
+--172.25.200.2(172.25.200.2:3306)
+--172.25.200.4(172.25.200.4:3306)
Wed Jan 31 16:04:06 2018 - [info] Checking replication health on 172.25.200.2..
Wed Jan 31 16:04:06 2018 - [info] ok.
Wed Jan 31 16:04:06 2018 - [info] Checking replication health on 172.25.200.4..
Wed Jan 31 16:04:06 2018 - [info] ok.
Wed Jan 31 16:04:06 2018 - [warning] master_ip_failover_script is not defined.
Wed Jan 31 16:04:06 2018 - [warning] shutdown_script is not defined.
Wed Jan 31 16:04:06 2018 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
4:启动管理器
[root@server1 masterha]# ls
app1.cnf
[root@server1 masterha]# nohup masterha_manager --conf=/usr/local/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover &
[1] 5002
[root@server1 masterha]# nohup: ignoring input and appending output to `nohup.out'
[root@server1 masterha]# ls
app1.cnf app1.master_status.health manager.log nohup.out
测试
[root@server1 masterha]# pkill -9 mysqld
检测是否可以进行切换,分别查看备机slave状态和从机的主机
mysql> show slave status\G;
Empty set (0.00 sec)
ERROR:
No query specified
mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------+
| binlog.000003 | 2152 | | | 05e10101-064d-11e8-8b0b-525400f2a3c0:1-6,
7ac7d860-064d-11e8-8b07-5254004c0507:1-3 |
+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
- 从机
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.200.2
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 2152
Relay_Log_File: server4-relay-bin.000003
Relay_Log_Pos: 989
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2152
Relay_Log_Space: 1700
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 3
Master_UUID: 7ac7d860-064d-11e8-8b07-5254004c0507
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 7ac7d860-064d-11e8-8b07-5254004c0507:1-3
Executed_Gtid_Set: 05e10101-064d-11e8-8b0b-525400f2a3c0:1-6,
7ac7d860-064d-11e8-8b07-5254004c0507:1-3,
ee3f33a6-064d-11e8-93d5-5254002799c4:1-4
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
- 测试
关闭集群手动在线切换
masterha_master_switch --conf=/usr/local/masterha/app1.cnf --master_state=alive --new_master_host=172.25.200.2 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=1000
模拟数据库发生故障切换,关闭集群管理器
masterha_master_switch --master_state=dead --conf=/usr/local/masterha/app1.cnf --dead_master_host=172.25.200.1 --dead_master_port=3306 --new_master_host=172.25.200.2 --new_master_port=3306