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 12 13 14   #这里的要改成自己机子的ip
  do
    sshpass -p "$passwd" ssh-copy-id -i ~/.ssh/id_dsa.pub -p 22 -o StrictHostKeyChecking=no $UserName@$IP$i &>/dev/null
done

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

[root@ c7m01 ~]# sh ssh.sh 
请输入服务器密码:123456
[root@ c7m01 ~]# 
[root@ c7m01 ~]# ssh root@10.0.0.13
Last login: Fri Feb 14 00:14:41 2020 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-5.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('123456') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

主服务器上操作

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

vim /etc/my.cnf

[mysqld]
server-id=1
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency = 1
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 2
Server version: 5.6.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

12、查看MySQL主库的master状态

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

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000002 |      530 |              |                  | f45fe1ec-53a4-11ea-899f-000c29d39f7e:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 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:1-2 |
| gtid_mode                       | ON                                       |  #开启GTID模块
| gtid_owned                      |                                          |
| gtid_purged                     |                                          |
| simplified_binlog_gtid_recovery | OFF                                      |
+---------------------------------+------------------------------------------+
7 rows in set (0.00 sec)

c702从服务器上操作

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

vim /etc/my.cnf

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

更改完成后重启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 2
Server version: 5.6.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

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

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host='10.0.0.12',
    -> master_user='rep',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=530;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql
> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.12 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 530 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000002 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: 530 Relay_Log_Space: 519 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: 1 Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for 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: fdcedb5b-53a4-11ea-899f-000c2927e658:1-5 Auto_Position: 0 1 row in set (0.00 sec) mysql>

c703服务器

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

vim /etc/my.cnf

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

更改完成后重启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 2
Server version: 5.6.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

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

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to
    -> master_host='10.0.0.12',
    -> master_user='rep',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=530;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql
> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.12 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 530 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000002 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: 530 Relay_Log_Space: 519 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: 1 Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for 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: 0033cbb6-53a5-11ea-899f-000c298eeea7:1-4 Auto_Position: 0 1 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, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

22、安装MHA node节点

[root@ c7m01 ~]# ll
total 140
-rw-------. 1 root root  1277 2019-09-17 22:18 anaconda-ks.cfg
-rw-r--r--  1 root root 81024 2020-02-13 16:32 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root 36328 2020-02-13 16:32 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm.1
-rw-r--r--  1 root root   349 2020-02-13 16:53 ssh.sh
[root@ c7m01 ~]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-node-0.58-0.el7.centos ################################# [100%]
[root@ c7m01 ~]# 

在c703上执行

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

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

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

[root@ c703 ~]# ll
total 132
-rw-------. 1 root root  1277 2019-09-17 22:18 anaconda-ks.cfg
-rw-r--r--  1 root root 81024 2020-02-13 16:32 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root 36328 2020-02-13 16:32 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm
-rw-r--r--  1 root root   349 2020-02-13 16:37 ssh.sh
[root@ c703 ~]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-manager-0.58-0.el7.cent################################# [100%]

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=2
repl_password=123456
repl_user=rep
ssh_user=root

[server1]
hostname=10.0.0.12
port=3306

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1  #如果这个节点挂了,mha将不可用,加上这个参数,slave挂了一样可以用
no_master=1  #从不将这台主机转换为master
#candidate_master=1 #如果候选master有延迟的话,relay日志超过100m,failover切换不能成功,加上此参数后会忽略延迟日志大小。
#check_repl_delay=0 #用防止master故障时,切换时slave有延迟,卡在那里切不过来

#注意这里的配置要把注释和空格全部删除

检查ssh

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

这里放一个报错的

 

 解决办法就是在三台服务器的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:6041) is running(0:PING_OK), master:10.0.0.12

27、关闭MHA

[root@ c703 ~]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[1]+  Exit 1                  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

28、从库从新加入新生

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

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.
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 10.0.0.13
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 191
               Relay_Log_File: mysqld-relay-bin.000006
                Relay_Log_Pos: 401
        Relay_Master_Log_File: mysql-bin.000003
             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: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 191
              Relay_Log_Space: 2309
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error reconnecting to master 'rep@10.0.0.12:3306' - retry-time: 60  retries: 4
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 200221 14:48:05
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: f45fe1ec-53a4-11ea-899f-000c29d39f7e:3-4
            Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:1-6,
f45fe1ec-53a4-11ea-899f-000c29d39f7e:3-4
                Auto_Position: 0

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=2
repl_password=123456
repl_user=rep
ssh_user=root

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1
no_master=1
#candidate_master=1
#check_repl_delay=0

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

32、MHA故障还原

先在c703上查看一下

[root@c703 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
Fri Feb 14 01:25:18 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.12', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, 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=2
repl_password=123456
repl_user=rep
ssh_user=root

[server1]
hostname=10.0.0.12
port=3306

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1
no_master=1
#candidate_master=1
#check_repl_delay=0

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 2>&1 &
[1] 6041

配置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 = '1';
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 = 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" ) {

        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";
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
     return 0  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:1 10.0.0.100/24
[root@ c702 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d3:9f:7e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.13/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/24 brd 10.0.0.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed3:9f7e/64 scope link 
       valid_lft forever preferred_lft forever

38、重启MHA

[root@ c703 ~]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[1]+  Exit 1                  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
[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] 6468

模拟主库宕机vip飘移

在c702上操作

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

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

查看c7m01上是否有vip漂移

 

[root@ c7m01 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:27:e6:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.13/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/24 brd 10.0.0.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe27:e658/64 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.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 231
               Relay_Log_File: mysqld-relay-bin.000004
                Relay_Log_Pos: 401
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 231
              Relay_Log_Space: 2892
              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: 2
                  Master_UUID: fdcedb5b-53a4-11ea-899f-000c2927e658
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for 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: fdcedb5b-53a4-11ea-899f-000c2927e658:1-7
            Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:1-6,
f45fe1ec-53a4-11ea-899f-000c29d39f7e:3-4,
fdcedb5b-53a4-11ea-899f-000c2927e658:1-7
                Auto_Position: 1
[1]+  Done                    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

 

这个时候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=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1]
hostname=10.0.0.12
port=3306

[server3]
hostname=10.0.0.14
ignore_fail=1
no_master=1
port=3306

 

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

 

[root@ c703 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
Fri Feb 21 16:19:25 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.13', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, 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 &

 

 

posted @ 2020-02-13 16:50  Zsecret  阅读(1252)  评论(0编辑  收藏  举报