趁着最近几天休息,就把之前没有补全的博客也来补补。
今天就和大家聊聊MySQL高可用方案中的MHA
在中小型架构中,针对MySQL做高可用 用的最多的可能就是keepalived/heartbeat+MySQL Replication。
这种方案配置简单、维护方便,并且也能实现MySQL的故障转移,因此更多的倾向于此架构
MHA是什么?
MHA是由日本Mysql专家用Perl写的一套Mysql故障切换方案,来保障数据库的高可用性,它的功能是能在0-30s之内实现主Mysql故障转移(failover),MHA故障转移可以很好的帮我们解决从库数据的一致性问题,同时最大化挽回故障发生后数据的一致性。
MHA里有两个角色一个是node节点 一个是manager节点,要实现这个MHA,必须最少要三台数据库服务器,一主多备,即一台充当master,一台充当master的备份机,另外一台是从属机,这里实验为了实现更好的效果使用四台机器,需要说明的是一旦主服务器宕机,备份机即开始充当master提供服务,如果主服务器上线也不会再成为master了,因为如果这样数据库的一致性就被改变了
这里有一个mha的网络拓扑图,我们先来简单的介绍下
先来聊聊这个拓扑图,文章开篇也有过介绍,MHA有两个重要的角色,一个是manager,另外一个是node;
从这个拓扑图中,或许大家不难分辨出如下信息
192.168.1.117 manager 管理节点 192.168.1.116 master 主库 192.168.1.118 slave01 从库 + 备库 192.168.1.119 slave02 从库
一、环境初始化
1、修改主机名
主机: manager执行命令 # sed -i 's/HOSTNAME=.*/HOSTNAME=manager/g' /etc/sysconfig/network && hostname manager 主机: master执行命令 # sed -i 's/HOSTNAME=.*/HOSTNAME=master/g' /etc/sysconfig/network && hostname master 主机: slave01执行命令 # sed -i 's/HOSTNAME=.*/HOSTNAME=slave01/g' /etc/sysconfig/network && hostname slave01 主机: slave02执行命令 # sed -i 's/HOSTNAME=.*/HOSTNAME=slave02/g' /etc/sysconfig/network && hostname slave02
2、主机名解析
在manager上执行如下命令 [root@manager ~]# cat >> /etc/hosts << EOF > 192.168.1.117 manager > 192.168.1.116 master > 192.168.1.118 slave01 > 192.168.1.119 slave02 > EOF [root@manager ~]# scp -o StrictHostKeyChecking=no /etc/hosts root@master:/etc/ [root@manager ~]# scp -o StrictHostKeyChecking=no /etc/hosts root@slave01:/etc/ [root@manager ~]# scp -o StrictHostKeyChecking=no /etc/hosts root@slave02:/etc/
3、ssh无密码登录
主机: manager执行命令 [root@manager ~]# ssh-keygen -t rsa [root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master [root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01 [root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02 主机: master执行命令 [root@master ~]# ssh-keygen -t rsa [root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager [root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01 [root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02 主机: slave01执行命令 [root@slave01 ~]# ssh-keygen -t rsa [root@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager [root@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master [root@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02 主机: slave02执行命令 [root@slave02 ~]# ssh-keygen -t rsa [root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager [root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master [root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01
二、规划mysql
1、安装mysql
在master、slave01和slave02上安装mysql服务,这里安装的方式是源码编译mysql-5.6.26提供脚本
# sh mysql-5.6.26.sh # . /etc/profile
2、配置master、slave01和slave02之间的主从复制
在MySQL5.6 的Replication配置中,master端同样要开启两个重要的选项,server-id和log-bin,并且选项server-id在全局架构中并且唯一,不能被其它主机使用,这里采用主机ip地址的最后一位充当server-id的值;slave端要开启relay-log;
主机: master执行命令 [root@master ~]# egrep "log-bin|server-id" /etc/my.cnf server-id = 116 log-bin-index = master-bin.index 主机: slave01执行命令 [root@slave01 ~]# egrep "log-bin|server-id" /etc/my.cnf server-id = 118 log-bin-index = master-bin.index 主机: slave02执行命令 [root@slave02 ~]# egrep "log-bin|server-id" /etc/my.cnf server-id = 119 log-bin-index = master-bin.index
3、在master、slave01上创建主从同步的账号。slave01是备用master,这个也需要建立授权用户
[root@master ~]# mysql -uroot -p123456 -e "grant all privileges on *.* to 'rep'@'192.168.1.%' identified by 'rep123';flush privileges" [root@slave01 ~]# mysql -uroot -p123456 -e "grant all privileges on *.* to 'rep'@'192.168.1.%' identified by 'rep123';flush privileges"
4、在master上执行命令,查看master状态信息
[root@master ~]# mysql -uroot -p123456 -e 'show master status;' Warning: Using a password on the command line interface can be insecure. +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000004 | 403 | | | | +-------------------+----------+--------------+------------------+-------------------+
5、在slave01和slave02上执行主从同步
slave01
[root@slave01 ~]# mysql -uroot -p123456 mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.1.116', -> MASTER_USER='rep', -> MASTER_PASSWORD='rep123', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='master-bin.000004', -> MASTER_LOG_POS=403; mysql> start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.116 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000004 Read_Master_Log_Pos: 403 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000004 Slave_IO_Running: Yes #表示主从ok 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: 403 Relay_Log_Space: 457 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: 106 Master_UUID: 8a331603-5c17-11e5-a60a-000c29068cb0 Master_Info_File: /mydata/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.01 sec) ERROR: No query specified
slave02
[root@slave02 ~]# mysql -uroot -p123456 mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.1.116', -> MASTER_USER='rep', -> MASTER_PASSWORD='rep123', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='master-bin.000004', -> MASTER_LOG_POS=403; mysql> start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.116 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000004 Read_Master_Log_Pos: 403 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000004 Slave_IO_Running: Yes #表示主从ok 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: 403 Relay_Log_Space: 457 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: 106 Master_UUID: 8a331603-5c17-11e5-a60a-000c29068cb0 Master_Info_File: /mydata/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.00 sec)
实验到这里表示主从已经配置完成!接下来我们就开始规划mha
三、规划mha
1、创建mha管理用的复制账号,每台数据库上都要创建4个账号,在这里以其中master为例
[root@master ~]# mysql -uroot -p123456 mysql> grant all privileges on *.* to 'mha_rep'@'192.168.1.117' identified by '123456'; mysql> grant all privileges on *.* to 'mha_rep'@'192.168.1.116' identified by '123456'; mysql> grant all privileges on *.* to 'mha_rep'@'192.168.1.118' identified by '123456'; mysql> grant all privileges on *.* to 'mha_rep'@'192.168.1.119' identified by '123456'; mysql> flush privileges; mysql> select user,host,password from mysql.user where user='mha_rep'; +---------+---------------+-------------------------------------------+ | user | host | password | +---------+---------------+-------------------------------------------+ | mha_rep | 192.168.1.117 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | mha_rep | 192.168.1.116 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | mha_rep | 192.168.1.118 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | mha_rep | 192.168.1.119 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +---------+---------------+-------------------------------------------+
2、在3台主机上(master、slave01和slave02)上分别安装mha4mysql-node包,这里以master为例,其它主机同理。
准确的来讲的话,应该是所有的节点包括manager和node的所有节点都要安装mha4mysql-node包,只不过等会manager要安装node节点也要安装manager节点,所以吧manager单独在下面安装了。
[root@master ~]# yum install perl-DBD-MySQL -y [root@master ~]# wget https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm [root@master ~]# rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
3、在manager上安装mha4mysql-manager和mha4mysql-node包
[root@manager ~]# yum install perl cpan perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Net-Telnet -y
注释:由于yum源里没有这四个安装包,因此我们需要单独下载来安装。
[root@manager ~]# wget http://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm [root@manager ~]# wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm [root@manager ~]# wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Mail-Sender-0.8.16-1.el6.rf.noarch.rpm [root@manager ~]# wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Mail-Sendmail-0.79-1.2.el6.rf.noarch.rpm [root@manager ~]# yum localinstall *.rpm -y
安装manager和node包
[root@manager ~]# wget [root@manager ~]# wget [root@manager ~]# yum localinstall *.rpm
4、查看mha4mysql-manager安装了哪些工具
[root@manager ~]# rpm -ql mha4mysql-manager |grep bin /usr/bin/masterha_check_repl /usr/bin/masterha_check_ssh /usr/bin/masterha_check_status /usr/bin/masterha_conf_host /usr/bin/masterha_manager /usr/bin/masterha_master_monitor /usr/bin/masterha_master_switch /usr/bin/masterha_secondary_check /usr/bin/masterha_stop
5、在manager主机上下载mha4mysql-manager的源码包
# wget https://downloads.mariadb.com/files/MHA/mha4mysql-manager-0.56.tar.gz
6、在manager主机上从mha4mysql-manager的源码包中提取mha的配置配置文件和脚本
[root@manager ~]# tar xf mha4mysql-manager-0.56.tar.gz [root@manager ~]# mkdir -p /usr/local/mha/scripts [root@manager ~]# cp mha4mysql-manager-0.56/samples/scripts/* /usr/local/mha/scripts/ [root@manager ~]# cp mha4mysql-manager-0.56/samples/conf/app1.cnf /usr/local/mha/mha.cnf [root@manager ~]# tree /usr/local/mha/ /usr/local/mha/ ├── mha.cnf └── scripts ├── master_ip_failover ├── master_ip_online_change ├── power_manager └── send_report
7、修改manager端mha的配置文件,如下
[root@manager ~]# cat /usr/local/mha/mha.cnf [server default] user=mha_rep password=123456 ssh_user=root repl_user=rep repl_password=rep123 ping_interval=1 manager_workdir=/usr/local/mha manager_log=/usr/local/mha/manager.log # monitor mysql secondary_check_script= masterha_secondary_check -s 192.168.1.116 -s 192.168.1.118 -s 192.168.1.119 report_script= /usr/local/mha/scripts/send_report master_ip_online_change_script= /usr/local/mha/scripts/master_ip_online_change #master_ip_failover_script=/usr/local/mha/scripts/master_ip_failover #shutdown_script= /usr/local/mha/scripts/power_manager [server1] hostname=master ssh_port=22 candidate_master=1 master_binlog_dir=/mydata/data [server2] hostname=slave01 ssh_port=22 candidate_master=1 master_binlog_dir=/mydata/data [server3] hostname=slave02 ssh_port=22 no_master=1 master_binlog_dir=/mydata/data
8、检查ssh是否畅通
[root@manager ~]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf Tue Sep 15 23:33:34 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Sep 15 23:33:34 2015 - [info] Reading application default configurations from /usr/local/mha/mha.cnf.. Tue Sep 15 23:33:34 2015 - [info] Reading server configurations from /usr/local/mha/mha.cnf.. Tue Sep 15 23:33:34 2015 - [info] Starting SSH connection tests.. Tue Sep 15 23:33:34 2015 - [debug] Tue Sep 15 23:33:34 2015 - [debug] Connecting via SSH from root@master(192.168.1.116:22) to root@slave01(192.168.1.118:22).. Tue Sep 15 23:33:34 2015 - [debug] ok. Tue Sep 15 23:33:34 2015 - [debug] Connecting via SSH from root@master(192.168.1.116:22) to root@slave02(192.168.1.119:22).. Tue Sep 15 23:33:34 2015 - [debug] ok. Tue Sep 15 23:33:35 2015 - [debug] Tue Sep 15 23:33:34 2015 - [debug] Connecting via SSH from root@slave01(192.168.1.118:22) to root@master(192.168.1.116:22).. Tue Sep 15 23:33:35 2015 - [debug] ok. Tue Sep 15 23:33:35 2015 - [debug] Connecting via SSH from root@slave01(192.168.1.118:22) to root@slave02(192.168.1.119:22).. Tue Sep 15 23:33:35 2015 - [debug] ok. Tue Sep 15 23:33:35 2015 - [debug] Tue Sep 15 23:33:35 2015 - [debug] Connecting via SSH from root@slave02(192.168.1.119:22) to root@master(192.168.1.116:22).. Tue Sep 15 23:33:35 2015 - [debug] ok. Tue Sep 15 23:33:35 2015 - [debug] Connecting via SSH from root@slave02(192.168.1.119:22) to root@slave01(192.168.1.118:22).. Tue Sep 15 23:33:35 2015 - [debug] ok. Tue Sep 15 23:33:35 2015 - [info] All SSH connection tests passed successfully.
如果得到以上结果,表明主机之间ssh互信是畅通的
9、检查主从复制是否正常
执行主从复制检查的时候,这个由于我是用源码变异的mysql会出现路径找不到的问题;比如
(1) Can't exec "mysqlbinlog": No such file or directory at /usr/local/perl5/MHA/BinlogManager.pm line 99. 解决办法: 在master、slave01和slave02上分别执行如下命令 # ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog (2)mysqlbinlog: unknown variable 'default-character-set=utf8' 解决办法: 在master、slave01和slave02上分别执行注释client部分的default-character-set=utf8选项,并重启mysqld服务 (3)Testing mysql connection and privileges..sh: mysql: command not found 在master、slave01和slave02上分别执行如下命令 # ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@manager ~]# masterha_check_repl --conf=/usr/local/mha/mha.cnf Tue Sep 15 23:45:35 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Sep 15 23:45:35 2015 - [info] Reading application default configurations from /usr/local/mha/mha.cnf.. Tue Sep 15 23:45:35 2015 - [info] Reading server configurations from /usr/local/mha/mha.cnf.. Tue Sep 15 23:45:35 2015 - [info] MHA::MasterMonitor version 0.55. Tue Sep 15 23:45:35 2015 - [info] Dead Servers: Tue Sep 15 23:45:35 2015 - [info] Alive Servers: Tue Sep 15 23:45:35 2015 - [info] master(192.168.1.116:3306) Tue Sep 15 23:45:35 2015 - [info] slave01(192.168.1.118:3306) Tue Sep 15 23:45:35 2015 - [info] slave02(192.168.1.119:3306) Tue Sep 15 23:45:35 2015 - [info] Alive Slaves: Tue Sep 15 23:45:35 2015 - [info] slave01(192.168.1.118:3306) Version=5.6.26-log (oldest major version between slaves) log-bin:enabled Tue Sep 15 23:45:35 2015 - [info] Replicating from 192.168.1.116(192.168.1.116:3306) Tue Sep 15 23:45:35 2015 - [info] Primary candidate for the new Master (candidate_master is set) Tue Sep 15 23:45:35 2015 - [info] slave02(192.168.1.119:3306) Version=5.6.26-log (oldest major version between slaves) log-bin:enabled Tue Sep 15 23:45:35 2015 - [info] Replicating from 192.168.1.116(192.168.1.116:3306) Tue Sep 15 23:45:35 2015 - [info] Not candidate for the new Master (no_master is set) Tue Sep 15 23:45:35 2015 - [info] Current Alive Master: master(192.168.1.116:3306) Tue Sep 15 23:45:35 2015 - [info] Checking slave configurations.. Tue Sep 15 23:45:35 2015 - [info] read_only=1 is not set on slave slave01(192.168.1.118:3306). Tue Sep 15 23:45:35 2015 - [warning] relay_log_purge=0 is not set on slave slave01(192.168.1.118:3306). Tue Sep 15 23:45:35 2015 - [info] read_only=1 is not set on slave slave02(192.168.1.119:3306). Tue Sep 15 23:45:35 2015 - [warning] relay_log_purge=0 is not set on slave slave02(192.168.1.119:3306). Tue Sep 15 23:45:35 2015 - [info] Checking replication filtering settings.. Tue Sep 15 23:45:35 2015 - [info] binlog_do_db= , binlog_ignore_db= Tue Sep 15 23:45:35 2015 - [info] Replication filtering check ok. Tue Sep 15 23:45:35 2015 - [info] Starting SSH connection tests.. Tue Sep 15 23:45:37 2015 - [info] All SSH connection tests passed successfully. Tue Sep 15 23:45:37 2015 - [info] Checking MHA Node version.. Tue Sep 15 23:45:37 2015 - [info] Version check ok. Tue Sep 15 23:45:37 2015 - [info] Checking SSH publickey authentication settings on the current master.. Tue Sep 15 23:45:37 2015 - [info] HealthCheck: SSH to master is reachable. Tue Sep 15 23:45:37 2015 - [info] Master MHA Node version is 0.54. Tue Sep 15 23:45:37 2015 - [info] Checking recovery script configurations on the current master.. Tue Sep 15 23:45:37 2015 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/mydata/data --output_file=/var/tmp/save_binary_logs_test --manager_version=0.55 --start_file=master-bin.000005 Tue Sep 15 23:45:37 2015 - [info] Connecting to root@master(master).. Creating /var/tmp if not exists.. ok. Checking output directory is accessible or not.. ok. Binlog found at /mydata/data, up to master-bin.000005 Tue Sep 15 23:45:37 2015 - [info] Master setting check done. Tue Sep 15 23:45:37 2015 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers.. Tue Sep 15 23:45:37 2015 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=slave01 --slave_ip=192.168.1.118 --slave_port=3306 --workdir=/var/tmp --target_version=5.6.26-log --manager_version=0.55 --relay_log_info=/mydata/data/relay-log.info --relay_dir=/mydata/data/ --slave_pass=xxx Tue Sep 15 23:45:37 2015 - [info] Connecting to [email protected](slave01:22).. Checking slave recovery environment settings.. Opening /mydata/data/relay-log.info ... ok. Relay log found at /mydata/data, up to mysql-relay-bin.000005 Temporary relay log file is /mydata/data/mysql-relay-bin.000005 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Tue Sep 15 23:45:38 2015 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=slave02 --slave_ip=192.168.1.119 --slave_port=3306 --workdir=/var/tmp --target_version=5.6.26-log --manager_version=0.55 --relay_log_info=/mydata/data/relay-log.info --relay_dir=/mydata/data/ --slave_pass=xxx Tue Sep 15 23:45:38 2015 - [info] Connecting to [email protected](slave02:22).. Checking slave recovery environment settings.. Opening /mydata/data/relay-log.info ... ok. Relay log found at /mydata/data, up to mysql-relay-bin.000005 Temporary relay log file is /mydata/data/mysql-relay-bin.000005 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Tue Sep 15 23:45:38 2015 - [info] Slaves settings check done. Tue Sep 15 23:45:38 2015 - [info] master (current master) +--slave01 +--slave02 Tue Sep 15 23:45:38 2015 - [info] Checking replication health on slave01.. Tue Sep 15 23:45:38 2015 - [info] ok. Tue Sep 15 23:45:38 2015 - [info] Checking replication health on slave02.. Tue Sep 15 23:45:38 2015 - [info] ok. Tue Sep 15 23:45:38 2015 - [warning] master_ip_failover_script is not defined. Tue Sep 15 23:45:38 2015 - [warning] shutdown_script is not defined. Tue Sep 15 23:45:38 2015 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
或者在命令执行后大家会看到有警告信息;比如
Tue Sep 15 23:45:35 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. 在命令执行后输出结果的第一行有这样的警告信息,找不到masterha_default.cnf,其实这个文件是mha的全局默认配置文件,由于我们没有使用全局,所以就跳过了这项,不过不妨碍真个环境。如果大家想用,其实也是可以的,在源码包里就有这个默认的模板配置文件,大家只需要稍作修改就可以排查这个警告信息 Tue Sep 15 23:45:38 2015 - [warning] master_ip_failover_script is not defined. Tue Sep 15 23:45:38 2015 - [warning] shutdown_script is not defined. 在命令执行后输出结果的最后几行中,提示未定义,大家看看/usr/local/mha/mha.cnf文件中,我们正好注释了这两行代码,其中master_ip_failover_script是后期做vip的时候才用到的。
四、mha实验模拟
1、在每次做mha实验的时候,我们都最好先执行如下命令做检测
[root@manager ~]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf [root@manager ~]# masterha_check_repl --conf=/usr/local/mha/mha.cnf
确定两条命令的返回结果都是无异常的,然后启动mha服务
2、在manager端启动mha服务并时刻监控日志文件的输出变化
[root@manager ~]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 & [root@manager ~]# ps -ef |grep masterha |grep -v 'grep' root 1595 1140 1 23:55 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/usr/local/mha/mha.cnf
3、实验流程第一阶段
准备,先来检查主从是否都均已正常
首先,停止master端的mysqld服务进程,然后查看备库也就是slave01是否已经提升到主库
其次,登录slave02端查看主从是否正常,是否更新到新的master的ip上也就是是否执行slave01的ip地址
最后,启动master端的mysqld服务进程,并将其加入到主从模式中
准备,实验开始
在slave01和slave02上执行,检查主从同步是否都正常,这里以slave01为例,slave02同理 [root@slave01 ~]# mysql -uroot -p123456 -e 'show slave status\G' |egrep 'Slave_IO_Running:|Slave_SQL_Running:' Warning: Using a password on the command line interface can be insecure. Slave_IO_Running: Yes #表示ok Slave_SQL_Running: Yes
首先,实验开始
(1)在master端上执行命令来停止mysqld服务进程 [root@master ~]# /etc/init.d/mysqld stop Shutting down MySQL.... SUCCESS! (2)查看manager端的mha输出日志,在这里只截取了一部分日志信息 [root@manager ~]# tail -f /usr/local/mha/manager.log ----- Failover Report ----- mha: MySQL Master failover master to slave01 succeeded #表示Master由master转移到slave01 Master master is down! #表示master已经down机 Check MHA Manager logs at manager:/usr/local/mha/manager.log for details. Started automated(non-interactive) failover. The latest slave slave01(192.168.1.118:3306) has all relay logs for recovery. Selected slave01 as a new master. slave01: OK: Applying all logs succeeded. slave02: This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. slave02: OK: Applying all logs succeeded. Slave started, replicating from slave01. slave01: Resetting slave info succeeded. Master failover to slave01(192.168.1.118:3306) completed successfully. Wed Sep 16 00:04:02 2015 - [info] Sending mail.. Unknown option: conf
其次,实验开始
登录slave02查看主从同步是否正常,查看是否已经转移到新的master的ip上 [root@slave02 ~]# mysql -uroot -p123456 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:' Warning: Using a password on the command line interface can be insecure. Master_Host: 192.168.1.118 #表明已经转移新的ip Slave_IO_Running: Yes #表示主从ok Slave_SQL_Running: Yes
最后,实验开始
(1)在master端启动mysqld服务 [root@master ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! (2)在manager端的mha日志文件中找到主从同步的sql语句,这条语句只需要修改密码即可使用 [root@manager ~]# grep 'MASTER_HOST' /usr/local/mha/manager.log |tail -n 1 Wed Sep 16 00:04:01 2015 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='slave01 or 192.168.1.118', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000006', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='xxx'; 注意: MASTER_HOST='slave01 or 192.168.1.118' 这个位置需要注意一下,最好只写一个并建议写ip地址 (3)在master上启动主从同步,密码为rep123 [root@master ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='192.168.1.118', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000006', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='rep123'; start slave;" [root@master ~]# mysql -uroot -p123456 -e "show slave status\G" Warning: Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.118 #slave01的ip地址 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000006 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000006 Slave_IO_Running: Yes #主从ok 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: 120 Relay_Log_Space: 457 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: 118 Master_UUID: 90c54d84-5c17-11e5-a60a-000c29d3bb11 Master_Info_File: /mydata/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0
4、实验流程第二阶段
准备,先来检查主从是否都均已正常
首先,停止slave01端的mysqld服务进程,然后查看master是否已经提升到新的主库
其次,登录slave02端查看主从是否正常,是否更新到新的master的ip上也就是是否执行master的ip地址
最后,启动master端的mysqld服务进程,并将其加入到主从模式中
这里强调下,默认情况下每次主备库切换后,mha服务都会停止。
在这里我们需要重新启动mha服务
[root@manager ~]# rm -rf /usr/local/mha/mha.failover.complete [root@manager ~]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 & [1] 2219 [root@manager ~]# ps -ef |grep masterha |grep -v 'grep' root 2219 1140 4 00:24 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/usr/local/mha/mha.cnf [root@manager ~]# [root@manager ~]# masterha_check_status --conf=/usr/local/mha/mha.cnf mha (pid:2219) is running(0:PING_OK), master:slave01 #表明现在的master是slave01主机
准备,实验开始
在master和slave02上执行,检查主从同步是否都正常,这里以master为例,slave02同理 [root@master ~]# mysql -uroot -p123456 -e 'show slave status\G' |egrep 'Slave_IO_Running:|Slave_SQL_Running:' Warning: Using a password on the command line interface can be insecure. Slave_IO_Running: Yes Slave_SQL_Running: Yes
首先,实验开始
(1)在slave01端上执行命令来停止mysqld服务进程 [root@slave01 ~]# /etc/init.d/mysqld stop Shutting down MySQL.... SUCCESS! (2)查看manager端的mha输出日志,在这里只截取了一部分日志信息 [root@manager ~]# tail -f /usr/local/mha/manager.log ----- Failover Report ----- mha: MySQL Master failover slave01 to master succeeded #表示Master由slave01转移到master Master slave01 is down! #表示slave01已经down机 Check MHA Manager logs at manager:/usr/local/mha/manager.log for details. Started automated(non-interactive) failover. The latest slave master(192.168.1.116:3306) has all relay logs for recovery. Selected master as a new master. master: OK: Applying all logs succeeded. slave02: This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. slave02: OK: Applying all logs succeeded. Slave started, replicating from master. master: Resetting slave info succeeded. Master failover to master(192.168.1.116:3306) completed successfully. Wed Sep 16 00:28:59 2015 - [info] Sending mail.. Unknown option: conf
其次,实验开始
登录slave02查看主从同步是否正常,查看是否已经转移到新的master的ip上 [root@slave02 ~]# mysql -uroot -p123456 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:' Warning: Using a password on the command line interface can be insecure. Master_Host: 192.168.1.116 #表明已经转移新的ip Slave_IO_Running: Yes #表示主从ok Slave_SQL_Running: Yes
最后,实验开始
(1)在slave01端启动mysqld服务 [root@slave01 ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! (2)在manager端的mha日志文件中找到主从同步的sql语句,这条语句只需要修改密码即可使用 [root@manager ~]# grep 'MASTER_HOST' /usr/local/mha/manager.log | tail -n 1 Wed Sep 16 00:28:58 2015 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='master or 192.168.1.116', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000006', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='xxx'; 注意: MASTER_HOST='slave01 or 192.168.1.118' 这个位置需要注意一下,最好只写一个并建议写ip地址 (3)在slave01上启动主从同步,密码为rep123 [root@slave01 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='192.168.1.116', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000006', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='rep123'; start slave;" [root@slave01 ~]# mysql -uroot -p123456 -e "show slave status\G" Warning: Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.116 #master的ip地址 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000006 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000006 Slave_IO_Running: Yes #主从ok 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: 120 Relay_Log_Space: 457 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: 106 Master_UUID: 8a331603-5c17-11e5-a60a-000c29068cb0 Master_Info_File: /mydata/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0
实验到这一步,已经完成了主库down后,备用的从库会自动提升到主库,并且其它从库也会重新指向新的master的ip地址。但是这里却存在一个问题,就是主备库确实实现了切换,但是对外提供的ip总不能是两个吧!为了整合keepalived/heartbeat的功能,这里也引入了vip,实现无透明切换
至于如何实现vip的故障转移,网上也有很多组合,有的是用keepalived实现的故障转移,也有实现这篇文章中将要提供的脚本检测功能。
说到这里,实验过程中,大家会注意执行命令的输出结果中的警告信息[warning],下面就来说说这个吧
首先,我们看下这个脚本,我们如果想用这个vip的功能,需要打开这个选项
[root@manager ~]# grep '^#master_ip_failover_script' /usr/local/mha/mha.cnf #master_ip_failover_script=/usr/local/mha/scripts/master_ip_failover
其次,修改里面几处配置
[root@manager ~]# mv /usr/local/mha/scripts/{master_ip_failover,master_ip_failover_bak} [root@manager ~]# cat /usr/local/mha/scripts/master_ip_failover #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); my $vip = '192.168.1.99'; # Virtual IP #可修改 my $gateway = '192.168.1.1';#Gateway IP #可修改 my $interface = 'eth0'; #可修改 my $key = "1"; my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1"; my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down"; GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, ); exit &main(); sub main { print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; if ( $command eq "stop" || $command eq "stopssh" ) { # $orig_master_host, $orig_master_ip, $orig_master_port are passed. # If you manage master ip address at global catalog database, # invalidate orig_master_ip here. my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host \n"; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { # all arguments are passed. # If you manage master ip address at global catalog database, # activate new_master_ip here. # You can also grant write access (create user, set read_only=0, etc) here. my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host \n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK \n"; `ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`; exit 0; } else { &usage(); exit 1; } } # A simple system call that enable the VIP on the new master sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } # A simple system call that disable the VIP on the old_master sub stop_vip() { `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
[root@manager ~]# chmod +x /usr/local/mha/scripts/master_ip_failover
进行ssh检查
[root@manager ~]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf Wed Sep 16 00:51:22 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Wed Sep 16 00:51:22 2015 - [info] Reading application default configurations from /usr/local/mha/mha.cnf.. Wed Sep 16 00:51:22 2015 - [info] Reading server configurations from /usr/local/mha/mha.cnf.. Wed Sep 16 00:51:22 2015 - [info] Starting SSH connection tests.. Wed Sep 16 00:51:23 2015 - [debug] Wed Sep 16 00:51:22 2015 - [debug] Connecting via SSH from root@master(192.168.1.116:22) to root@slave01(192.168.1.118:22).. Wed Sep 16 00:51:22 2015 - [debug] ok. Wed Sep 16 00:51:22 2015 - [debug] Connecting via SSH from root@master(192.168.1.116:22) to root@slave02(192.168.1.119:22).. Wed Sep 16 00:51:22 2015 - [debug] ok. Wed Sep 16 00:51:23 2015 - [debug] Wed Sep 16 00:51:23 2015 - [debug] Connecting via SSH from root@slave01(192.168.1.118:22) to root@master(192.168.1.116:22).. Wed Sep 16 00:51:23 2015 - [debug] ok. Wed Sep 16 00:51:23 2015 - [debug] Connecting via SSH from root@slave01(192.168.1.118:22) to root@slave02(192.168.1.119:22).. Wed Sep 16 00:51:23 2015 - [debug] ok. Wed Sep 16 00:51:24 2015 - [debug] Wed Sep 16 00:51:23 2015 - [debug] Connecting via SSH from root@slave02(192.168.1.119:22) to root@master(192.168.1.116:22).. Wed Sep 16 00:51:23 2015 - [debug] ok. Wed Sep 16 00:51:23 2015 - [debug] Connecting via SSH from root@slave02(192.168.1.119:22) to root@slave01(192.168.1.118:22).. Wed Sep 16 00:51:23 2015 - [debug] ok. Wed Sep 16 00:51:24 2015 - [info] All SSH connection tests passed successfully.
进行主从复制检查
[root@manager ~]# masterha_check_repl --conf=/usr/local/mha/mha.cnf Wed Sep 16 00:53:59 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Wed Sep 16 00:53:59 2015 - [info] Reading application default configurations from /usr/local/mha/mha.cnf.. Wed Sep 16 00:53:59 2015 - [info] Reading server configurations from /usr/local/mha/mha.cnf.. Wed Sep 16 00:53:59 2015 - [info] MHA::MasterMonitor version 0.55. Wed Sep 16 00:53:59 2015 - [info] Dead Servers: Wed Sep 16 00:53:59 2015 - [info] Alive Servers: Wed Sep 16 00:53:59 2015 - [info] master(192.168.1.116:3306) Wed Sep 16 00:53:59 2015 - [info] slave01(192.168.1.118:3306) Wed Sep 16 00:53:59 2015 - [info] slave02(192.168.1.119:3306) Wed Sep 16 00:53:59 2015 - [info] Alive Slaves: Wed Sep 16 00:53:59 2015 - [info] slave01(192.168.1.118:3306) Version=5.6.26-log (oldest major version between slaves) log-bin:enabled Wed Sep 16 00:53:59 2015 - [info] Replicating from 192.168.1.116(192.168.1.116:3306) Wed Sep 16 00:53:59 2015 - [info] Primary candidate for the new Master (candidate_master is set) Wed Sep 16 00:53:59 2015 - [info] slave02(192.168.1.119:3306) Version=5.6.26-log (oldest major version between slaves) log-bin:enabled Wed Sep 16 00:53:59 2015 - [info] Replicating from 192.168.1.116(192.168.1.116:3306) Wed Sep 16 00:53:59 2015 - [info] Not candidate for the new Master (no_master is set) Wed Sep 16 00:53:59 2015 - [info] Current Alive Master: master(192.168.1.116:3306) Wed Sep 16 00:53:59 2015 - [info] Checking slave configurations.. Wed Sep 16 00:53:59 2015 - [info] read_only=1 is not set on slave slave01(192.168.1.118:3306). Wed Sep 16 00:53:59 2015 - [warning] relay_log_purge=0 is not set on slave slave01(192.168.1.118:3306). Wed Sep 16 00:53:59 2015 - [info] read_only=1 is not set on slave slave02(192.168.1.119:3306). Wed Sep 16 00:53:59 2015 - [warning] relay_log_purge=0 is not set on slave slave02(192.168.1.119:3306). Wed Sep 16 00:53:59 2015 - [info] Checking replication filtering settings.. Wed Sep 16 00:53:59 2015 - [info] binlog_do_db= , binlog_ignore_db= Wed Sep 16 00:53:59 2015 - [info] Replication filtering check ok. Wed Sep 16 00:53:59 2015 - [info] Starting SSH connection tests.. Wed Sep 16 00:54:00 2015 - [info] All SSH connection tests passed successfully. Wed Sep 16 00:54:00 2015 - [info] Checking MHA Node version.. Wed Sep 16 00:54:01 2015 - [info] Version check ok. Wed Sep 16 00:54:01 2015 - [info] Checking SSH publickey authentication settings on the current master.. Wed Sep 16 00:54:01 2015 - [info] HealthCheck: SSH to master is reachable. Wed Sep 16 00:54:01 2015 - [info] Master MHA Node version is 0.54. Wed Sep 16 00:54:01 2015 - [info] Checking recovery script configurations on the current master.. Wed Sep 16 00:54:01 2015 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/mydata/data --output_file=/var/tmp/save_binary_logs_test --manager_version=0.55 --start_file=master-bin.000006 Wed Sep 16 00:54:01 2015 - [info] Connecting to root@master(master).. Creating /var/tmp if not exists.. ok. Checking output directory is accessible or not.. ok. Binlog found at /mydata/data, up to master-bin.000006 Wed Sep 16 00:54:01 2015 - [info] Master setting check done. Wed Sep 16 00:54:01 2015 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers.. Wed Sep 16 00:54:01 2015 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=slave01 --slave_ip=192.168.1.118 --slave_port=3306 --workdir=/var/tmp --target_version=5.6.26-log --manager_version=0.55 --relay_log_info=/mydata/data/relay-log.info --relay_dir=/mydata/data/ --slave_pass=xxx Wed Sep 16 00:54:01 2015 - [info] Connecting to [email protected](slave01:22).. Checking slave recovery environment settings.. Opening /mydata/data/relay-log.info ... ok. Relay log found at /mydata/data, up to mysql-relay-bin.000002 Temporary relay log file is /mydata/data/mysql-relay-bin.000002 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Wed Sep 16 00:54:01 2015 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=slave02 --slave_ip=192.168.1.119 --slave_port=3306 --workdir=/var/tmp --target_version=5.6.26-log --manager_version=0.55 --relay_log_info=/mydata/data/relay-log.info --relay_dir=/mydata/data/ --slave_pass=xxx Wed Sep 16 00:54:01 2015 - [info] Connecting to [email protected](slave02:22).. Checking slave recovery environment settings.. Opening /mydata/data/relay-log.info ... ok. Relay log found at /mydata/data, up to mysql-relay-bin.000002 Temporary relay log file is /mydata/data/mysql-relay-bin.000002 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Wed Sep 16 00:54:02 2015 - [info] Slaves settings check done. Wed Sep 16 00:54:02 2015 - [info] master (current master) +--slave01 +--slave02 Wed Sep 16 00:54:02 2015 - [info] Checking replication health on slave01.. Wed Sep 16 00:54:02 2015 - [info] ok. Wed Sep 16 00:54:02 2015 - [info] Checking replication health on slave02.. Wed Sep 16 00:54:02 2015 - [info] ok. Wed Sep 16 00:54:02 2015 - [info] Checking master_ip_failover_script status: Wed Sep 16 00:54:02 2015 - [info] /usr/local/mha/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=master --orig_master_ip=192.168.1.116 --orig_master_port=3306 IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 192.168.1.99;/sbin/arping -I eth0 -c 3 -s 192.168.1.99 192.168.1.1 >/dev/null 2>&1=== Checking the Status of the script.. OK Wed Sep 16 00:54:05 2015 - [info] OK. Wed Sep 16 00:54:05 2015 - [warning] shutdown_script is not defined. Wed Sep 16 00:54:05 2015 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
如果在命令执行后的输出结果中找不到[warning] master_ip_failover_script is not defined.表示已经启动此功能
接下来,我们来启动mha服务
接下来的流程大致可以这样来做
准备,启动mha服务
首先,停止master端的mysqld进程,让slave01提供到主库并获取vip地址
其次,查看其它从库slave02上主从同步是否正常,是否重新指向新的master的地址
最后,启动master端的mysqld进程,重新加入到主从模式中
准备,实验开始
[root@manager ~]# rm -rf /usr/local/mha/mha.failover.complete [root@manager ~]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 & [1] 2714 [root@manager ~]# ps -ef |grep masterha |grep -v 'grep' root 2714 1140 1 00:55 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/usr/local/mha/mha.cnf [root@manager ~]# masterha_check_status --conf=/usr/local/mha/mha.cnf mha (pid:2714) is running(0:PING_OK), master:master
首先,实验开始
(1)在master端上执行命令来停止mysqld服务进程 [root@master ~]# /etc/init.d/mysqld stop Shutting down MySQL.... SUCCESS! (2)查看manager端的mha输出日志,在这里只截取了一部分日志信息 [root@manager ~]# tail -f /usr/local/mha/manager.log Enabling the VIP - 192.168.1.99 on the new master - slave01 #表示vip的地址是192.168.1.99已经在新的master上开启,新的master是slave01 ----- Failover Report ----- mha: MySQL Master failover master to slave01 succeeded #表示Master由master转移到slave01 Master master is down! #表示master已经down机 Check MHA Manager logs at manager:/usr/local/mha/manager.log for details. Started automated(non-interactive) failover. Invalidated master IP address on master. The latest slave slave01(192.168.1.118:3306) has all relay logs for recovery. Selected slave01 as a new master. slave01: OK: Applying all logs succeeded. slave01: OK: Activated master IP address. slave02: This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. slave02: OK: Applying all logs succeeded. Slave started, replicating from slave01. slave01: Resetting slave info succeeded. Master failover to slave01(192.168.1.118:3306) completed successfully. Wed Sep 16 01:03:10 2015 - [info] Sending mail.. Unknown option: conf (3)登录slave01查看是否获取到vip地址 [root@slave01 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:d3:bb:11 brd ff:ff:ff:ff:ff:ff inet 192.168.1.118/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.99/24 brd 192.168.1.255 scope global secondary eth0:1 #表明已经获取 inet6 fe80::20c:29ff:fed3:bb11/64 scope link valid_lft forever preferred_lft forever
其次,实验开始
登录slave02查看主从同步是否正常,查看是否已经转移到新的master的ip上 [root@slave02 ~]# mysql -uroot -p123456 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:' Warning: Using a password on the command line interface can be insecure. Master_Host: 192.168.1.118 #表明已经转移新的ip Slave_IO_Running: Yes #表示主从ok Slave_SQL_Running: Yes
最后,实验开始
(1)在master端启动mysqld服务 [root@master ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! (2)在manager端的mha日志文件中找到主从同步的sql语句,这条语句只需要修改密码即可使用 [root@manager ~]# grep 'MASTER_HOST' /usr/local/mha/manager.log | tail -n 1 Wed Sep 16 01:03:07 2015 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='slave01 or 192.168.1.118', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000007', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='xxx'; (3)在master上启动主从同步,密码为rep123 [root@master ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='192.168.1.118', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000007', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='rep123'; start slave;" [root@master ~]# mysql -uroot -p123456 -e "show slave status\G" Warning: Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.118 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000007 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000007 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: 120 Relay_Log_Space: 457 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: 118 Master_UUID: 90c54d84-5c17-11e5-a60a-000c29d3bb11 Master_Info_File: /mydata/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0
今天就先写到这里吧!赶紧去睡觉,困死啦!
后续有时间了会继续补充mha的文章,确实是个好东西!
我已经把实验中用到的包和脚本都放在了群盘上,有需要的可以直接下载!http://pan.baidu.com/s/1c05ARHe
忘了说了我的系统版本是CentOS 6.5 64bit
分享一篇文章,请点击链接
高性能、高可用、可扩展的MySQL集群如何组件