mysql+MHA高可用 (一主双从)

时间:2023-03-10 06:25:55
mysql+MHA高可用 (一主双从)

1、准备三台服务器

10.0.0.12

10.0.0.13

10.0.0.14

2、在三台服务器上执行操作

时间同步

[root@ c7m01 ~]# echo "*/5* * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root

修改主机名(主)

[root@ c7m01 ~]# vim /etc/hostname 

c7m01

修改主机名(从)

[root@ c702 ~]# vim /etc/hostname

c702

修改主机名(从)

[root@ c703 ~]# vim /etc/hostname 

c703

修改完主机名重启机子就可以

3、三台服务器配置hosts解析(这里的主机名要和自己的一样,以便于区分)

[root@ c7m01 ~]# vim /etc/hostsEOF 

c7m01 10.0.0.12
c702 10.0.0.13
c703 10.0.0.14

4、关闭防火墙和selinux  (三台服务器执行同样的操作)

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

[root@ c7m01 ~]# sed -i ' /^SELINUX/s#enforcing#disabled#g' /etc/selinux/config

5、配置免密登录(三台服务器执行同样的操作)

[root@ c7m01 ~]# vim ssh.sh

#!/bin/bash
yum -y install sshpass &> /dev/null
read -p "请输入服务器密码:" passwd
UserName=root
IP="10.0.0."
#创建密钥
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null
#分发公钥
for i in 14 #这里的要改成自己机子的ip
do
sshpass -p "$passwd" ssh-copy-id -i ~/.ssh/id_dsa.pub -p -o StrictHostKeyChecking=no $UserName@$IP$i &>/dev/null
done

6、执行脚本配置,并连接其中一台服务器

[root@ c7m01 ~]# sh ssh.sh
请输入服务器密码:
[root@ c7m01 ~]#
[root@ c7m01 ~]# ssh root@10.0.0.13
Last login: Fri Feb :: from 10.0.0.1
[root@ c702 ~]# exit
logout
Connection to 10.0.0.13 closed.

7、mysql安装yum repo(三台服务器执行同样的操作)

[root@ c7m01 ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[root@ c7m01 ~]# rpm -ivh mysql-community-release-el7-.noarch.rpm

[root@ c7m01 ~]# yum -y install mysql-server

8、启动MySQL,三台服务器执行同样的操作

[root@ c7m01 ~]# systemctl restart mysql

9、修改MySQL密码,三台服务器执行同样的操作

mysql> update mysql.user set password=password('') where user='root' and host='localhost';
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> flush privileges;
Query OK, rows affected (0.00 sec) mysql>

主服务器上操作

10、在c7m01服务器上更改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge =
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency =
log_slave_updates = 1 更改完成后重启mysql
systemctl restart mysql

11、创建同步用户

[root@ c7m01 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '';
Query OK, rows affected (0.01 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec)

12、查看MySQL主库的master状态

mysql> show master status\G
*************************** . row ***************************
File: mysql-bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: f45fe1ec-53a4-11ea-899f-000c29d39f7e:-
row in set (0.00 sec) mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin. | | | | f45fe1ec-53a4-11ea-899f-000c29d39f7e:- |
+------------------+----------+--------------+------------------+------------------------------------------+
row in set (0.00 sec)

13、查看GTID状态

mysql> show global variables like '%gtid%';
+---------------------------------+------------------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery | OFF |
| enforce_gtid_consistency | ON | #执行GTID模块
| gtid_executed | f45fe1ec-53a4-11ea-899f-000c29d39f7e:- |
| gtid_mode | ON | #开启GTID模块
| gtid_owned | |
| gtid_purged | |
| simplified_binlog_gtid_recovery | OFF |
+---------------------------------+------------------------------------------+
rows in set (0.00 sec)

c702从服务器上操作

14、在c702服务器上更改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge =
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency =
log_slave_updates = 更改完成后重启mysql
systemctl restart mysql

15、创建同步用户

[root@ c702 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '';
Query OK, rows affected (0.00 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec)

16、关闭复制功能,配置指向master,开启服务器复制状态,检查复制状态,出现俩个yes状态表示成功

mysql> stop slave;
Query OK, rows affected, warning (0.00 sec) mysql> change master to
-> master_host='10.0.0.12',
-> master_user='rep',
-> master_password='',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=;
Query OK, rows affected, warnings (0.06 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec) mysql> start slave;
Query OK, rows affected (0.01 sec)
mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.12
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysqld-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: fdcedb5b-53a4-11ea-899f-000c2927e658:-
Auto_Position:
row in set (0.00 sec) mysql>

c703服务器

17、在c702服务器上更改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge =
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency =
log_slave_updates = 更改完成后重启mysql
systemctl restart mysql

18、创建同步用户

[root@ c703 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '';
Query OK, rows affected (0.00 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec)

19、关闭复制功能,配置指向master,开启服务器复制状态,检查复制状态,出现俩个yes状态表示成功

mysql> stop slave;
Query OK, rows affected (0.00 sec) mysql> change master to
-> master_host='10.0.0.12',
-> master_user='rep',
-> master_password='',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=;
Query OK, rows affected, warnings (0.00 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec) mysql> start slave;
Query OK, rows affected (0.00 sec)
mysql> show slave status \G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.12
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysqld-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:-
Auto_Position:
row in set (0.00 sec)

20、在三台服务器装MHA的依赖

yum -y install perl-dbd-mysql

yum -y install per-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForKManager perl-Time-HiRes

21、授权MHA管理用户(三台服务器执行同样的操作)

mysql> grant all privileges on *.* to mha@'10.0.0.%' identified by 'mha';
Query OK, rows affected (0.00 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec)

22、安装MHA node节点

[root@ c7m01 ~]# ll
total
-rw-------. root root -- : anaconda-ks.cfg
-rw-r--r-- root root -- : mha4mysql-manager-0.58-.el7.centos.noarch.rpm
-rw-r--r-- root root -- : mha4mysql-node-0.58-.el7.centos.noarch.rpm
-rw-r--r-- root root -- : mysql-community-release-el7-.noarch.rpm
-rw-r--r-- root root -- : mysql-community-release-el7-.noarch.rpm.
-rw-r--r-- root root -- : ssh.sh
[root@ c7m01 ~]# rpm -ivh mha4mysql-node-0.58-.el7.centos.noarch.rpm
Preparing... ################################# [%]
Updating / installing...
:mha4mysql-node-0.58-.el7.centos ################################# [%]
[root@ c7m01 ~]#

在c703上执行

如果安装到错误的机器上可以撤销掉,否则后期vip漂移会出错

[root@ c7m01 ~]# rpm -qa |grep mha4mysql-manager-0.58-.el7.centos.noarch.rpm
[root@ c7m01 ~]# rpm -e mha4mysql-manager

23、安装管理MHA的管理节点

[root@ c703 ~]# ll
total
-rw-------. root root -- : anaconda-ks.cfg
-rw-r--r-- root root -- : mha4mysql-manager-0.58-.el7.centos.noarch.rpm
-rw-r--r-- root root -- : mha4mysql-node-0.58-.el7.centos.noarch.rpm
-rw-r--r-- root root -- : mysql-community-release-el7-.noarch.rpm
-rw-r--r-- root root -- : ssh.sh
[root@ c703 ~]# rpm -ivh mha4mysql-manager-0.58-.el7.centos.noarch.rpm
Preparing... ################################# [%]
Updating / installing...
:mha4mysql-manager-0.58-.el7.cent################################# [%]

24、配置MHA

[root@ c703 ~]# mkdir -p /etc/mha
[root@ c703 ~]# mkdir -p /var/log/mha/app1
[root@ c703 ~]# vim /etc/mha/app1.cnf [server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql #binlog的目录,如果说miysql的环境不一样,binlog位置不同,每台服务器的binlog的位置写在server标签里面即可
user=mha
password=mha
ping_interval=
repl_password=
repl_user=rep
ssh_user=root [server1]
hostname=10.0.0.12
port= [server2]
hostname=10.0.0.13
port= [server3]
hostname=10.0.0.14
port=
ignore_fail= #如果这个节点挂了,mha将不可用,加上这个参数,slave挂了一样可以用
no_master= #从不将这台主机转换为master
#candidate_master= #如果候选master有延迟的话,relay日志超过100m,failover切换不能成功,加上此参数后会忽略延迟日志大小。
#check_repl_delay= #用防止master故障时,切换时slave有延迟,卡在那里切不过来 #注意这里的配置要把注释和空格全部删除

检查ssh

[root@ c703 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Fri Feb :: - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Feb :: - [info] Reading application default configuration from /etc/mha/app1.cnf..
Fri Feb :: - [info] Reading server configuration from /etc/mha/app1.cnf..
Fri Feb :: - [info] Starting SSH connection tests..
Fri Feb :: - [debug]
Fri Feb :: - [debug] Connecting via SSH from root@10.0.0.12(10.0.0.12:) to root@10.0.0.13(10.0.0.13:)..
Fri Feb :: - [debug] ok.
Fri Feb :: - [debug] Connecting via SSH from root@10.0.0.12(10.0.0.12:) to root@10.0.0.14(10.0.0.14:)..
Fri Feb :: - [debug] ok.
Fri Feb :: - [debug]
Fri Feb :: - [debug] Connecting via SSH from root@10.0.0.13(10.0.0.13:) to root@10.0.0.12(10.0.0.12:)..
Fri Feb :: - [debug] ok.
Fri Feb :: - [debug] Connecting via SSH from root@10.0.0.13(10.0.0.13:) to root@10.0.0.14(10.0.0.14:)..
Fri Feb :: - [debug] ok.
Fri Feb :: - [debug]
Fri Feb :: - [debug] Connecting via SSH from root@10.0.0.14(10.0.0.14:) to root@10.0.0.12(10.0.0.12:)..
Fri Feb :: - [debug] ok.
Fri Feb :: - [debug] Connecting via SSH from root@10.0.0.14(10.0.0.14:) to root@10.0.0.13(10.0.0.13:)..
Fri Feb :: - [debug] ok.
Fri Feb :: - [info] All SSH connection tests passed successfully.
[root@ c703 ~]#

这里放一个报错的

mysql+MHA高可用 (一主双从)

解决办法就是在三台服务器的mysql配置文件加一个跳过域名解析

skip-name-resolve

[root@ c703 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf

25、启动MHA

[root@ c703 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
[1] 6041
[root@ c703 ~]# ps -ef |grep mha
root 6041 4852 0 14:37 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root 6066 4852 0 14:38 pts/0 00:00:00 grep --color=auto mha

26、查看MHA状态

[root@ c703 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:) is running(:PING_OK), master:10.0.0.12

27、关闭MHA

[root@ c703 ~]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[]+ Exit nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log >&

28、从库从新加入新生

[root@ c703 ~]# grep -i "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -

29、测试MHA故障转移

停掉c7m01上的mysql 10.0.0.12

[root@ c7m01 ~]# systemctl stop mysql

30、在c703上查看slave的状态,发现master_host变成10.0.0.13

[root@ c703 ~]# mysql -uroot -p123456 -e 'show slave status\G';
Warning: Using a password on the command line interface can be insecure.
*************************** . row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 10.0.0.13
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysqld-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Connecting
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error: error reconnecting to master 'rep@10.0.0.12:3306' - retry-time: retries:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp: ::
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: f45fe1ec-53a4-11ea-899f-000c29d39f7e:-
Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:-,
f45fe1ec-53a4-11ea-899f-000c29d39f7e:-
Auto_Position:

31、查看c703的配置文件

[root@ c703 ~]# cat /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
user=mha
password=mha
ping_interval=
repl_password=
repl_user=rep
ssh_user=root [server2]
hostname=10.0.0.13
port= [server3]
hostname=10.0.0.14
port=
ignore_fail=
no_master=
#candidate_master=
#check_repl_delay=

当c7m01上的主库荡机之后,MHA会自动检查,发现主库mysql停机,立刻会把从库上提升为主库,然后另一台服务器会把mysql主从复制的master_host改为新提升的主库

32、MHA故障还原

先在c703上查看一下

[root@c703 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -
Fri Feb :: - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.12', MASTER_PORT=, MASTER_AUTO_POSITION=, MASTER_USER='rep', MASTER_PASSWORD='xxx';

33、在c7m01上执行

[root@c7m01 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='10.0.0.13',MASTER_PORT=3306, MASTER_AUTO_POSITION=1,MASTER_USER='rep',MASTER_PASSWORD='123456';"
[root@c7m01 ~]# mysql -uroot -p123456 -e "start slave;"

[root@c7m01 ~]# mysql -uroot -p123456 -e "show slave status \G"

这个时候的MHA是一次性的,当12上的mysql荡掉之后,就会把13的mysql变成主库,当再次荡机之后,不会在发生改变

在c703上执行

34、修复mysql,将server1标签加入到app1的配置文件中

[root@ c703 ~]# cat /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
user=mha
password=mha
ping_interval=
repl_password=
repl_user=rep
ssh_user=root [server1]
hostname=10.0.0.12
port= [server2]
hostname=10.0.0.13
port= [server3]
hostname=10.0.0.14
port=
ignore_fail=
no_master=
#candidate_master=
#check_repl_delay=

34、重新启动MHA

[root@ c703 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log >& &
[]

配置vip漂移,有俩种方式

第一种是keepalived的方式,管理虚拟ip的漂移

第二种是MHA自带脚本的方式,管理虚拟ip的漂移(这个漂移就是那个服务器的mysql库提升为主库,就漂移到那个上面,要根据binlog的最新slave方式提升)

35、MHA脚本方式

修改app1配置文件,添加

[root@ c703 ~]# vim /etc/mha/app1.cnf 

[server default]
master_ip_failover_script=/usr/bin/master_ip_failover #添加这一行

编写脚本

[root@ c703 ~]# vim /usr/bin/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 = '10.0.0.100/24'; #这里的vip地址写一个与自己IP地址相同的IP段
my $key = '';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #这里的网卡要看自己外网的外卡是不是ens33,不是的话要改成自己的外网网卡
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_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" ) { my $exit_code = ;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = ;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) { my $exit_code = ;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = ;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit ;
}
else {
&usage();
exit ;
}
} sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
return unless ($ssh_user);
`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";
}

36、给脚本加权限

[root@ c703 ~]# chmod +x /usr/bin/master_ip_failover

37、手动添加vip,绑定到主库的机子上,我这里是10.0.0.13

[root@ c702 ~]# ifconfig ens33: 10.0.0.100/
[root@ c702 ~]# ip a show ens33
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::d3:9f:7e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.13/ brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/ brd 10.0.0.255 scope global secondary ens33:
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed3:9f7e/ scope link
valid_lft forever preferred_lft forever

38、重启MHA

[root@ c703 ~]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[]+ Exit nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log >&
[root@ c703 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log >& &
[]

模拟主库宕机vip飘移

在c702上操作

[root@ c702 ~]# systemctl stop mysql
[root@ c702 ~]# ip a show ens33
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::d3:9f:7e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.12/ brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed3:9f7e/ scope link
valid_lft forever preferred_lft forever

这个时候vip已经漂移到另一台服务器上,将另一台服务器做为主库

查看c7m01上是否有vip漂移

[root@ c7m01 ~]# ip a show ens33
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::e6: brd ff:ff:ff:ff:ff:ff
inet 10.0.0.13/ brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/ brd 10.0.0.255 scope global secondary ens33:
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe27:e658/ scope link
valid_lft forever preferred_lft forever

当vip漂移到另一台服务器上表示成功,然后查看MHA的服务器上是否改变到另一台服务器上的master_host

[root@ c703 ~]# mysql -uroot -p123456 -e 'show slave status\G';
Warning: Using a password on the command line interface can be insecure.
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.12
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysqld-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: fdcedb5b-53a4-11ea-899f-000c2927e658
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: fdcedb5b-53a4-11ea-899f-000c2927e658:-
Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:-,
f45fe1ec-53a4-11ea-899f-000c29d39f7e:-,
fdcedb5b-53a4-11ea-899f-000c2927e658:-
Auto_Position:
[]+ Done nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log >&

这个时候MHA上app1配置文件里server2已经被诶剔除了

[root@ c703 ~]# cat /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/bin/master_ip_failover
password=mha
ping_interval=
repl_password=
repl_user=rep
ssh_user=root
user=mha [server1]
hostname=10.0.0.12
port= [server3]
hostname=10.0.0.14
ignore_fail=
no_master=
port=

这个时候开始修复,手动操作,将他恢复到之前的状态

[root@ c703 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -
Fri Feb :: - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.13', MASTER_PORT=, MASTER_AUTO_POSITION=, MASTER_USER='rep', MASTER_PASSWORD='xxx';
[root@c702 ~]#systemctl restart mysql
[root@c702 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='10.0.0.12',MASTER_PORT=3306, MASTER_AUTO_POSITION=1,MASTER_USER='rep',MASTER_PASSWORD='123456';"
[root@c702 ~]# mysql -uroot -p123456 -e "start slave;"

[root@c702 ~]# mysql -uroot -p123456 -e "show slave status \G"

#重启MHA
 nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &