MySQL5.6 部署MHA

本文轉載:http://467754239.blog.51cto.com/4878013/1695175 

 

在中小型架構中,針對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的網絡拓撲圖,我們先來簡單的介紹下

wKiom1X7f7vTufm5AARxOjr4orE819.jpg

MHA有兩個重要的角色,一個是manager,另外一個是node;系統:Centos 5.5_x64

從這個拓撲圖中,或許大家不難分辨出如下信息

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)

 

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-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包

wget https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
wget https://downloads.mariadb.com/files/MHA/mha4-manager-0.54-0.el6.noarch.rpm

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

 實驗中用到的包和腳本下載:http://pan.baidu.com/s/1jGF6XwE 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章