赞
踩
MHA 是由日本人 yoshinorim使用perl语言开发的比较成熟的 MySQL 高可用方案。MHA 能够在30秒内实现故障切换,并能在故障切换中,最大可能的保证数据一致性。MHA(Master HA)是一款开源的 MySQL 的高可用程序,它为 MySQL 主从复制架构提供了 automating master failover 功能。MHA 在监控到 master 节点故障时,会提升其中拥有最新数据的 slave 节点成为新的master 节点,在此期间,MHA 会通过于其它从节点获取额外信息来避免一致性方面的问题。MHA 还提供了 master 节点的在线切换功能,即按需切换 master/slave 节点。MHA主要包含2个组成部分:MHA Manager管理节点和MHA Node数据节点。
说明:mha4mysql最近更新时间为2018年3月25号,也就是说MHA工具已经停止了更新。
MHA的目的在于维持Mysql Replication中master库的高可用性,其最大的特点是可以修复多个slave之间的差异日志,最终使所有slave保持数据一致,然后从中选择一个充当新的master,并将其他slave指向它。当master出现故障时,可以通过对比slave之间I/O thread读取主库binlog的position号,选取最接近的slave作为备选主库。其他的从库可以通过与备选主库对比生成差异的中继日志,在备选主库上应用从原来master保存的binlog,同时将备选主库提升为master。最后在其他slave上应用相应的差异中继日志并从新的master开始复制。
序号 | 主机名 | IP地址 | 主从角色 | server-id | MHA角色 |
---|---|---|---|---|---|
1 | test1 | 192.168.0.124 | master | 124 | node |
2 | test2 | 192.168.0.125 | slave | 125 | node |
3 | test3 | 192.168.0.126 | slave | 126 | manager,node |
配置test1、test2、test3主机之间的互信,参照博文
Linux之SSH免密登录配置
需要达到如下效果,互信之间ssh登录不需要输入密码进行验证。
[root@test1 opt]# ssh test2
Last login: Wed Feb 24 14:56:12 2021 from test1
[root@test2 ~]# ssh test3
Last login: Wed Feb 24 14:56:40 2021 from test3
[root@test3 ~]# ssh test1
Last login: Wed Feb 24 14:56:08 2021 from test1
详细搭建mysql主从步骤可以参照Mysql之GTID复制,其中第2个从节点的安装配置参照博文从节点的配置即可。
检查主从状态:
在主库上创建管理账号
mysql> create user dba@‘192.168.0.%’ identified by ‘Test!123’;
Query OK, 0 rows affected (0.02 sec)
mysql> grant all privileges on . to dba@‘192.168.0.%’;
flush privileges;Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@test1 opt]# yum install -y perl-DBD-MySQL
[root@test1 opt]# wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@test1 opt]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
Preparing… ################################# [100%]
Updating / installing…
1:mha4mysql-node-0.58-0.el7.centos ################################# [100%]
rpm方式安装node安装后也会在/usr/bin 下面会生成一下几个脚本(这些工具通常由MHA manager
的脚本触发,无需人为操作)主要如下:
[root@test3 opt]# wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
[root@test3 opt]# yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch* perl-Parallel-ForkManager*
[root@test3 opt]# perl-Parallel-ForkManager*
[root@test3 opt]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
Preparing… ################################# [100%]
Updating / installing…
1:mha4mysql-manager-0.58-0.el7.cent################################# [100%]
[root@test3 bin]# mkdir -p /usr/local/mha
[root@test3 bin]# mkdir -p /usr/local/mha/scritps/
[root@test3 opt]# mkdir -p /etc/mha
[root@test3 MHA]# wget https://github.com/yoshinorim/mha4mysql-manager/archive/v0.58.tar.gz
[root@test3 MHA]# tar -zxvf v0.58.tar.gz
[root@test3 MHA]# cd mha4mysql-manager-0.58/samples/scripts/
[root@test3 scripts]# cp ./* /usr/local/mha/scritps/
根据自己的规划,参加如下脚本添加部分配置,主要确认IP地址和网卡名称。
[root@test3 opt]# vim mha.conf
[server default]
user=dba //mha管理用户
password=Test!123 //mha管理密码
manager_workdir=/usr/local/mha //mha_master自己的工作路径
manager_log=/usr/local/mha/manager.log // mha_master自己的日志文件
remote_workdir=/usr/local/mha //每个远程主机的工作目录在何处
ssh_user=root // 基于ssh的密钥认证
repl_user=bak124 //数据库用户名
repl_password=Test!123 //数据库密码
ping_interval=1 //ping间隔时长
master_ip_failover_script=/usr/local/mha/scritps/master_ip_failover
master_ip_online_change_script=/usr/local/mha/scripts/master_ip_online_change
[server1]
hostname=192.168.0.124
ssh_port=22
master_binlog_dir=/var/lib/mysql
candidate_master=1
[server2]
hostname=192.168.0.125
ssh_port=22
master_binlog_dir=/var/lib/mysql
candidate_master=1
[server3]
hostname=192.168.0.126
ssh_port=22
master_binlog_dir=/var/lib/mysql
candidate_master=1
[root@test3 scritps]# masterha_check_ssh --conf=/etc/mha/mha.conf
Thu Feb 25 10:30:16 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Feb 25 10:30:16 2021 - [info] Reading application default configuration from /etc/mha/mha.conf…
Thu Feb 25 10:30:16 2021 - [info] Reading server configuration from /etc/mha/mha.conf…
Thu Feb 25 10:30:16 2021 - [info] Starting SSH connection tests…
Thu Feb 25 10:30:18 2021 - [debug]
Thu Feb 25 10:30:16 2021 - [debug] Connecting via SSH from root@192.168.0.124(192.168.0.124:22) to root@192.168.0.125(192.168.0.125:22)…
Thu Feb 25 10:30:17 2021 - [debug] ok.
Thu Feb 25 10:30:17 2021 - [debug] Connecting via SSH from root@192.168.0.124(192.168.0.124:22) to root@192.168.0.126(192.168.0.126:22)…
Thu Feb 25 10:30:17 2021 - [debug] ok.
Thu Feb 25 10:30:18 2021 - [debug]
Thu Feb 25 10:30:17 2021 - [debug] Connecting via SSH from root@192.168.0.125(192.168.0.125:22) to root@192.168.0.124(192.168.0.124:22)…
Thu Feb 25 10:30:17 2021 - [debug] ok.
Thu Feb 25 10:30:17 2021 - [debug] Connecting via SSH from root@192.168.0.125(192.168.0.125:22) to root@192.168.0.126(192.168.0.126:22)…
Thu Feb 25 10:30:18 2021 - [debug] ok.
Thu Feb 25 10:30:19 2021 - [debug]
Thu Feb 25 10:30:17 2021 - [debug] Connecting via SSH from root@192.168.0.126(192.168.0.126:22) to root@192.168.0.124(192.168.0.124:22)…
Thu Feb 25 10:30:18 2021 - [debug] ok.
Thu Feb 25 10:30:18 2021 - [debug] Connecting via SSH from root@192.168.0.126(192.168.0.126:22) to root@192.168.0.125(192.168.0.125:22)…
Thu Feb 25 10:30:18 2021 - [debug] ok.
Thu Feb 25 10:30:19 2021 - [info] All SSH connection tests passed successfully.
[root@test3 mha]# masterha_check_repl --conf=/etc/mha/mha.conf
Thu Feb 25 10:52:46 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Feb 25 10:52:46 2021 - [info] Reading application default configuration from /etc/mha/mha.conf…
Thu Feb 25 10:52:46 2021 - [info] Reading server configuration from /etc/mha/mha.conf…
Thu Feb 25 10:52:46 2021 - [info] MHA::MasterMonitor version 0.58.
Thu Feb 25 10:52:48 2021 - [info] GTID failover mode = 1
Thu Feb 25 10:52:48 2021 - [info] Dead Servers:
Thu Feb 25 10:52:48 2021 - [info] Alive Servers:
Thu Feb 25 10:52:48 2021 - [info] 192.168.0.124(192.168.0.124:3306)
Thu Feb 25 10:52:48 2021 - [info] 192.168.0.125(192.168.0.125:3306)
Thu Feb 25 10:52:48 2021 - [info] 192.168.0.126(192.168.0.126:3306)
Thu Feb 25 10:52:48 2021 - [info] Alive Slaves:
Thu Feb 25 10:52:48 2021 - [info] 192.168.0.125(192.168.0.125:3306) Version=5.7.33-log (oldest major version between slaves) log-bin:enabled
Thu Feb 25 10:52:48 2021 - [info] GTID ON
Thu Feb 25 10:52:48 2021 - [info] Replicating from 192.168.0.124(192.168.0.124:3306)
Thu Feb 25 10:52:48 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Feb 25 10:52:48 2021 - [info] 192.168.0.126(192.168.0.126:3306) Version=5.7.33-log (oldest major version between slaves) log-bin:enabled
Thu Feb 25 10:52:48 2021 - [info] GTID ON
Thu Feb 25 10:52:48 2021 - [info] Replicating from 192.168.0.124(192.168.0.124:3306)
Thu Feb 25 10:52:48 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Feb 25 10:52:48 2021 - [info] Current Alive Master: 192.168.0.124(192.168.0.124:3306)
Thu Feb 25 10:52:48 2021 - [info] Checking slave configurations…
Thu Feb 25 10:52:48 2021 - [info] read_only=1 is not set on slave 192.168.0.125(192.168.0.125:3306).
Thu Feb 25 10:52:48 2021 - [info] read_only=1 is not set on slave 192.168.0.126(192.168.0.126:3306).
Thu Feb 25 10:52:48 2021 - [info] Checking replication filtering settings…
Thu Feb 25 10:52:48 2021 - [info] binlog_do_db= , binlog_ignore_db=
Thu Feb 25 10:52:48 2021 - [info] Replication filtering check ok.
Thu Feb 25 10:52:48 2021 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Thu Feb 25 10:52:48 2021 - [info] Checking SSH publickey authentication settings on the current master…
Thu Feb 25 10:52:48 2021 - [info] HealthCheck: SSH to 192.168.0.124 is reachable.
Thu Feb 25 10:52:48 2021 - [info]
192.168.0.124(192.168.0.124:3306) (current master)
±-192.168.0.125(192.168.0.125:3306)
±-192.168.0.126(192.168.0.126:3306)
Thu Feb 25 10:52:48 2021 - [info] Checking replication health on 192.168.0.125…
Thu Feb 25 10:52:48 2021 - [info] ok.
Thu Feb 25 10:52:48 2021 - [info] Checking replication health on 192.168.0.126…
Thu Feb 25 10:52:48 2021 - [info] ok.
Thu Feb 25 10:52:48 2021 - [info] Checking master_ip_failover_script status:
Thu Feb 25 10:52:48 2021 - [info] /usr/local/mha/scritps/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.0.124 --orig_master_ip=192.168.0.124 --orig_master_port=3306
Thu Feb 25 10:52:48 2021 - [info] OK.
Thu Feb 25 10:52:48 2021 - [warning] shutdown_script is not defined.
Thu Feb 25 10:52:48 2021 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
[root@test1 bin]# ip addr add 192.168.0.128 dev ens33:1
[root@test1 bin]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:0b:9f:a0 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.124/24 brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.0.128/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b9a:8d9a:4d5f:b566/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@test3 mha]# nohup masterha_manager --conf=/etc/mha/mha.conf > /tmp/mha_manager.log </dev/null 2>&1 &
[1] 37561
[root@test3 mha]# masterha_check_status --conf=/etc/mha/mha.conf
mha (pid:37561) is running(0:PING_OK), master:192.168.0.124
[root@test1 bin]# systemctl stop mysqld
切换完成后已经停止MHA服务
[root@test3 mha]# masterha_check_status --conf=/etc/mha/mha.conf
mha is stopped(2:NOT_RUNNING).
125节点变成了新的master节点
mysql> show slave status\G
Empty set (0.00 sec)
mysql> show master slave\G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘slave’ at line 1
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 1379
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 65a0c788-7671-11eb-8627-000c290b9fa0:1-6
1 row in set (0.00 sec)
126上看到新的master已经变成了125
[root@test3 mha]# masterha_master_switch --help
Usage:
# For master failover
masterha_master_switch --master_state=dead
–global_conf=/etc/masterha_default.cnf
–conf=/usr/local/masterha/conf/app1.cnf --dead_master_host=host1
# For online master switch
masterha_master_switch --master_state=alive
–global_conf=/etc/masterha_default.cnf
–conf=/usr/local/masterha/conf/app1.cnf
rm -rf /usr/local/mha/mha.failover.complete
报错信息:
解决方案:
停止MHA服务,然后再执行在线手动切换。
[root@test3 mha]# masterha_stop --conf=/etc/mha/mha.conf
Stopped mha successfully.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。