Mysql之MHA高可用

一.MHA的介绍

1.MHA概念

(1)MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件
(2)MHA 的出现就是解决MySQL 单点的问题
(3)MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作
(4)MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用

2.MHA 的组成

(1)MHA Node(数据节点)

(2)MHA Node 运行在每台 MySQL 服务器上

(3)MHA Manager(管理节点)

(4)MHA Manager 可以单独部署在一台独立的机器上,管理多个 master-slave 集群;也可以部署在一台 slave 节点上

3.MHA 的特点

(1)自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
(2)使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
(3)目前MHA支持一主多从架构,最少三台服务,即一主两从

4.MHA 的工作原理

(1)MHA Manager 会定时探测集群中的 master 节点。当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master, 然后将所有其他的 slave 重新指向新的 master。整个故障转移过程对应用程序完全透明

二.搭建MySQL+MHA思路

1.MHA架构

(1)数据库安装
(2)一主两从
(3)MHA搭建

2.故障模拟

(1)模拟主库失效
(2)备选主库成为主库
(3)原故障主库恢复重新加入到MHA成为从库

3.MHA的部署过程

(1)所有mysql都做主从复制用户授权,和 mha-manager 访问 数据库用户的授权

(2)先做时间同步和mysq1主从复制,并设置从为只读模式

(3)所有节点安装 mha-node 组件,在manager节点再安装 mha-manager 组件

(4)所有节点做 ssh 密钥对免交互登录认证

(5)在 mha-manager 节点上准备好 故障切换 脚本 和 manager 进程配置文件

(6)在 master 节点使用 ifconfig 创建 VIP

(7)使用 masterha check ssh 和 masterha check repl 做好mha启动前的检查,再使用 masterha manager 启动 mha-manager 进程

(8)做故障切换测试:故障切换后mha-manager进程会自动退出,配置文件自动删除IFmaster的配置,VIP会自动漂移到新master节点,其它slave会自动跟新master做主从复制

3.实验前准备

MHA manager 节点服务器:CentOS7.4(64 位) manager/192.168.247.131 ,安装MHA node 和 manager 组件
Master 节点服务器:CentOS7.4(64 位) mysql1/192.168.247.132 ,安装mysql5.7、MHA node 组件
Slave1 节点服务器:CentOS7.4(64 位) mysql2/192.168.247.80 ,安装mysql5.7、MHA node 组件
Slave2 节点服务器:CentOS7.4(64 位) mysql3/192.168.247.90 ,安装mysql5.7、MHA node 组件

三.实际操作

1.Master、Slave1、Slave2 节点上安装 mysql5.7

2.修改 Master、Slave1、Slave2 节点的主机名

hostnamectl set-hostname Mysql1
hostnamectl set-hostname Mysql2
hostnamectl set-hostname Mysql3

3.修改的 Master 节点Mysql主配置文件

vim /etc/my.cnf
  [mysqld]
  server-id = 1
  log_bin = master-bin
  log-slave-updates = true
systemctl restart mysqld

4.修改的 Slave1 节点 Mysql主配置文件

vim /etc/my.cnf
  server-id = 2 						
  log_bin = master-bin
  relay-log = relay-log-bin
  relay-log-index = slave-relay-bin.index
systemctl restart mysqld

5.修改的 Slave2 节点 Mysql主配置文件

vim /etc/my.cnf
  server-id = 3 						
  log_bin = master-bin
  relay-log = relay-log-bin
  relay-log-index = slave-relay-bin.index
systemctl restart mysqld

6.在 Master节点上都创建两个软链接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@localhost ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@localhost ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

7.在 Slave1 节点上都创建两个软链接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@localhost ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@localhost ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

8.在 Slave2 节点上都创建两个软链接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@localhost ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@localhost ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

9.配置 mysql 一主两从

(1)所有数据库节点进行 mysql 授权

mysql -uroot -p
grant replication slave on *.* to 'myslave'@'192.168.247.%' identified by '903';		
#从数据库同步使用
grant all privileges on *.* to 'mha'@'192.168.247.%' identified by 'manager';		
#manager 使用

grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';				
#防止从库通过主机名连接不上主库
grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';
grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';
flush privileges;
[root@localhost ~]# mysql -uroot -pabc123
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave on *.* to 'myslave'@'192.168.247.%' identified by '604';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> grant all privileges on *.* to 'mha'@'192.168.247.%' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> grant all privileges on *.* to 'mha'@'mysql1' identified by 'manager';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> grant all privileges on *.* to 'mha'@'mysql2' identified by 'manager';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> grant all privileges on *.* to 'mha'@'mysql3' identified by 'manager';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000026 |     1747 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

(2)在 Master 节点查看二进制文件和同步点

show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     9015 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000026 |     1747 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

(3)在 Slave1、Slave2 节点执行同步操作

change master to master_host='192.168.247.132',master_user='myslave',master_password='903',master_log_file='master-bin.000001',master_log_pos=9015; 
start slave;
mysql> change master to master_host='192.168.247.132',master_user='myslave',master_password='604',master_log_file='mysql-bin.000026',master_log_pos=1747;
Query OK, 0 rows affected, 2 warnings (0.01 sec)   #若在此报错输入inset slave;

(4)在 Slave1、Slave2 节点查看数据同步结果

show slave status\G		
//确保 IO 和 SQL 线程都是 Yes,代表同步正常。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.247.132
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000026
          Read_Master_Log_Pos: 1747
               Relay_Log_File: relay-log-bin.000006
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000026
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

(5)两个从库必须设置为只读模式

set global read_only=1;
mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)

(6)插入数据测试数据库同步

  • 在 Master 主库插入条数据,测试是否同步
create database test_db;
use test_db;
create table test(id int);
insert into test(id) values (1);
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)
mysql> use test_db;
Database changed
mysql> create table test(id int);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test(id) values (1);
Query OK, 1 row affected (0.01 sec)

10.安装 MHA 软件

(1)所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源

[root@lion ~]# cd /etc/yum repos.d
[root@lion yum.repos.d]# mv repo.bak/* ./
yum install epel-release --nogpgcheck -y
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
[root@lion yum.repos.d]# yum install epel-release --nogpgcheck -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: ftp.sjtu.edu.cn
......
已安装:
  epel-release.noarch 0:7-11
完毕!
[root@lion yum.repos.d]# yum install -y perl-DBD-MySQL \
> perl-Config-Tiny \
> perl-Log-Dispatch \
> perl-Parallel-ForkManager \
> perl-ExtUtils-CBuilder \
> perl-ExtUtils-MakeMaker \
> perl-CPAN
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                     
......
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction

(2)安装 MHA 软件包,先在所有服务器上必须先安装 node 组件

  • 对于每个操作系统版本不一样,这里 CentOS7.4 必须选择 0.57 版本。
    在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。
cd /opt
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
[root@lion mha4mysql-manager-0.57]# cd /opt
[root@lion opt]# tar xf mha4mysql-node-0.57.tar.gz
[root@lion opt]# cd mha4mysql-node-0.57
[root@lion mha4mysql-node-0.57]# perl Makefile.PL
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI        ...loaded. (1.627)
- DBD::mysql ...loaded. (4.023)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::node
[root@lion mha4mysql-node-0.57]# make && make install
cp lib/MHA/BinlogManager.pm blib/lib/MHA/BinlogManager.pm
cp lib/MHA/BinlogPosFindManager.pm blib/lib/MHA/BinlogPosFindManager.pm
cp lib/MHA/BinlogPosFinderXid.pm blib/lib/MHA/BinlogPosFinderXid.pm
cp lib/MHA/BinlogHeaderParser.pm blib/lib/MHA/BinlogHeaderParser.pm
cp lib/MHA/BinlogPosFinder.pm blib/lib/MHA/BinlogPosFinder.pm
......

(3)在 MHA manager 节点上安装 manager 组件

cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
[root@lion mha4mysql-node-0.57]# cd /opt
[root@lion opt]# cd mha4mysql-manager-0.57
[root@lion mha4mysql-manager-0.57]# perl Makefile.PL
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.627)
- DBD::mysql            ...loaded. (4.023)
- Time::HiRes           ...loaded. (1.9725)
- Config::Tiny          ...loaded. (2.14)
- Log::Dispatch         ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst        ...loaded. (0.57)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::manager
[root@lion mha4mysql-manager-0.57]# make && make install
cp lib/MHA/ManagerUtil.pm blib/lib/MHA/ManagerUtil.pm
cp lib/MHA/Config.pm blib/lib/MHA/Config.pm
cp lib/MHA/HealthCheck.pm blib/lib/MHA/HealthCheck.pm
cp lib/MHA/ServerManager.pm blib/lib/MHA/ServerManager.pm
cp lib/MHA/ManagerConst.pm blib/lib/MHA/ManagerConst.pm
cp lib/MHA/ManagerAdmin.pm blib/lib/MHA/ManagerAdmin.pm
cp lib/MHA/FileStatus.pm blib/lib/MHA/FileStatus.pm
......

11.在所有服务器上配置无密码认证

(1)在 manager 节点上配置到所有数据库节点的无密码认证

ssh-keygen -t rsa 				#一路按回车键
ssh-copy-id 192.168.247.132
ssh-copy-id 192.168.247.80
ssh-copy-id 192.168.247.90
[root@lion mha4mysql-manager-0.57]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FpoxfRwbJVMWkB68YTYHK/UIJZu19MG7FukkTVk9ZuQ root@lion
The key's randomart image is:
+---[RSA 2048]----+
|        oo%B*+oo |
|       . XO&=..+.|
|      o *+BOoooE.|
|       = ++ *    |
|      o S  + o   |
|       .    +    |
|           .     |
|                 |
|                 |
+----[SHA256]-----+
[root@lion mha4mysql-manager-0.57]#
[root@lion mha4mysql-manager-0.57]# ssh-copy-id 192.168.247.132
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.132 (192.168.247.132)' can't be established.
ECDSA key fingerprint is SHA256:K+hDIc4SGCwRN1fDVeScWf3VGJ/C3lYV/PUTrOZmFZ4.
ECDSA key fingerprint is MD5:75:58:91:bb:7e:44:43:07:65:c8:2a:68:d2:39:11:54.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.132'"
and check to make sure that only the key(s) you wanted were added.
[root@lion mha4mysql-manager-0.57]# ssh-copy-id 192.168.247.80
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.80 (192.168.247.80)' can't be established.
ECDSA key fingerprint is SHA256:fGI7dm0cGxfAVhzwPSzojEa9bVLxv7gCzhZM8GPNzwY.
ECDSA key fingerprint is MD5:27:f6:d8:ea:c7:9b:45:a3:ca:4b:de:c9:6d:8d:2d:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.80'"
and check to make sure that only the key(s) you wanted were added.
[root@lion mha4mysql-manager-0.57]# ssh-copy-id 192.168.247.90
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.90 (192.168.247.90)' can't be established.
ECDSA key fingerprint is SHA256:fGI7dm0cGxfAVhzwPSzojEa9bVLxv7gCzhZM8GPNzwY.
ECDSA key fingerprint is MD5:27:f6:d8:ea:c7:9b:45:a3:ca:4b:de:c9:6d:8d:2d:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.90'"
and check to make sure that only the key(s) you wanted were added.

(2)在 mysql1 上配置到数据库节点 mysql2 和 mysql3 的无密码认证

ssh-keygen -t rsa
ssh-copy-id 192.168.247.80
ssh-copy-id 192.168.247.90
[root@localhost mha4mysql-node-0.57]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:OcnDCnvW69pEBzWxSWkPc9u0tE9ogcsNI0nxUktOGy8 root@mysql1
The key's randomart image is:
+---[RSA 2048]----+
|         .B==.   |
|         oBX==+  |
|        ..+BEX.= |
|       o + .=.B .|
|    .   S .  . o |
|     o + +      .|
|    . + o        |
|     o o .       |
|      .o+        |
+----[SHA256]-----+
[root@localhost mha4mysql-node-0.57]# ssh-copy-id 192.168.247.80
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.80 (192.168.247.80)' can't be established.
ECDSA key fingerprint is SHA256:fGI7dm0cGxfAVhzwPSzojEa9bVLxv7gCzhZM8GPNzwY.
ECDSA key fingerprint is MD5:27:f6:d8:ea:c7:9b:45:a3:ca:4b:de:c9:6d:8d:2d:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.247.80'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost mha4mysql-node-0.57]# ssh-copy-id 192.168.247.90
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.90 (192.168.247.90)' can't be established.
ECDSA key fingerprint is SHA256:fGI7dm0cGxfAVhzwPSzojEa9bVLxv7gCzhZM8GPNzwY.
ECDSA key fingerprint is MD5:27:f6:d8:ea:c7:9b:45:a3:ca:4b:de:c9:6d:8d:2d:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.247.90'"
and check to make sure that only the key(s) you wanted were added.

(3)在 mysql2 上配置到数据库节点 mysql1 和 mysql3 的无密码认证

ssh-keygen -t rsa
ssh-copy-id 192.168.247.132
ssh-copy-id 192.168.247.90
[root@localhost mha4mysql-node-0.57]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bXFsSmLsOme53FuV35BWWoQB3QYYkaPAxKVVLVDsy/U root@mysql2
The key's randomart image is:
+---[RSA 2048]----+
|       +..+=*Oo=.|
|       .+o .* +.o|
|        =.oo+o .o|
|       o +.=. .* |
|        S +. oB. |
|       . o  oo oE|
|      o +   .   o|
|       = o .     |
|        o o.     |
+----[SHA256]-----+
您在 /var/spool/mail/root 中有新邮件
[root@localhost mha4mysql-node-0.57]# ssh-copy-id 192.168.247.132
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.132 (192.168.247.132)' can't be established.
ECDSA key fingerprint is SHA256:K+hDIc4SGCwRN1fDVeScWf3VGJ/C3lYV/PUTrOZmFZ4.
ECDSA key fingerprint is MD5:75:58:91:bb:7e:44:43:07:65:c8:2a:68:d2:39:11:54.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.132'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost mha4mysql-node-0.57]# ssh-copy-id 192.168.247.90
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.90 (192.168.247.90)' can't be established.
ECDSA key fingerprint is SHA256:fGI7dm0cGxfAVhzwPSzojEa9bVLxv7gCzhZM8GPNzwY.
ECDSA key fingerprint is MD5:27:f6:d8:ea:c7:9b:45:a3:ca:4b:de:c9:6d:8d:2d:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.90'"
and check to make sure that only the key(s) you wanted were added.

(4)在 mysql3 上配置到数据库节点 mysql1 和 mysql2 的无密码认证

ssh-keygen -t rsa
ssh-copy-id 192.168.247.132
ssh-copy-id 192.168.247.80
[root@localhost mha4mysql-node-0.57]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WuESl5x+9bTtWwycnJ/ICIJb0AVrgYyUEhbTsqF2RXk root@mysql3
The key's randomart image is:
+---[RSA 2048]----+
| ==.=.oo..       |
|.+.+ =.E+o       |
|. = ..o+*   . .  |
|.o .  += . . = = |
|. .  ..oS..   O .|
|      o+... o .=.|
|     ..    . o .=|
|                o|
|               . |
+----[SHA256]-----+
您在 /var/spool/mail/root 中有新邮件
[root@localhost mha4mysql-node-0.57]# ssh-copy-id 192.168.247.132
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.132 (192.168.247.132)' can't be established.
ECDSA key fingerprint is SHA256:K+hDIc4SGCwRN1fDVeScWf3VGJ/C3lYV/PUTrOZmFZ4.
ECDSA key fingerprint is MD5:75:58:91:bb:7e:44:43:07:65:c8:2a:68:d2:39:11:54.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.132'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost mha4mysql-node-0.57]# ssh-copy-id 192.168.247.80
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.80 (192.168.247.80)' can't be established.
ECDSA key fingerprint is SHA256:fGI7dm0cGxfAVhzwPSzojEa9bVLxv7gCzhZM8GPNzwY.
ECDSA key fingerprint is MD5:27:f6:d8:ea:c7:9b:45:a3:ca:4b:de:c9:6d:8d:2d:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.247.80'"
and check to make sure that only the key(s) you wanted were added.

12.在 manager 节点上配置 MHA

(1)在 manager 节点上复制相关脚本到/usr/local/bin 目录

cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
[root@lion mha4mysql-manager-0.57]# cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin

(2)复制上述的自动切换时 VIP 管理的脚本到 /usr/local/bin 目录,这里使用master_ip_failover脚本来管理 VIP 和故障切换

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@lion mha4mysql-manager-0.57]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

(3)修改内容如下:(删除原有内容,直接复制并修改vip相关参数。可在拷贝前输入 :set paste 解决vim粘贴乱序问题)

vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
 
use Getopt::Long;
 
my (
    $command, $orig_master_host, $orig_master_ip,$ssh_user,
    $orig_master_port, $new_master_host, $new_master_ip,$new_master_port,
    $orig_master_ssh_port,$new_master_ssh_port,$new_master_user,$new_master_password
);
 
# 这里定义的虚拟IP配置要注意,这个ip必须要与你自己的集群在同一个网段,否则无效
my $vip = '192.168.247.200/24';
my $key = '1';
# 这里的网卡名称 “ens32” 需要根据你机器的网卡名称进行修改
# 如果多台机器直接的网卡名称不统一,有两种方式,一个是改脚本,二是把网卡名称修改成统一
# 我这边实际情况是修改成统一的网卡名称
my $ssh_start_vip = "sudo /sbin/ifconfig ens32:$key $vip";
my $ssh_stop_vip = "sudo /sbin/ifconfig ens32:$key down";
my $ssh_Bcast_arp= "sudo /sbin/arping -I ens32 -c 3 -A $vip";

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,
    'orig_master_ssh_port=i' => \$orig_master_ssh_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
    'new_master_ssh_port' => \$new_master_ssh_port,
    'new_master_user' => \$new_master_user,
    'new_master_password' => \$new_master_password
 
);
 
exit &main();
 
sub main {
    $ssh_user = defined $ssh_user ? $ssh_user : 'root';
    print "\n\nIN SCRIPT TEST====$ssh_user|$ssh_stop_vip==$ssh_user|$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();
        &start_arp();
            $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() {
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
 
sub start_arp() {
    `ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
}
sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --ssh_user=user --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";
}

(4)创建 MHA 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器

mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
vim /etc/masterha/app1.cnf	#删除原有内容,直接复制并修改节点服务器的IP地址
  [server default]
  manager_log=/var/log/masterha/app1/manager.log
  manager_workdir=/var/log/masterha/app1
  master_binlog_dir=/usr/local/mysql/data
  master_ip_failover_script=/usr/local/bin/master_ip_failover
  master_ip_online_change_script=/usr/local/bin/master_ip_online_change
  password=manager
  ping_interval=1
  remote_workdir=/tmp
  repl_password=903
  repl_user=myslave
  secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.247.80 -s   192.168.247.90
  shutdown_script=""
  ssh_user=root
  user=mha
  [server1]
  hostname=192.168.247.132
  port=3306
  [server2]
  candidate_master=1
  check_repl_delay=0
  hostname=192.168.247.80
  port=3306
  [server3]
  hostname=192.168.247.90
  port=3306
[root@lion mha4mysql-manager-0.57]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@lion mha4mysql-manager-0.57]# vim /usr/local/bin/master_ip_failover
 [server default]
  manager_log=/var/log/masterha/app1/manager.log
  manager_workdir=/var/log/masterha/app1
  master_binlog_dir=/usr/local/mysql/data
  master_ip_failover_script=/usr/local/bin/master_ip_failover
  master_ip_online_change_script=/usr/local/bin/master_ip_online_change
  password=manager
  ping_interval=1
  remote_workdir=/tmp
  repl_password=903
  repl_user=myslave
  secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.247.80 -s   192.168.247.90
  shutdown_script=""
  ssh_user=root
  user=mha
  [server1]
  hostname=192.168.247.132
  port=3306
  [server2]
  candidate_master=1
  check_repl_delay=0
  hostname=192.168.247.80
  port=3306
  [server3]
  hostname=192.168.247.90
  port=3306

13.第一次配置需要在 Master 节点上手动开启虚拟IP

/sbin/ifconfig ens32:1 192.168.247.200/24
[root@localhost mha4mysql-node-0.57]# /sbin/ifconfig ens32:1 192.168.247.200/24

14.在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully

masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:09:45 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Nov 26 23:09:45 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Nov 26 23:09:45 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Nov 26 23:09:45 2020 - [info] Starting SSH connection tests..
Tue Nov 26 23:09:46 2020 - [debug] 
Tue Nov 26 23:09:45 2020 - [debug]  Connecting via SSH from [email protected](192.168.247.80:22) to [email protected](192.168.247.90:22)..
Tue Nov 26 23:09:46 2020 - [debug]   ok.
Tue Nov 26 23:09:47 2020 - [debug] 
Tue Nov 26 23:09:46 2020 - [debug]  Connecting via SSH from [email protected](192.168.247.90:22) to [email protected](192.168.247.80:22)..
Tue Nov 26 23:09:47 2020 - [debug]   ok.
Tue Nov 26 23:09:47 2020 - [info] All SSH connection tests passed successfully.
[root@lion mha4mysql-manager-0.57]# masterha_check_ssh -conf=/opt/mysql-mha/mysql_mha.cnf
Tue Jun 27 16:49:07 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jun 27 16:49:07 2023 - [info] Reading application default configuration from /opt/mysql-mha/mysql_mha.cnf..
Tue Jun 27 16:49:07 2023 - [info] Reading server configuration from /opt/mysql-mha/mysql_mha.cnf..
Tue Jun 27 16:49:07 2023 - [info] Starting SSH connection tests..
Tue Jun 27 16:49:09 2023 - [debug]
Tue Jun 27 16:49:07 2023 - [debug]  Connecting via SSH from [email protected](192.168.247.80:22) to [email protected](192.168.247.132:22)..
Tue Jun 27 16:49:08 2023 - [debug]   ok.
Tue Jun 27 16:49:08 2023 - [debug]  Connecting via SSH from [email protected](192.168.247.80:22) to [email protected](192.168.247.90:22)..
Tue Jun 27 16:49:09 2023 - [debug]   ok.
Tue Jun 27 16:49:10 2023 - [debug]
Tue Jun 27 16:49:08 2023 - [debug]  Connecting via SSH from [email protected](192.168.247.90:22) to [email protected](192.168.247.132:22)..
Tue Jun 27 16:49:09 2023 - [debug]   ok.
Tue Jun 27 16:49:09 2023 - [debug]  Connecting via SSH from [email protected](192.168.247.90:22) to [email protected](192.168.247.80:22)..
Tue Jun 27 16:49:10 2023 - [debug]   ok.
Tue Jun 27 16:49:13 2023 - [debug]
Tue Jun 27 16:49:07 2023 - [debug]  Connecting via SSH from [email protected](192.168.247.132:22) to [email protected](192.168.247.80:22)..
Tue Jun 27 16:49:13 2023 - [debug]   ok.
Tue Jun 27 16:49:13 2023 - [debug]  Connecting via SSH from [email protected](192.168.247.132:22) to [email protected](192.168.247.90:22)..
Tue Jun 27 16:49:13 2023 - [debug]   ok.
Tue Jun 27 16:49:13 2023 - [info] All SSH connection tests passed successfully.

15.在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常

masterha_check_repl -conf=/etc/masterha/app1.cnf

Tue Nov 26 23:132:29 2020 - [info] Slaves settings check done.
Tue Nov 26 23:132:29 2020 - [info] 
192.168.247.80(192.168.247.80:3306) (current master)
 +--192.168.247.90(192.168.247.90:3306)
Tue Nov 26 23:132:29 2020 - [info] Checking replication health on 192.168.247.90..
Tue Nov 26 23:132:29 2020 - [info]  ok.
Tue Nov 26 23:132:29 2020 - [info] Checking master_ip_failover_script status:
Tue Nov 26 23:132:29 2020 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.247.80 --orig_master_ip=192.168.247.80 --orig_master_port=3306 
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.247.200===
Checking the Status of the script.. OK 
Tue Nov 26 23:132:29 2020 - [info]  OK.
Tue Nov 26 23:132:29 2020 - [warning] shutdown_script is not defined.
Tue Nov 26 23:132:29 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
[root@lion mha4mysql-manager-0.57]# masterha_check_repl -conf=/opt/mysql-mha/mysql_mha.cnf
Tue Jun 27 16:59:24 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jun 27 16:59:25 2023 - [info]   192.168.247.132(192.168.247.132:3306)
Tue Jun 27 16:59:25 2023 - [info]   192.168.247.80(192.168.247.80:3306)
Tue Jun 27 16:59:25 2023 - [info]   192.168.247.90(192.168.247.90:3306)
Tue Jun 27 16:59:25 2023 - [info] Alive Slaves:
Tue Jun 27 16:59:25 2023 - [info]   192.168.247.80(192.168.247.80:3306)  Version=5.7.41-
Tue Jun 27 16:59:30 2023 - [info]   Connecting to [email protected](192.168.247.132:22)..
  Creating /opt/mysql-mha/mha-node if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
Tue Jun 27 16:59:32 2023 - [warning] shutdown_script is not defined.
Tue Jun 27 16:59:32 2023 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.

16.在 manager 节点上启动 MHA

nohup masterha_manager \
--conf=/etc/masterha/app1.cnf \
--remove_dead_master_conf \
--ignore_last_failover < /dev/null > /var/log/manager.log 2>&1 &
[root@lion mha4mysql-manager-0.57]# nohup masterha_manager \
> --conf=/opt/mysql-mha/mysql_mha.cnf \
> --remove_dead_master_conf \
> --ignore_last_failover < /dev/null > /var/log/mha_manager.log 2>&1 &
[1] 28378

17.查看 MHA 状态,可以看到当前的 master 是 Mysql1 节点

masterha_check_status --conf=/etc/masterha/app1.cnf
[root@lion mha4mysql-manager-0.57]# masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf
mysql_mha (pid:28378) is running(0:PING_OK), master:192.168.247.132
[root@lion mha4mysql-manager-0.57]# cat /opt/mysql-mha/manager.log | grep "current master"
Tue Jun 27 17:00:07 2023 - [info] Checking SSH publickey authentication settings on the current master..
192.168.247.132(192.168.247.132:3306) (current master)

18.查看 MHA 日志,也以看到当前的 master 是 192.168.247.132

cat /var/log/masterha/app1/manager.log | grep "current master"
[root@lion mha4mysql-manager-0.57]# cat /opt/mysql-mha/manager.log | grep "current master"
Tue Jun 27 17:00:07 2023 - [info] Checking SSH publickey authentication settings on the current master..
192.168.247.132(192.168.247.132:3306) (current master)

19.查看 Mysql1 的 VIP 地址

(1)192.168.247.200 是否存在,这个 VIP 地址不会因为 manager 节点停止 MHA 服务而消失

[root@localhost mha4mysql-node-0.57]# ifconfig
ens32: flags=4163  mtu 1500
        inet 192.168.247.132  netmask 255.255.255.0  broadcast 192.168.247.255
        inet6 fe80::b7de:ddd:163c:8ed6  prefixlen 64  scopeid 0x20
        ether 00:0c:29:d4:a5:c4  txqueuelen 1000  (Ethernet)
        RX packets 45113  bytes 39544881 (37.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25490  bytes 6376311 (6.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ens32:1: flags=4163  mtu 1500
        inet 192.168.247.200  netmask 255.255.255.0  broadcast 192.168.247.255
        ether 00:0c:29:d4:a5:c4  txqueuelen 1000  (Ethernet)
ens34: flags=4163  mtu 1500
        ether 00:0c:29:d4:a5:ce  txqueuelen 1000  (Ethernet)
        RX packets 3773  bytes 233724 (228.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1006  bytes 177204 (173.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 48  bytes 5616 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 5616 (5.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:88:76:06  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

四.故障模拟

1.在 manager 节点服务器

(1)监控观察日志记录

tail -f /var/log/masterha/app1/manager.log
[root@lion mha4mysql-manager-0.57]# tail -f /opt/mysql-mha/manager.log
IN SCRIPT TEST====root|sudo /sbin/ifconfig ens32:1 down==root|sudo /sbin/ifconfig ens32:1 192.168.247.200/24===
Checking the Status of the script.. OK
Tue Jun 27 17:00:09 2023 - [info]  OK.
Tue Jun 27 17:00:09 2023 - [warning] shutdown_script is not defined.
Tue Jun 27 17:00:09 2023 - [info] Set master ping interval 1 seconds.
Tue Jun 27 17:00:09 2023 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 192.168.247.80 -s 192.168.247.90
Tue Jun 27 17:00:09 2023 - [info] Starting ping health check on 192.168.247.132(192.168.247.132:3306)..
Tue Jun 27 17:00:09 2023 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
mysql_mha: MySQL Master failover 192.168.247.80(192.168.247.80:3306) to 192.168.247.132(192.168.247.132:3306) succeeded
 # 成功后显示页面
Master 192.168.247.80(192.168.247.80:3306) is down!
Check MHA Manager logs at lion:/opt/mysql-mha/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.247.80(192.168.247.80:3306)
The latest slave 192.168.247.132(192.168.247.132:3306) has all relay logs for recovery.
Selected 192.168.247.132(192.168.247.132:3306) as a new master.
192.168.247.132(192.168.247.132:3306): OK: Applying all logs succeeded.
192.168.247.132(192.168.247.132:3306): OK: Activated master IP address.
192.168.247.90(192.168.247.90:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.247.90(192.168.247.90:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.247.132(192.168.247.132:3306)
192.168.247.132(192.168.247.132:3306): Resetting slave info succeeded.
Master failover to 192.168.247.132(192.168.247.132:3306) completed successfully.

2.在 Master 节点服务器

(1)在 Mysql1 上停止 mysql 服务

systemctl stop mysqld
或
pkill -9 mysql
[root@lion mha4mysql-manager-0.57]# systemctl stop mysqld.service

3.正常自动切换一次后,MHA 进程会退出。HMA 会自动修改 app1.cnf 文件内容,将宕机的 mysql1 节点删除。查看 mysql2 是否接管 VIP

[root@localhost mha4mysql-node-0.57]# ifconfig
ens32: flags=4163  mtu 1500
        inet 192.168.247.80  netmask 255.255.255.0  broadcast 192.168.247.255
        inet6 fe80::e5a6:8276:ca32:6add  prefixlen 64  scopeid 0x20
        inet6 fe80::b373:8a76:7b0b:f8ee  prefixlen 64  scopeid 0x20
        inet6 fe80::558e:634e:9863:ac97  prefixlen 64  scopeid 0x20
        ether 00:0c:29:68:43:c3  txqueuelen 1000  (Ethernet)
        RX packets 117512  bytes 104347456 (99.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 71594  bytes 17251471 (16.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ens32:1: flags=4163  mtu 1500
        inet 192.168.247.200  netmask 255.255.255.0  broadcast 192.168.247.255
        ether 00:0c:29:68:43:c3  txqueuelen 1000  (Ethernet)
lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 183  bytes 25994 (25.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 183  bytes 25994 (25.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:37:21:48  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

五.故障修复步骤

1.修复mysql

systemctl restart mysqld

2.修复主从

(1)在现主库服务器 Mysql2 查看二进制文件和同步点

show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      784 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

(2)在原主库服务器 mysql1 执行同步操作

change master to master_host='192.168.247.80',master_user='myslave',master_password='903',master_log_file='master-bin.000002',master_log_pos=784;
start slave;
mysql> change master to master_host='192.168.247.80',master_user='myslave',master_password='903',master_log_file='master-bin.000002',master_log_pos=784;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

3.在 manager 节点上修改配置文件app1.cnf(再把这个记录添加进去,因为它检测掉失效时候会自动消失)

vi /etc/masterha/app1.cnf
......
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.247.132 -s 192.168.247.90
......
[server1]
hostname=192.168.247.80
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.247.132
port=3306
[server3]
hostname=192.168.247.90
port=3306

4.在 manager 节点上启动 MHA

nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
  • 解决中英字不兼容报错的问题
dos2unix /usr/local/bin/master_ip_failover 

六.故障切换备选主库的算法

1.一般判断从库的是从(position/GTID)判断优劣,数据有差异,最接近于master的slave,成为备选主

2.数据一致的情况下,按照配置文件顺序,选择备选主库

3.设定有权重(candidate_master=1),按照权重强制指定备选主

(1)默认情况下如果一个slave落后master 1320M的relay logs的话,即使有权重,也会失效
(2)如果check_repl_delay=0的话,即使落后很多日志,也强制选择其为备选主

  • Mysql 1主机
[root@localhost ~]# ifconfig
ens32: flags=4163  mtu 1500
        inet 192.168.247.132  netmask 255.255.255.0  broadcast 192.168.247.255
        inet6 fe80::b7de:ddd:163c:8ed6  prefixlen 64  scopeid 0x20
        ether 00:0c:29:d4:a5:c4  txqueuelen 1000  (Ethernet)
        RX packets 73825  bytes 42345623 (40.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43076  bytes 11113379 (10.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens32:1: flags=4163  mtu 1500
        inet 192.168.247.200  netmask 255.255.255.0  broadcast 192.168.247.255
        ether 00:0c:29:d4:a5:c4  txqueuelen 1000  (Ethernet)

ens34: flags=4163  mtu 1500
        ether 00:0c:29:d4:a5:ce  txqueuelen 1000  (Ethernet)
        RX packets 7278  bytes 454944 (444.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1883  bytes 333522 (325.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 81  bytes 11981 (11.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 81  bytes 11981 (11.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:88:76:06  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • Mysql 2主机
[root@localhost mha4mysql-node-0.57]# ifconfig
ens32: flags=4163  mtu 1500
        inet 192.168.247.80  netmask 255.255.255.0  broadcast 192.168.247.255
        inet6 fe80::e5a6:8276:ca32:6add  prefixlen 64  scopeid 0x20
        inet6 fe80::b373:8a76:7b0b:f8ee  prefixlen 64  scopeid 0x20
        inet6 fe80::558e:634e:9863:ac97  prefixlen 64  scopeid 0x20
        ether 00:0c:29:68:43:c3  txqueuelen 1000  (Ethernet)
        RX packets 122632  bytes 104845451 (99.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 77500  bytes 19261856 (18.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens32:1: flags=4163  mtu 1500
        inet 192.168.247.200  netmask 255.255.255.0  broadcast 192.168.247.255
        ether 00:0c:29:68:43:c3  txqueuelen 1000  (Ethernet)

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 185  bytes 26098 (25.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 185  bytes 26098 (25.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:37:21:48  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

你可能感兴趣的:(MYSQL,mysql,android,数据库)