1. 程式人生 > >mysql高可用解決方案MHA架構

mysql高可用解決方案MHA架構

tom 進行 row mysql password tar 2.3 一次性任務 eset

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架構