MHA脚本管理方式

(1)获取管理脚本master_ip_failover

提示:yum安装的manager是没有这个脚本的。 
我们需要从manager的源码包里复制一个。

 
    1. [root@mysql-db03 ~]# ll mha4mysql-manager-0.56.tar.gz
    2. -rw-r--r--. 1 root root 113914 Sep 4 18:43 mha4mysql-manager-0.56.tar.gz
    3. [root@mysql-db03 ~]# tar xf mha4mysql-manager-0.56.tar.gz
    4. [root@mysql-db03 ~]# cd mha4mysql-manager-0.56
    5. [root@mysql-db03 mha4mysql-manager-0.56]# cd samples/scripts/
    6. [root@mysql-db03 scripts]# ll
    7. total 32
    8. -rwxr-xr-x. 1 4984 users 3648 Mar 31 2014 master_ip_failover #这就是管理虚拟IP的脚本
    9. -rwxr-xr-x. 1 4984 users 9870 Mar 31 2014 master_ip_online_change
    10. -rwxr-xr-x. 1 4984 users 11867 Mar 31 2014 power_manager
    11. -rwxr-xr-x. 1 4984 users 1360 Mar 31 2014 send_report
    12. [root@mysql-db03 scripts]# cp master_ip_failover /usr/local/bin/ #复制到/usr/local/bin目录下
    13. [root@mysql-db03 scripts]# which master_ip_failover
    14. /usr/local/bin/master_ip_failover

(2)修改mha配置文件

 
  1. [root@mysql-db03 scripts]# vim /etc/mha/mha1.cnf
  2. [root@mysql-db03 scripts]# cat /etc/mha/mha1.cnf
  3. [server default]
  4. manager_log=/var/log/mha/mha1/manager
  5. manager_workdir=/var/log/mha/mha1
  6. master_binlog_dir=/usr/local/mysql/data
  7. master_ip_failover_script=/usr/local/bin/master_ip_failover #添加脚本位置
  8. password=666666
  9. ping_interval=2
  10. repl_password=666666
  11. repl_user=rep
  12. ssh_user=root
  13. user=mha
  14. #candidate_master=1 #留着备用的注释项
  15. #check_repl_delay=0 #留着备用的注释项
  16. [server1]
  17. hostname=192.168.200.20
  18. port=3306
  19. [server2]
  20. hostname=192.168.200.28
  21. port=3306
  22. [server3]
  23. hostname=192.168.200.29
  24. port=3306

(3)修改源程序自带VIP漂移脚本


#在下边的脚本里添加如下4行代码: 
[root@mysql-db03 scripts]# sed -n '34,37p' /usr/local/bin/master_ip_failover 
my $vip = '192.168.0.60/24'; #定义VIP my $key = '0'; #定义网卡后缀数值,如果eth0就是0,eth1就是1 
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip"; #绑定VIP的命令 my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down"; #关闭VIP的命令 

(4)重启动mha管理端

 
  1. [root@mysql-db03 ~]# ps -ef | grep mha | grep -v grep #查看mha进程
  2. root 14138 13211 0 19:22 pts/3 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
  3. [root@mysql-db03 ~]# pkill perl #杀掉perl进程
  4. [1]+ Exit 1 nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1
  5. [root@mysql-db03 ~]# ps -ef | grep mha | grep -v grep #查看mha进程
  6. [root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > #启动mha进程 /var/log/mha/mha1/manager.log 2>&1 &

(1)查看mysql-db01网络状态 
image_1ct49rr4n1u4u10ekivciov7532m.png-56.2kB 
(2)停掉mysql-db01的MySQL数据库服务

  1. [root@localhost ~]# /etc/init.d/mysqld stop
  2. Shutting down MySQL....... SUCCESS!
  3. [root@localhost ~]# ifconfig #VIP没了
  4. eth0 Link encap:Ethernet HWaddr 00:0C:29:E0:9C:4A
  5. inet addr:192.168.200.20 Bcast:192.168.200.255 Mask:255.255.255.0
  6. inet6 addr: fe80::20c:29ff:fee0:9c4a/64 Scope:Link
  7. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  8. RX packets:249568 errors:0 dropped:0 overruns:0 frame:0
  9. TX packets:22184 errors:0 dropped:0 overruns:0 carrier:0
  10. collisions:0 txqueuelen:1000
  11. RX bytes:337973697 (322.3 MiB) TX bytes:3172031 (3.0 MiB)
  12. lo Link encap:Local Loopback
  13. inet addr:127.0.0.1 Mask:255.0.0.0
  14. inet6 addr: ::1/128 Scope:Host
  15. UP LOOPBACK RUNNING MTU:16436 Metric:1
  16. RX packets:66 errors:0 dropped:0 overruns:0 frame:0
  17. TX packets:66 errors:0 dropped:0 overruns:0 carrier:0
  18. collisions:0 txqueuelen:0
  19. RX bytes:10106 (9.8 KiB) TX bytes:10106 (9.8 KiB)

(3)查看mysql-db02 
image_1ct4a0vr519181v0u1v3qp1l65a33.png-57.7kB

(4)查看mysql-db03的主从同步情况 
image_1ct4a454g1mbcag113hup508v23g.png-48kB
(5)mysql-db01故障恢复

  1. [root@localhost ~]# /etc/init.d/mysqld start
  2. Starting MySQL........ SUCCESS!
  3. [root@localhost ~]# mysql -uroot -p666666
  4. Warning: Using a password on the command line interface can be insecure.
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 1
  7. Server version: 5.6.17-log MySQL Community Server (GPL)
  8. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> CHANGE MASTER TO MASTER_HOST='192.168.200.28', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='666666';
  14. Query OK, 0 rows affected, 2 warnings (0.11 sec)
  15. mysql> start slave;
  16. Query OK, 0 rows affected, 1 warning (0.13 sec)
  17. mysql> show slave status\G
  18. *************************** 1. row ***************************
  19. Slave_IO_State: Waiting for master to send event
  20. Master_Host: 192.168.200.28
  21. Master_User: rep
  22. Master_Port: 3306
  23. Connect_Retry: 60
  24. Master_Log_File: mysql-bin.000006
  25. Read_Master_Log_Pos: 231
  26. Relay_Log_File: localhost-relay-bin.000002
  27. Relay_Log_Pos: 361
  28. Relay_Master_Log_File: mysql-bin.000006
  29. Slave_IO_Running: Yes
  30. Slave_SQL_Running: Yes
  31. ...一下省略...

(6)补上缺失的mha配置文件

  1. [root@localhost ~]# cat /etc/mha/mha1.cnf
  2. [server default]
  3. manager_log=/var/log/mha/mha1/manager
  4. manager_workdir=/var/log/mha/mha1
  5. master_binlog_dir=/usr/local/mysql/data
  6. master_ip_failover_script=/usr/local/bin/master_ip_failover
  7. password=666666
  8. ping_interval=2
  9. repl_password=666666
  10. repl_user=rep
  11. ssh_user=root
  12. user=mha
  13. [server1]
  14. hostname=192.168.200.20
  15. port=3306
  16. [server2]
  17. hostname=192.168.200.28
  18. port=3306
  19. [server3]
  20. hostname=192.168.200.29
  21. port=3306

(7)启动mha管理进程

  1. [root@localhost ~]# ps -ef | grep mha | grep -v grep
  2. [root@localhost ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
  3. [1] 7382
  4. [root@localhost ~]# ps -ef | grep mha | grep -v grep
  5. root 7382 5389 1 03:14 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

配置binlog-server备份服务器

主库宕机,也许会造成主库binlog复制不及时而导致数据丢失的情况出现,因此配置binlog-server进行时时同步备份,是必要的一种安全手段。

 

修改mha配置文件

 
  1. [root@localhost ~]# vim /etc/mha/mha1.cnf
  2. [root@localhost ~]# cat /etc/mha/mha1.cnf
  3. [server default]
  4. manager_log=/var/log/mha/mha1/manager
  5. manager_workdir=/var/log/mha/mha1
  6. master_binlog_dir=/usr/local/mysql/data #全局的binlog存放位置
  7. master_ip_failover_script=/usr/local/bin/master_ip_failover
  8. password=666666
  9. ping_interval=2
  10. repl_password=666666
  11. repl_user=rep
  12. ssh_user=root
  13. user=mha
  14. [server1]
  15. hostname=192.168.200.20
  16. port=3306
  17. [server2]
  18. hostname=192.168.200.28
  19. port=3306
  20. [server3]
  21. hostname=192.168.200.29
  22. port=3306
  23. [binlog1] #添加binlog模块
  24. no_master=1 #不允许切换为主
  25. hostname=192.168.200.29 #存放IP
  26. master_binlog_dir=/data/mysql/binlog/ #binlog存放位置优先级比全局的高
 

拉取主库上的binlog日志到mysql-db03的存放目录里

  1. [root@localhost ~]# mkdir -p /data/mysql/binlog #创建存放目录
  2. [root@localhost ~]# cd /data/mysql/binlog #进入存放目录
  3. [root@localhost binlog]# ll
  4. total 0
  5. [root@localhost binlog]# which mysqlbinlog
  6. /usr/local/bin/mysqlbinlog
  7. [root@localhost binlog]# mysqlbinlog -R --host=192.168.200.60 --user=mha --password=666666 --raw --stop-never mysql-bin.000001 & #拉取主库binlog,这里的IP是VIP的
  8. [2] 8133
  9. [root@localhost binlog]# Warning: Using a password on the command line interface can be insecure.
  10. [root@localhost binlog]# ll
  11. total 24
  12. -rw-r--r--. 1 root root 143 Nov 25 03:37 mysql-bin.000001
  13. -rw-r--r--. 1 root root 174 Nov 25 03:37 mysql-bin.000002
  14. -rw-r--r--. 1 root root 685 Nov 25 03:37 mysql-bin.000003
  15. -rw-r--r--. 1 root root 254 Nov 25 03:37 mysql-bin.000004
  16. -rw-r--r--. 1 root root 254 Nov 25 03:37 mysql-bin.000005
  17. -rw-r--r--. 1 root root 120 Nov 25 03:37 mysql-bin.000006
  18. [root@localhost binlog]# ps -ef | grep mysqlbinlog | grep -v grep
  19. root 8133 5389 0 03:37 pts/0 00:00:00 mysqlbinlog -R --host=192.168.200.60 --user=mha --password=x xxxx --raw --stop-never mysql-bin.000001
 

启动mha管理进程

 
  1. [root@localhost binlog]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
  2. [root@localhost binlog]# ps -ef | grep mysqlbinlog | grep -v grep
  3. root 8133 5389 0 03:37 pts/0 00:00:00 mysqlbinlog -R --host=192.168.200.60 --user=mha --password=x xxxx --raw --stop-never mysql-bin.000001 #binlog进程在
  4. [root@localhost binlog]# ps -ef | grep perl | grep -v grep
  5. root 7382 5389 0 03:14 pts/0 00:00:01 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover #mha进程在