码迷,mamicode.com
首页 > 数据库 > 详细

mysql高可用解决方案MHA架构

时间:2019-05-09 00:46:55      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:pos   events   lan   grant   解决方案   tomat   节点   验证   地址   

1、初始化操作

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

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

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

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

2、账号建立

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

MariaDB [(none)]> grant all on *.* to mhauser@‘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:
[root@mananger ~]# ssh-keygen
[root@mananger ~]# ssh-copy-id 192.168.2.6
[root@mananger ~]# scp -rp .ssh 192.168.2.16:/root/
[root@mananger ~]# scp -rp .ssh 192.168.2.26:/root/
[root@mananger ~]# scp -rp .ssh 192.168.2.36:/root/

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

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

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

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

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

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

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

mananger:
[root@mananger ~]# mkdir /data
[root@mananger ~]# mkdir /etc/mha/
[root@mananger ~]# 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:
[root@mananger ~]# masterha_check_ssh --conf=/etc/mha/app1.conf     #密钥验证检查
[root@mananger ~]# masterha_check_repl --conf=/etc/mha/app1.conf        #复制检查
[root@mananger ~]# masterha_manager --conf=/etc/mha/app1.conf       #前台启动,等着主服务宕机之后,立即提升一个slave为新的master

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

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

9、查看日志

[root@mananger ~]# 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架构

标签:pos   events   lan   grant   解决方案   tomat   节点   验证   地址   

原文地址:https://blog.51cto.com/14230743/2391279

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!