目录
1.5、将下文导入my.cnf(101、102、103都做)
3.4、mha-manager确认replication状态
Msyql MHA高可用
MHA简介:
MHA在mysql故障切换过程中,MHA能做到0-30秒之内自动完成数据库故障切换操作,并且在进行故障切换的过程中,MHA能在做大的程度上保证数据的一致性,以达到真正意义上的高可用,改软件有两部分组成, MHA Manager (管理节点),MHA Node (数据节点),MHA Manager 可以单独部署在一台独立的主机上管理多个master-slave集群,也可以部署在一台slave上, MHA Nodee 运行在每台SQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新的slave提升为新的master节点,然后将所有的slave重新指向新的master,整个故障转移过程对应用程序完全透明。
MHA 优点:
1、主从切换非常迅速,0-30s
2、最大程读上解决数据一致性的问题
3、不需要修改当前已有的mysql架构与配置
4、不需要另外多余的服务器
5、没有性能损耗
6、没有存储引擎的限制
MHA 故障切换流程
1、从dow机的master中保存二进制日志事件
2、识别含有最新更新的slave
3、将更新过数据的slave 中的中继日志 同步到其他的slave
4、应用从master中保存的二进制日志事件
5、提升一个slave 成为新的master
6、使其他的slave 连接新的master 进行复制
Manager工具:
masterha_chexk_ssh :检查MHA的SSH配置
masterha_chexk_repl: 检查Mysql主从复制
masterha_manager :启动MHA
masterha_check_status:检测当前MH的运行状态
masterha_master_monitor:将测master是否宕机
MHA搭建操作步骤:
主机名 | 主机ip | Server_id | 部署应用 |
---|---|---|---|
mha-master | 192.168.200.101 | 1 | mha-node&MySQL-master |
mha-slave1 | 192.168.200.102 | 2 | mha-node&MySQL-slave1 |
mha-slave2 | 192.168.200.103 | 3 | mha-node&MySQL-slave2 |
mha-manager | 192.168.200.104 | 无 | mha-node&mha-manager |
一、基础环境搭建
1.1、将主机全部修改主机名&生成密钥
[root@localhost ~]# hostnamectl set-hostname mha-master && bash
[root@localhost ~]# hostnamectl set-hostname mha-slave1 && bash
[root@localhost ~]# hostnamectl set-hostname mha-slave2 && bash
[root@localhost ~]# hostnamectl set-hostname mha-manager && bash
ssh-keygen #生成公钥 私钥,
ssh-copy-id root@192.168.200.% #将公钥发送到目标主机
#所有主机互相发送ssh公钥,做免密
1.2、man-manager修改host同步
[root@mha-manager ~]# cat >> /etc/hosts << EOF
192.168.200.101 mha-master
192.168.200.102 mha-slave1
192.168.200.103 mha-slave2
192.168.200.104 mha-manager
EOF
[root@mha-manager ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.101 mha-master
192.168.200.102 mha-slave1
192.168.200.103 mha-slave2
192.168.200.104 mha-manager
//ping测试
[root@mha-manager ~]# for i in {master,slave1,slave2,manager}; do ping -c 1
mha-$i; done
PING mha-master (192.168.200.101) 56(84) bytes of data.
64 bytes from mha-master (192.168.200.101): icmp_seq=1 ttl=64 time=0.330 ms
--- mha-master ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.330/0.330/0.330/0.000 ms
PING mha-slave1 (192.168.200.102) 56(84) bytes of data.
64 bytes from mha-slave1 (192.168.200.102): icmp_seq=1 ttl=64 time=0.295 ms
--- mha-slave1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.295/0.295/0.295/0.000 ms
PING mha-slave2 (192.168.200.103) 56(84) bytes of data.
64 bytes from mha-slave2 (192.168.200.103): icmp_seq=1 ttl=64 time=0.365 ms
--- mha-slave2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.365/0.365/0.365/0.000 ms
PING mha-manager (192.168.200.104) 56(84) bytes of data.
64 bytes from mha-manager (192.168.200.104): icmp_seq=1 ttl=64 time=0.014 ms
--- mha-manager ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.014/0.014/0.014/0.000 ms
#所有主机同步hosts文件
[root@mha-manager ~]# for i in {master,slave1,slave2};do echo "#####$i•info#####";ssh mha-$i hostname --all-ip;ssh mha-$i cat /etc/hosts; done
#####master-info#####
192.168.200.101
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.101 mha-master
192.168.200.102 mha-slave1
192.168.200.103 mha-slave2
192.168.200.104 mha-manager
#####slave1-info#####
192.168.200.102
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.101 mha-master
192.168.200.102 mha-slave1
192.168.200.103 mha-slave2
192.168.200.104 mha-manager
#####slave2-info#####
192.168.200.103
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.101 mha-master
192.168.200.102 mha-slave1
192.168.200.103 mha-slave2
192.168.200.104 mha-manager
1.3、yum安装mysql 5.7版本
#下载yum 源
yum -y install https://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
#下载mysql
yum -y install --nogpgcheck mysql-community-server
1.44、优化MySQL(101、102、103都做)
[root@mha-master ~]# cp /etc/my.cnf{,.back}
[root@mha-master ~]# mkdir -pv /data/mysql/logs
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/mysql"
mkdir: 已创建目录 "/data/mysql/logs"
[root@mha-master ~]# chown -R mysql:mysql /data/mysql/
1.5、将下文导入my.cnf(101、102、103都做)
[mysql]
port=3306
bind-address=0.0.0.0
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
user = mysql
socket=/var/lib/mysql/mysql.sock
datadir=/data/mysql/data
max_connections = 2000
relay-log=/data/mysql/data/relay-log
log-bin=/data/mysql/data/mysql-bin
max_binlog_size = 500M
binlog_cache_size = 128K
binlog-ignore-db = mysql
expire_logs_days = 7
binlog_format="ROW"
server-id=1
skip-name-resolve
#慢查询日志优化
lower_case_table_names=1
slow_query_log=on
slow_query_log_file=/data/mysql/logs/slow_query_log.log
long_query_time=2
symbolic-links=0
log-error=/data/mysql/logs/mysql.err
#mysql优化配置
max_connect_errors = 100
query_cache_type=1
query_cache_size=128M
innodb_buffer_pool_size=4G
innodb_buffer_pool_instances=16
innodb_open_files=800 #innodb
innodb_log_file_size=128M
innodb_log_files_in_group=3#redo log
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=2
gtid_mode = on
binlog-ignore-db = mysql
relay-log-recovery=1
sync_binlog=1
read_only = on
enforce_gtid_consistency = 1
log_slave_updates = 1
#同步my.cnf
[root@mha-master ~]# scp /etc/my.cnf mha-slave1:/etc/
my.cnf
100% 8916 6.7MB/s 00:00
[root@mha-master ~]# scp /etc/my.cnf mha-slave2:/etc/
my.cnf
100% 8916 9.9MB/s 00:00
1.6、mha-slave1
[root@mha-slave1 ~]# sed -i 's/server-id=1/server-id=2/g' /etc/my.cnf
[root@mha-slave1 ~]# mkdir -pv /data/mysql/logs
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/mysql"
mkdir: 已创建目录 "/data/mysql/logs"
[root@mha-slave1 ~]# chown -R mysql:mysql /data/mysql/
[root@mha-slave1 ~]# systemctl start mysqld
[root@mha-slave1 ~]# systemctl enable mysqld
[root@mha-slave1 ~]# grep password /data/mysql/logs/mysql.err
2022-01-18T03:18:30.830531Z 1 [Note] A temporary password is generated for
root@localhost: BqJDC,lef3R&
[root@mha-slave1 ~]# mysql -uroot -p'BqJDC,lef3R&'
mysql: [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.7.37-log
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123123';
Query OK, 0 rows affected (5.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> \q
Bye
1.6.1、mha-slave2
[root@mha-slave1 ~]# sed -i 's/server-id=1/server-id=2/g' /etc/my.cnf
[root@mha-slave1 ~]# mkdir -pv /data/mysql/logs
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/mysql"
mkdir: 已创建目录 "/data/mysql/logs"
[root@mha-slave1 ~]# chown -R mysql:mysql /data/mysql/
[root@mha-slave1 ~]# systemctl start mysqld
[root@mha-slave1 ~]# systemctl enable mysqld
[root@mha-slave1 ~]# grep password /data/mysql/logs/mysql.err
2022-01-18T03:18:30.830531Z 1 [Note] A temporary password is generated for
root@localhost: BqJDC,lef3R&
[root@mha-slave1 ~]# mysql -uroot -p'BqJDC,lef3R&'
mysql: [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.7.37-log
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123123';
Query OK, 0 rows affected (5.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> \q
Bye
二、开始配置主从复制
2.1、mha-master
[root@mha-master ~]# mysql -uroot -p123123
mysql: [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 3
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 repl@'192.168.200.%' identified by
'repl_user';
Query OK, 0 rows affected, 1 warning (0.12 sec)
//创建监控用户
mysql> grant all on *.* to 'mha'@'192.168.200.%' identified by 'mha_user';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
3.6.2:mha-slave1
3.6.3:mha-slave2
mysql> \q
Bye
2.2、mha-slave1
[root@mha-slave1 ~]# mysql -uroot -p123123
mysql: [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 3
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 repl@'192.168.200.%' identified by
'repl_user';
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> grant all on *.* to 'mha'@'192.168.200.%' identified by 'mha_user';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
2.3、mha-slave2
[root@mha-slave1 ~]# mysql -uroot -p123123
mysql: [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 3
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 repl@'192.168.200.%' identified by
'repl_user';
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> grant all on *.* to 'mha'@'192.168.200.%' identified by 'mha_user';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
完成MySQL主从
2.4、mha-master确认binlog
mha-master
[root@mha-master ~]# mysql -uroot -p123123
mysql: [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 4
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> show master status;
+------------------+----------+--------------+------------------+---------------
---------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------
---------------------------+
| mysql-bin.000002 | 1288 | | mysql,mysql | 5b7281ad-780c-
11ec-8e49-000c297c8a97:1-5 |
+------------------+----------+--------------+------------------+---------------
---------------------------+
1 row in set (0.00 sec)
mysql> \q
Bye
2.5、mha-slave1完成从库复制
[root@mha-slave1 ~]# mysql -uroot -p123123
mysql: [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 5
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> change master to
master_host='192.168.200.101',master_user='repl',master_password='repl_user',mas
ter_log_file='mysql-bin.000002',master_log_pos=1288;
Query OK, 0 rows affected, 2 warnings (0.14 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1288
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes #关注此项是否为yes
Slave_SQL_Running: Yes #关注此项是否为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: 1288
Relay_Log_Space: 521
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: 5b7281ad-780c-11ec-8e49-000c297c8a97
3.7.3:mha-slave2完成从库复制
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more
updates
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: 4f4c120f-780d-11ec-9831-000c292a62e0:1-5
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.02 sec)
ERROR:
No query specified
2.5.1、mha-slave2完成从库复制
[root@mha-slave2 ~]# mysql -uroot -p123123
mysql: [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 3
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> change master to
master_host='192.168.200.101',master_user='repl',master_password='repl_user',mas
ter_log_file='mysql-bin.000002',master_log_pos=1288;
Query OK, 0 rows affected, 2 warnings (0.06 sec)
mysql> commit;
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: 192.168.200.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1288
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
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: 1288
Relay_Log_Space: 521
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: 5b7281ad-780c-11ec-8e49-000c297c8a97
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more
updates
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: f04e0d29-780d-11ec-9506-000c2987e0bd:1-7
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> quit
三、MHA部署
3.1、101-103开始安装mha-node
[root@mha-master ~]# mkdir -pv /data/software
mkdir: 已创建目录 "/data/software"
[root@mha-master ~]# cd /data/software/
[root@mha-master software]# yum install -y mha4mysql-node-0.58-
0.el7.centos.noarch.rpm
//同步软件包
[root@mha-master software]# ssh mha-slave1 mkdir -pv /data/software
mkdir: 已创建目录 "/data/software"
[root@mha-master software]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm mhaslave1:/data/software/
mha4mysql-node-0.58-0.el7.centos.noarch.rpm
100% 35KB 1.5MB/s 00:00
[root@mha-master software]# ssh mha-slave2 mkdir -pv /data/software
mkdir: 已创建目录 "/data/software"
[root@mha-master software]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm mhaslave2:/data/software/
mha4mysql-node-0.58-0.el7.centos.noarch.rpm
100% 35KB 10.9MB/s 00:00
mha-slave1安装
[root@mha-slave1 ~]# cd /data/software/ && yum install -y mha4mysql-node-0.58-
0.el7.centos.noarch.rpm
mha-slave2安装
[root@mha-slave2 ~]# cd /data/software/ && yum install -y mha4mysql-node-0.58-
0.el7.centos.noarch.rpm
3.2、配置mha-manager(104)
[root@mha-manager ~]# mkdir -pv /data/mha/{conf,logs,tools,tmp}
//配置ip切换脚本
[root@mha-manager ~]# vim /data/mha/tools/master_ip_failover_script
#!/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
);
#定义VIP
my $vip = '192.168.200.200/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
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,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
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() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --
orig_master_host=host --orig_master_ip=ip --orig_master_port=port --
new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
[root@mha-manager ~]# chmod 777 /data/mha/tools/master_ip_failover_script
###配置mha配置文件
[root@mha-manager ~]# vim /data/mha/conf/mha192.168.200.101-104.conf
[server default]
manager_workdir=/data/mha/tmp
manager_log=/data/mha/logs/manager.log
master_binlog_dir=/data/mysql/data
remote_workdir=/data/mha/tmp
ssh_user=root
user=mha
password=mha_user
repl_user=repl
repl_password=repl_user
ping_interval=1
master_ip_failover_script=/data/mha/tools/master_ip_failover_script
[server1]
hostname=192.168.200.101
ssh_port=22
candidate_master=1
[server2]
hostname=192.168.200.102
ssh_port=22
candidate_master=1
[server3]
hostname=192.168.200.103
ssh_port=22
candidate_master=0
3.3、测试mha-manager的ssh互信
[root@mha-manager ~]# masterha_check_ssh --
conf=/data/mha/conf/mha192.168.200.101-104.conf
Tue Jan 18 15:33:02 2022 - [warning] Global configuration file
/etc/masterha_default.cnf not found. Skipping.
Tue Jan 18 15:33:02 2022 - [info] Reading application default configuration from
/data/mha/conf/mha192.168.200.101-104.conf..
Tue Jan 18 15:33:02 2022 - [info] Reading server configuration from
/data/mha/conf/mha192.168.200.101-104.conf..
Tue Jan 18 15:33:02 2022 - [info] Starting SSH connection tests..
Tue Jan 18 15:33:03 2022 - [debug]
Tue Jan 18 15:33:02 2022 - [debug] Connecting via SSH from
root@192.168.200.101(192.168.200.101:22) to
root@192.168.200.102(192.168.200.102:22)..
Tue Jan 18 15:33:03 2022 - [debug] ok. (确认状态)
Tue Jan 18 15:33:03 2022 - [debug] Connecting via SSH from
root@192.168.200.101(192.168.200.101:22) to
root@192.168.200.103(192.168.200.103:22)..
Tue Jan 18 15:33:03 2022 - [debug] ok. (确认状态)
Tue Jan 18 15:33:04 2022 - [debug]
Tue Jan 18 15:33:03 2022 - [debug] Connecting via SSH from
root@192.168.200.102(192.168.200.102:22) to
root@192.168.200.101(192.168.200.101:22)..
Tue Jan 18 15:33:03 2022 - [debug] ok. (确认状态)
Tue Jan 18 15:33:03 2022 - [debug] Connecting via SSH from
root@192.168.200.102(192.168.200.102:22) to
root@192.168.200.103(192.168.200.103:22)..
Tue Jan 18 15:33:04 2022 - [debug] ok. (确认状态)
Tue Jan 18 15:33:05 2022 - [debug]
Tue Jan 18 15:33:03 2022 - [debug] Connecting via SSH from
root@192.168.200.103(192.168.200.103:22) to
root@192.168.200.101(192.168.200.101:22)..
Tue Jan 18 15:33:04 2022 - [debug] ok. (确认状态)
Tue Jan 18 15:33:04 2022 - [debug] Connecting via SSH from
root@192.168.200.103(192.168.200.103:22) to
root@192.168.200.102(192.168.200.102:22)..
Tue Jan 18 15:33:04 2022 - [debug] ok. (确认状态)
Tue Jan 18 15:33:05 2022 - [info] All SSH connection tests passed successfully.
3.4、mha-manager确认replication状态
[root@mha-manager ~]# masterha_check_repl --
conf=/data/mha/conf/mha192.168.200.101-104.conf
Tue Jan 18 15:35:06 2022 - [warning] Global configuration file
/etc/masterha_default.cnf not found. Skipping.
Tue Jan 18 15:35:06 2022 - [info] Reading application default configuration from
/data/mha/conf/mha192.168.200.101-104.conf..
Tue Jan 18 15:35:06 2022 - [info] Reading server configuration from
/data/mha/conf/mha192.168.200.101-104.conf..
Tue Jan 18 15:35:06 2022 - [info] MHA::MasterMonitor version 0.58.
Tue Jan 18 15:35:08 2022 - [info] GTID failover mode = 1
Tue Jan 18 15:35:08 2022 - [info] Dead Servers:
Tue Jan 18 15:35:08 2022 - [info] Alive Servers:
Tue Jan 18 15:35:08 2022 - [info] 192.168.200.101(192.168.200.101:3306)
Tue Jan 18 15:35:08 2022 - [info] 192.168.200.102(192.168.200.102:3306)
Tue Jan 18 15:35:08 2022 - [info] 192.168.200.103(192.168.200.103:3306)
Tue Jan 18 15:35:08 2022 - [info] Alive Slaves:
Tue Jan 18 15:35:08 2022 - [info] 192.168.200.102(192.168.200.102:3306)
Version=5.7.37-log (oldest major version between slaves) log-bin:enabled
Tue Jan 18 15:35:08 2022 - [info] GTID ON
Tue Jan 18 15:35:08 2022 - [info] Replicating from
192.168.200.101(192.168.200.101:3306)
Tue Jan 18 15:35:08 2022 - [info] Primary candidate for the new Master
(candidate_master is set)
Tue Jan 18 15:35:08 2022 - [info] 192.168.200.103(192.168.200.103:3306)
Version=5.7.37-log (oldest major version between slaves) log-bin:enabled
Tue Jan 18 15:35:08 2022 - [info] GTID ON
Tue Jan 18 15:35:08 2022 - [info] Replicating from
192.168.200.101(192.168.200.101:3306)
Tue Jan 18 15:35:08 2022 - [info] Current Alive Master:
192.168.200.101(192.168.200.101:3306)
Tue Jan 18 15:35:08 2022 - [info] Checking slave configurations..
Tue Jan 18 15:35:08 2022 - [info] Checking replication filtering settings..
Tue Jan 18 15:35:08 2022 - [info] binlog_do_db= , binlog_ignore_db= mysql
Tue Jan 18 15:35:08 2022 - [info] Replication filtering check ok.
Tue Jan 18 15:35:08 2022 - [info] GTID (with auto-pos) is supported. Skipping
all SSH and Node package checking.
Tue Jan 18 15:35:08 2022 - [info] Checking SSH publickey authentication settings
on the current master..
Tue Jan 18 15:35:08 2022 - [info] HealthCheck: SSH to 192.168.200.101 is
reachable.
Tue Jan 18 15:35:08 2022 - [info]
192.168.200.101(192.168.200.101:3306) (current master)
+--192.168.200.102(192.168.200.102:3306)
+--192.168.200.103(192.168.200.103:3306)
Tue Jan 18 15:35:08 2022 - [info] Checking replication health on
192.168.200.102..
Tue Jan 18 15:35:08 2022 - [info] ok. (确认状态)
Tue Jan 18 15:35:08 2022 - [info] Checking replication health on
192.168.200.103..
Tue Jan 18 15:35:08 2022 - [info] ok. (确认状态)
Tue Jan 18 15:35:08 2022 - [info] Checking master_ip_failover_script status:
Tue Jan 18 15:35:08 2022 - [info] /data/mha/tools/master_ip_failover_script --
command=status --ssh_user=root --orig_master_host=192.168.200.101 --
orig_master_ip=192.168.200.101 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1
192.168.200.200/24===
Checking the Status of the script.. OK
Tue Jan 18 15:35:08 2022 - [info] OK.
Tue Jan 18 15:35:08 2022 - [warning] shutdown_script is not defined.
Tue Jan 18 15:35:08 2022 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK. (确认状态)
3.5、启动mha状态
[root@mha-manager ~]# nohup masterha_manager --
conf=/data/mha/conf/mha192.168.200.101-104.conf &>
/data/mha/logs/mha_manager.log &
[1] 21516
//检查mha状态
[root@mha-manager ~]# masterha_check_status --
conf=/data/mha/conf/mha192.168.200.101-104.conf
mha192.168.200.101-104 (pid:21516) is running(0:PING_OK), master:192.168.200.101
3.6、配置mha-master-ip信息
[root@mha-manager ~]# ssh mha-master ifconfig ens33:1 192.168.200.200/24
[root@mha-manager ~]# ssh mha-master ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.101 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::20c:29ff:fe7c:8a97 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:7c:8a:97 txqueuelen 1000 (Ethernet)
RX packets 201438 bytes 273581532 (260.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 25843 bytes 2633466 (2.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.200 netmask 255.255.255.0 broadcast 192.168.200.255
ether 00:0c:29:7c:8a:97 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 156 bytes 22326 (21.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 156 bytes 22326 (21.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0