mysql高可用解决方案MHA架构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql高可用解决方案MHA架构相关的知识,希望对你有一定的参考价值。

1、初始化操作

MHA:192.168.2.6
master:192.168.2.16
slave1:192.168.2.26
slave2:192.168.2.36

master:
[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=16
skip_name_resolve       #跳过ip地址反向解析
log_bin                 #二进制日志
[[email protected] ~]# systemctl start mariadb

slavel:
[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=26
read-only
log-bin                     
relay_log_purge=0           #不清理中继日志
skip_name_resolve
[[email protected] ~]# systemctl start mariadb

slave2:
[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=36
read-only
log-bin
relay_log_purge=0
skip_name_resolve
[[email protected] ~]# systemctl start mariadb

2、账号建立

master:
MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.2.%‘ identified by ‘123456‘;      #复制账号

MariaDB [(none)]> grant all on *.* to [email protected]‘192.168.2.%‘ identified by ‘123456‘;     #管理账号

slavel:
MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST=‘192.168.2.16‘,
    ->   MASTER_USER=‘repluser‘,
    ->   MASTER_PASSWORD=‘123456‘,
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE=‘mariadb-bin.000001‘,
    ->   MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.06 sec)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G

slave2:
MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST=‘192.168.2.16‘,
    ->   MASTER_USER=‘repluser‘,
    ->   MASTER_PASSWORD=‘123456‘,
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE=‘mariadb-bin.000001‘,
    ->   MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G

3、测试主从复制是否成功

master:
MariaDB [(none)]> create database db1;

slave1:
MariaDB [(none)]> show databases;

slave2:
MariaDB [(none)]> show databases;

4、基于key验证,并测试互相正常通信

mananger:
[[email protected] ~]# ssh-keygen
[[email protected] ~]# ssh-copy-id 192.168.2.6
[[email protected] ~]# scp -rp .ssh 192.168.2.16:/root/
[[email protected] ~]# scp -rp .ssh 192.168.2.26:/root/
[[email protected] ~]# scp -rp .ssh 192.168.2.36:/root/

[[email protected] ~]# ssh 192.168.2.16
Last login: Tue May  7 22:25:19 2019 from 192.168.2.6
[[email protected] ~]# ssh 192.168.2.26
Last login: Wed May  8 05:11:02 2019 from 192.168.2.1

5、下载安装MHA(有依赖性需要配置epel)

mananger:
[[email protected] ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56-0.el6.noarch.rpm
[[email protected] ~]# yum -y install *.rpm

master:
[[email protected] ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm
[[email protected] ~]# yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm

slave1:
[[email protected] ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm
[[email protected] ~]# yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm 

slave2:
[[email protected] ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm
[[email protected] ~]# yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm

6、在管理节点建立配置文件

mananger:
[[email protected] ~]# mkdir /data
[[email protected] ~]# mkdir /etc/mha/
[[email protected] ~]# vim /etc/mha/app1.conf
[server default]
user=mhauser
password=123456
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=123456
ping_interval=1
[server1]
hostname=192.168.2.16
candidate_master=1
[server2]
hostname=192.168.2.26
candidate_master=1
[server3]
hostname=192.168.2.36
candidate_master=1

7、在管理节点,MHA验证和启动

mananger:
[[email protected] ~]# masterha_check_ssh --conf=/etc/mha/app1.conf     #密钥验证检查
[[email protected] ~]# masterha_check_repl --conf=/etc/mha/app1.conf        #复制检查
[[email protected] ~]# masterha_manager --conf=/etc/mha/app1.conf       #前台启动,等着主服务宕机之后,立即提升一个slave为新的master

8、测试:主服务器写入大量数据,然后突然宕机

master:
[[email protected] ~]# mysql db1 < testlog.sql    
[[email protected] ~]# kill -9 11442  

9、查看日志

[[email protected] ~]# cat /data/mastermha/app1/manager.log 
----- Failover Report -----

app1: MySQL Master failover 192.168.2.16(192.168.2.16:3306) to 192.168.2.26(192.168.2.26:3306) succeeded

Master 192.168.2.16(192.168.2.16:3306) is down!

Check MHA Manager logs at mananger:/data/mastermha/app1/manager.log for details.

Started automated(non-interactive) failover.
The latest slave 192.168.2.26(192.168.2.26:3306) has all relay logs for recovery.
Selected 192.168.2.26(192.168.2.26:3306) as a new master.
192.168.2.26(192.168.2.26:3306): OK: Applying all logs succeeded.
192.168.2.36(192.168.2.36:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.2.36(192.168.2.36:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.2.26(192.168.2.26:3306)
192.168.2.26(192.168.2.26:3306): Resetting slave info succeeded.
Master failover to 192.168.2.26(192.168.2.26:3306) completed successfully.

slave1、slave2查看状态
MariaDB [(none)]> show slave status\G

注意:MHA一次性任务,所以将以提升的master的配置文件read-only去掉,否则机器重启后不能写入数据

10、最终验证

新的主服务进行数据操作,看是否可以同步到从服务

以上是关于mysql高可用解决方案MHA架构的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL高可用集群架构-MHA架构

MySQL高可用集群架构——MHA架构

MySQL高可用架构之MHA

MySQL高可用架构之MHA

Mysql MHA高可用集群架构

MySQL高可用架构之MHA