架构图:
| 192.168.10.11 | 192.168.10.12 | 192.168.10.13 |
|---|---|---|
| MySQL Master | MySQL Slave | MySQL Slave |
| MHA-node | MHA-node | MHA-node + MHA master |
前提条件:
1. 机器节点在同一个网段, 交换机下2. 系统为同版本, 网卡都是ens33 或者 都是eth0 的命名3. 各节点免密钥认证4. 各节点mysql创建主从复制用户, mha用户, 且用户密码一致
1、命令软连接
所有节点:
ln -s /data/app/mysql5728/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /data/app/mysql5728/bin/mysql /usr/bin/mysql
#检查
ls /usr/bin/ | grep mysql
2、各节点免密钥认证
所有节点:
ssh-keygen -t rsa -b 2048
ssh-copy-id root@192.168.10.11
ssh-copy-id root@192.168.10.12
ssh-copy-id root@192.168.10.13
#检查
ssh 192.168.10.11 date
ssh 192.168.10.12 date
ssh 192.168.10.13 date
3、安装软件
所有节点安装Node软件依赖包
yum install perl-DBD-MySQL -y
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
#磁盘版本支持MySQL 5.7 和 8.0
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm
Manager软件安装
#db03节点
yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
yum install -y mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
注意:
maneger依赖于node, 安装master之前先安装node
在所有mysql节点创建mha用户
#本次在主从创建, 自动同步到从库
grant all privileges on *.* to 'mha'@'192.168.10.%' identified by 'mha';
#MySQL 8.0
create user 'mha'@'192.168.10.%' identified by 'mha';
grant all privileges on *.* to 'mha'@'192.168.10.%';
Manager配置文件准备
#db03节点
#创建配置文件目录
mkdir -p /etc/mha
#创建日志目录
mkdir -p /var/log/mha/app1
#编辑mha配置文件
cat > /etc/mha/app1.cnf <<EOF
[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/data/3306/logs
user=mha
password=mha
ping_interval=2
repl_password=123
repl_user=slave
ssh_user=root
[server1]
hostname=192.168.10.11
port=3306
[server2]
hostname=192.168.10.12
port=3306
candidate_master=1
[server3]
no_master=1
hostname=192.168.10.13
port=3306
EOF
#参数解释
[server default]
manager_log=/var/log/mha/app1/manager #日志文件
manager_workdir=/var/log/mha/app1 #数据目录
master_binlog_dir=/data/3306/logs #MySQL binlog目录, 不指定会报错
user=mha #MHA监控用户 (mysql.user表)
password=mha #MHA监控密码 (mysql.user表)
ping_interval=2 #ping间隔, 用来检测master是否正常, 默认是3秒, 尝试三次没有回应的时候自动进行failover
repl_password=123 #mysql主从复制密码
repl_user=slave #mysql主从复制用户
ssh_user=root #ssh免密钥认证用户
[server1]
hostname=192.168.10.11
port=3306
[server2]
hostname=192.168.10.12
port=3306
candidate_master=1 #master机宕掉后, 优先启用这台作为新master
[server3]
no_master=1 #一定不会选这个机器为master, 根据情况设置
hostname=192.168.10.13
port=3306
参考链接:
https://www.jianshu.com/p/41f0774226df
状态检查
masterha_check_ssh --conf=/etc/mha/app1.cnf
#正确输出信息
Tue Aug 17 16:48:56 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 17 16:48:56 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Aug 17 16:48:56 2021 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Aug 17 16:48:56 2021 - [info] Starting SSH connection tests..
Tue Aug 17 16:48:57 2021 - [debug]
Tue Aug 17 16:48:56 2021 - [debug] Connecting via SSH from root@192.168.10.11(192.168.10.11:22) to root@192.168.10.12(192.168.10.12:22)..
Tue Aug 17 16:48:56 2021 - [debug] ok.
Tue Aug 17 16:48:56 2021 - [debug] Connecting via SSH from root@192.168.10.11(192.168.10.11:22) to root@192.168.10.13(192.168.10.13:22)..
Tue Aug 17 16:48:57 2021 - [debug] ok.
Tue Aug 17 16:48:58 2021 - [debug]
Tue Aug 17 16:48:57 2021 - [debug] Connecting via SSH from root@192.168.10.12(192.168.10.12:22) to root@192.168.10.11(192.168.10.11:22)..
Tue Aug 17 16:48:57 2021 - [debug] ok.
Tue Aug 17 16:48:57 2021 - [debug] Connecting via SSH from root@192.168.10.12(192.168.10.12:22) to root@192.168.10.13(192.168.10.13:22)..
Tue Aug 17 16:48:58 2021 - [debug] ok.
Tue Aug 17 16:48:59 2021 - [debug]
Tue Aug 17 16:48:57 2021 - [debug] Connecting via SSH from root@192.168.10.13(192.168.10.13:22) to root@192.168.10.11(192.168.10.11:22)..
Tue Aug 17 16:48:58 2021 - [debug] ok.
Tue Aug 17 16:48:58 2021 - [debug] Connecting via SSH from root@192.168.10.13(192.168.10.13:22) to root@192.168.10.12(192.168.10.12:22)..
Tue Aug 17 16:48:59 2021 - [debug] ok.
Tue Aug 17 16:48:59 2021 - [info] All SSH connection tests passed successfully.
--------------------------------------------------
masterha_check_repl --conf=/etc/mha/app1.cnf
#正确输出信息
Tue Aug 17 16:49:13 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 17 16:49:13 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Aug 17 16:49:13 2021 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Aug 17 16:49:13 2021 - [info] MHA::MasterMonitor version 0.58.
Tue Aug 17 16:49:14 2021 - [info] GTID failover mode = 1
Tue Aug 17 16:49:14 2021 - [info] Dead Servers:
Tue Aug 17 16:49:14 2021 - [info] Alive Servers:
Tue Aug 17 16:49:14 2021 - [info] 192.168.10.11(192.168.10.11:3306)
Tue Aug 17 16:49:14 2021 - [info] 192.168.10.12(192.168.10.12:3306)
Tue Aug 17 16:49:14 2021 - [info] 192.168.10.13(192.168.10.13:3306)
Tue Aug 17 16:49:14 2021 - [info] Alive Slaves:
Tue Aug 17 16:49:14 2021 - [info] 192.168.10.12(192.168.10.12:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Tue Aug 17 16:49:14 2021 - [info] GTID ON
Tue Aug 17 16:49:14 2021 - [info] Replicating from 192.168.10.11(192.168.10.11:3306)
Tue Aug 17 16:49:14 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Aug 17 16:49:14 2021 - [info] 192.168.10.13(192.168.10.13:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Tue Aug 17 16:49:14 2021 - [info] GTID ON
Tue Aug 17 16:49:14 2021 - [info] Replicating from 192.168.10.11(192.168.10.11:3306)
Tue Aug 17 16:49:14 2021 - [info] Not candidate for the new Master (no_master is set)
Tue Aug 17 16:49:14 2021 - [info] Current Alive Master: 192.168.10.11(192.168.10.11:3306)
Tue Aug 17 16:49:14 2021 - [info] Checking slave configurations..
Tue Aug 17 16:49:14 2021 - [info] read_only=1 is not set on slave 192.168.10.12(192.168.10.12:3306).
Tue Aug 17 16:49:14 2021 - [info] read_only=1 is not set on slave 192.168.10.13(192.168.10.13:3306).
Tue Aug 17 16:49:14 2021 - [info] Checking replication filtering settings..
Tue Aug 17 16:49:14 2021 - [info] binlog_do_db= , binlog_ignore_db=
Tue Aug 17 16:49:14 2021 - [info] Replication filtering check ok.
Tue Aug 17 16:49:14 2021 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Tue Aug 17 16:49:14 2021 - [info] Checking SSH publickey authentication settings on the current master..
Tue Aug 17 16:49:14 2021 - [info] HealthCheck: SSH to 192.168.10.11 is reachable.
Tue Aug 17 16:49:14 2021 - [info]
192.168.10.11(192.168.10.11:3306) (current master)
+--192.168.10.12(192.168.10.12:3306)
+--192.168.10.13(192.168.10.13:3306)
Tue Aug 17 16:49:14 2021 - [info] Checking replication health on 192.168.10.12..
Tue Aug 17 16:49:14 2021 - [info] ok.
Tue Aug 17 16:49:14 2021 - [info] Checking replication health on 192.168.10.13..
Tue Aug 17 16:49:14 2021 - [info] ok.
Tue Aug 17 16:49:14 2021 - [warning] master_ip_failover_script is not defined.
Tue Aug 17 16:49:14 2021 - [warning] shutdown_script is not defined.
Tue Aug 17 16:49:14 2021 - [info] Got exit code 0 (Not master dead).
开启 mha-manager
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状态
masterha_check_status --conf=/etc/mha/app1.cnf
#正确输出信息
app1 (pid:4478) is running(0:PING_OK), master:192.168.10.11
查看进程
ps aux | grep masterha_manager
