MHA功能:
- 从故障的mysql保存二进制日志时间(binlog events);
- 识别含有最新更新的slave;
- 应用差异的中继日志(relay log)到其他的slave;
- 应用从master保存的二进制日志文事件;
- 提升一个slave为新的master;
- 是其他的slave连接新的master进行复制;
MHA环境搭建:
- manager节点:192.168.125.210
- node1节点:192.168.125.200
- node2节点:192.168.125.201
- node3节点:192.168.125.202
1,首先要配置mysql主从复制。
2,SSH免密钥互相访问配置:
[root@localhost ~]# ssh-keygen -t rsa -P '' //生成公钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:LzSIfKtqa4fh70k5xAreOULBySucDWzUsHf4+6PlHmQ root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| oo |
|= o.. |
| X o . |
|o Boo. . |
|o= .=.oES |
|+.oo.o+o o |
| +.*+..o. . |
| *.o++... |
| oo*=.o+. |
+----[SHA256]-----+
[root@localhost ~]# cat .ssh/id_rsa.pub > .ssh/authorized_keys //导入到本地认证文件
[root@localhost ~]# chmod go= .ssh/authorized_keys
[root@localhost ~]# scp .ssh/id_rsa .ssh/authorized_keys 192.168.125.200:/root/.ssh/ //SCP到各个节点就可以互相访问了。
[root@localhost ~]# scp -p .ssh/id_rsa .ssh/authorized_keys 192.168.125.201:/root/.ssh/
[root@localhost ~]# scp -p .ssh/id_rsa .ssh/authorized_keys 192.168.125.202:/root/.ssh/
3,安装MHA;
下载地址: https://downloads.mariadb.com/MHA/mha4mysql-manager-0.55-0.el6.noarch.rpm
https://downloads.mariadb.com/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
需要安装epel源,通过yum安装,在manager安装(manager和node包),在其他主从复制节点安装‘node’包。
4,配置MHA:
root@manager ~]# cat /etc/masterha/app1.cnf
[server default]
manager_workdir=/etc/masterha/app1 #配置manager的工作目录
manager_log=/etc/masterha/app1/manager.log #设置manager的日志
master_binlog_dir=/var/lib/mysql/ #设置master保存binlog的位置,以便MHA可以找到master的日志
user=mhaadmin #设置监控用的root权限账号,每个节点需要创建。
password=123123 #密码
ssh_user=root #设置ssh的登陆用户名。
repl_user=glq #设置复制环境中的复制用户名。
repl_password=123123 #密码
[server1]
hostname=192.168.125.200
port=3306
#master_binlog_dir=/var/lib64/mysql
candidate_master=1 #设置为候选master,即使这个库不是集群中最新的slave,也会选择为master。
#check_repl_delay=0 #默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master
[server2]
hostname=192.168.125.201
port=3306
#master_binlog_dir=/var/lib64/mysql
candidate_master=1
[server3]
hostname=192.168.125.202
port=3306
#master_binlog_dir=/var/lib64/mysql
#ignore_fail=1
#no_master=1
5,测试MHA;
[root@manager ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf #测试ssh连接是否正常。
Wed Mar 14 15:44:16 2018 - [info] All SSH connection tests passed successfully.
[root@manager ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf #测试主从配置是否正常
Wed Mar 14 15:45:09 2018 - [info]
192.168.125.200 (current master)
+--192.168.125.201
+--192.168.125.202
Wed Mar 14 15:45:09 2018 - [info] Checking replication health on 192.168.125.201..
Wed Mar 14 15:45:09 2018 - [info] ok.
Wed Mar 14 15:45:09 2018 - [info] Checking replication health on 192.168.125.202..
Wed Mar 14 15:45:09 2018 - [info] ok.
Wed Mar 14 15:45:09 2018 - [warning] master_ip_failover_script is not defined.
Wed Mar 14 15:45:09 2018 - [warning] shutdown_script is not defined.
Wed Mar 14 15:45:09 2018 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
6,启动MHA;
[root@manager ~]# masterha_manager --conf=/etc/masterha/app1.cnf
Wed Mar 14 15:46:24 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Mar 14 15:46:24 2018 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
Wed Mar 14 15:46:24 2018 - [info] Reading server configurations from /etc/masterha/app1.cnf..
####停止master的mysql之后的状态:
[root@manager ~]# masterha_manager --conf=/etc/masterha/app1.cnf
Wed Mar 14 15:46:24 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Mar 14 15:46:24 2018 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
Wed Mar 14 15:46:24 2018 - [info] Reading server configurations from /etc/masterha/app1.cnf..
Creating /var/tmp if not exists.. ok. #检测到master不存在
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql/, up to master-bin.000003 #发现二进制日志。
Wed Mar 14 15:47:08 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Mar 14 15:47:08 2018 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
Wed Mar 14 15:47:08 2018 - [info] Reading server configurations from /etc/masterha/app1.cnf..
后台运行MHA指令:
[root@manager ~]# nohup master_manager --conf=/etc/masterha/app1.cnf >/etc/masterha/app1/manager.log 2>&1 &
[1] 2832
手动切换从节点为主节点:
[root@manager ~]# masterha_master_switch --master_state=alive --conf=/etc/masterha/app1.cnf --orig_master_is_new_slave --running_updates_limit=3600 --interactive=0
Wed Mar 14 15:52:45 2018 - [info]
Wed Mar 14 15:52:45 2018 - [info] * Phase 5: New master cleanup phase..
Wed Mar 14 15:52:45 2018 - [info]
Wed Mar 14 15:52:45 2018 - [info] 192.168.125.201: Resetting slave info succeeded.
Wed Mar 14 15:52:45 2018 - [info] Switching master to 192.168.125.201(192.168.125.201:3306) completed successfully.
停止MHA manager监控:
[root@manager ~]# masterha_stop --conf=/etc/masterha/app1.cnf