场景: 一主多从复制架构中,主服务器宕机,自动在从服务器中选择一个与主服务器复制数据最接近的提升为主;
实现软件工具MHA:
对主节点进行监控,可实现自动故障转移至其它从节点;通过提升某一从节点为新的主节点,基于主从复制实现,还需要客户端配合实现,目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器
项目官网:
https://code.google.com/archive/p/mysql-master-ha/
1.安装manger节点和node节点
[root@manager ~]#ll /data/
-rw-r--r-- 1 root root 87119 Nov 10 2017 mha4mysql-manager-0.56-0.el6.noarch.rpm
-rw-r--r-- 1 root root 36326 Nov 10 2017 mha4mysql-node-0.56-0.el6.noarch.rpm
[root@manager data]#yum install mha*.rpm
2.配置基于Key的SSHkey相互访问
[root@manager data]#ssh-keygen
[root@manager ~]#ls -a .ssh/
. .. id_rsa id_rsa.pub
[root@manager ~]#ssh-copy-id 192.168.37.100
[root@manager ~]#ls .ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
[root@manager ~]#scp -r .ssh/ 192.168.37.102:/root
[root@manager ~]#scp -r .ssh/ 192.168.37.103:/root
[root@manager ~]#scp -r .ssh/ 192.168.37.20:/root
3.配置MHA配置文件:配置文件位置可随便指定
[root@manager ~]#mkdir /data/mha
[root@manager ~]#vim /data/mha/app1.cnf
[server default]
user=mhauser
password=centos
manager_workdir=/data/mastermha/app1
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=replslave
repl_password=123456
ping_interval=1
[server1]
hostname=192.168.37.102
candidate_master=1
[server2]
hostname=192.168.37.103
candidate_master=1
[server3]
hostname=192.168.37.20
[root@manager ~]#masterha_check_ssh --conf=/data/mha/app1.cnf
Wed Jul 17 10:45:33 2019 - [info] All SSH connection tests passed successfully.
[root@manager ~]#masterha_check_repl --conf=/data/mha/app1.cnf
MySQL Replication Health is OK.
4.开启MHA监控服务
#应通过如screen放入后台执行,防止因远程连接窗口关闭导致进程关闭!
[root@manager ~]#masterha_manager --conf=/data/mha/app1.cnf
1.启用主从复制功能,修改my.cnf
[root@master data]#vim /etc/my.cnf
[mysqld]
server-id=102
log-bin
skip_name_resolve=1
2.登录数据库,创建复制用户以及管理节点管理账户
MariaDB [(none)]> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin.000001 | 329 |
+-------------------+-----------+
MariaDB [(none)]> grant replication slave on *.* to replslave@'192.168.37.%' identified by '123456';
MariaDB [(none)]> grant all on *.* to mhauser@'192.168.37.%' identified by 'centos';
3.安装mha的node节点
[root@master data]#yum install mha*.rpm
1.启用主从复制功能,修改my.cnf
[root@slave1 ~]#vim /etc/my.cnf
[mysqld]
server-id=103
log-bin
read-only
relay_log_purge=0
skip_name_resolve=1
[root@slave1 ~]#systemctl restart mariadb
2.登录数据库,指定master主节点,并启动服务
MariaDB [(none)]> change master to master_host='192.168.37.102',master_user='replslave',master_password='123456',master_port=3306,master_log_file='master-bin.000001',master_log_pos=329;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
3.安装mha的node节点
[root@master data]#yum install mha*.rpm
1.启用主从复制功能,修改my.cnf
[root@slave2 ~]#vim /etc/my.cnf
[mysqld]
server-id=20
log-bin
read-only
relay_log_purge=0
skip_name_resolve=1
[root@slave1 ~]#systemctl restart mariadb
2.登录数据库,指定master主节点,并启动服务
MariaDB [(none)]> change master to master_host='192.168.37.102',master_user='replslave',master_password='123456',master_port=3306,master_log_file='master-bin.000001',master_log_pos=329;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
3.安装mha的node节点
[root@master data]#yum install mha*.rpm
(一)测试原master服务宕机,MHA提升新主节点过程
1.开启manager管理节点日志跟踪,持续查看
[root@manager ~]#tail -f /data/mastermha/app1/manager.log
2.master主服务器写入数据
MariaDB [none] use test
create table testlog (id int auto_increment primary key,name char(10),age int default 20);
delimiter $$
create procedure sp_testlog()
begin
declare i int;
set i = 1;
while i <= 100000
do insert into testlog(name,age) values (concat('wang',i),i);
set i = i +1;
end while;
end$$
delimiter ;
MariaDB [test]> call sp_testlog;
3.主服务器写入数据过程中,停掉主服务器的数据库服务
[root@master ~]#systemctl stop mariadb
(二)测试提升新主前后,数据库复制情况
1.查看主节点二进制日志
[root@master ~]#mysqlbinlog /var/lib/mysql/master-bin.000001 -vvv