一.Mysql架構的簡介
MHA:
MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,它由日本DeNA公司youshimaton(現就職於Facebook公司)開發,是一套優秀的作爲MySQL高可用性環境下故障切換和主從提升的高可用軟件。在MySQL故障切換過程中,MHA能做到在0~30秒之內自動完成數據庫的故障切換操作,並且在進行故障切換的過程中,MHA能在最大程度上保證數據的一致性,以達到真正意義上的高可用。
該軟件由兩部分組成:MHA Manager(管理節點)和MHA Node(數據節點)。
MHA Manager可以單獨部署在一臺獨立的機器上管理多個master-slave集羣,也可以部署在一臺slave節點上。
MHA Node運行在每臺MySQL服務器上,MHA Manager會定時探測集羣中的master節點,當master出現故障時,它可以自動將最新數據的slave提升爲新的master,然後將所有其他的slave重新指向新的master。整個故障轉移過程對應用程序完全透明。
MHA優點:
在MHA自動故障切換過程中,MHA試圖從宕機的主服務器上保存二進制日誌,最大程度的保證數據的不丟失,但這並不總是可行的。例如,如果主服務器硬件故障或無法通過ssh訪問,MHA沒法保存二進制日誌,只進行故障轉移而丟失了最新的數據。使用MySQL 5.5的半同步複製,可以大大降低數據丟失的風險。MHA可以與半同步複製結合起來。如果只有一個slave已經收到了最新的二進制日誌,MHA可以將最新的二進制日誌應用於其他所有的slave服務器上,因此可以保證所有節點的數據一致性。
MHA處理流程:
從宕機崩潰的master保存二進制日誌事件(binlog events);
識別含有最新更新的slave;
應用差異的中繼日誌(relay log)到其他的slave;
應用從master保存的二進制日誌事件(binlog events);
提升一個slave爲新的master;
使其他的slave連接新的master進行復制;
二.mysql的高可用集羣架構的搭建
實驗環境:
主機名(IP) | 服務 |
---|---|
server1(172.25.16.1) | master |
server2(172.25.16.2) | slave(備master) |
server3(172.25.16.3) | slave |
server4(172.25.16.4) | MHA |
在server1、server2、server3配置基於gtid的主從複製
- 刷新數據庫,server1、server2、server3的操作相同
- 編輯配置文件
server1:
server2:
server3:
- 重啓數據庫,獲取初始密碼
systemctl start mysqld
cat /var/log/mysql.log | grep password
- 配置主從
server1(主):
[root@server1 mysql]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user root@localhost identified by 'Szy+123en';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to repl@'172.25.16.%' identified by 'Szy+123en';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.04 sec)
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set global rpl_semi_sync_master_timeout=10000000000000;
Query OK, 0 rows affected (0.00 sec)
server2(從):
[root@server2 mysql]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user root@localhost identified by 'Szy+123en';
Query OK, 0 rows affected (0.14 sec)
mysql> change master to master_host='172.25.16.1', master_user='repl',master_password='Szy+123en',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.17 sec)
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave io_thread;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.16.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 691
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 898
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 691
Relay_Log_Space: 1107
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: c1d68221-b782-11e9-9293-5254004772f0
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: c1d68221-b782-11e9-9293-5254004772f0:1-2
Executed_Gtid_Set: c1d68221-b782-11e9-9293-5254004772f0:1-2,
ef766a82-b783-11e9-a2e0-52540039676f:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
server3(從):
mysql> alter user root@localhost identified by 'Szy+123en';
Query OK, 0 rows affected (0.08 sec)
mysql> change master to master_host='172.25.16.1', master_user='repl',master_password='Szy+123en',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.15 sec)
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.07 sec)
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave io_thread;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.16.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 691
Relay_Log_File: server3-relay-bin.000002
Relay_Log_Pos: 898
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 691
Relay_Log_Space: 1107
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: c1d68221-b782-11e9-9293-5254004772f0
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: c1d68221-b782-11e9-9293-5254004772f0:1-2
Executed_Gtid_Set: 2988fbc1-b785-11e9-ab3c-52540046c65d:1,
c1d68221-b782-11e9-9293-5254004772f0:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
測試:
在server1寫入數據:
mysql> create database westos;
Query OK, 1 row affected (0.01 sec)
mysql> use westos;
Database changed
mysql> create table userlist(
-> username varchar(10) not null,
-> password varchar(15) not null);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into userlist values ('user1','123')
-> ;
Query OK, 1 row affected (0.02 sec)
mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
1 row in set (0.00 sec)
server2查看
server3查看
三.在server4上安裝管理節點
[root@server4 MHA-7]# ls
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
mha4mysql-manager-0.58.tar.gz
mha4mysql-node-0.58-0.el7.centos.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@server4 MHA-7]# yum install *
2.添加解析
vim /etc/hosts
3.生成server4的ssh密鑰,併發送給server1、server2、server3
ssh-keygen
ssh-copy-id server1
ssh-copy-id server2
ssh-copy-id server3
4.server1、server2、server3安裝節點
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server1:
mha4mysql-node-0.58-0.el7.centos.noarch. 100% 35KB 9.1MB/s 00:00
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2:
mha4mysql-node-0.58-0.el7.centos.noarch. 100% 35KB 9.4MB/s 00:00
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server3:
mha4mysql-node-0.58-0.el7.centos.noarch. 100% 35KB 10.6MB/s 00:00
[root@server1 ~]# yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
5.編輯mha配置文件
[root@server4 ~]# mkdir /etc/masterha
[root@server4 ~]# ls
MHA-7
[root@server4 ~]# cd /etc/masterha/
[root@server4 masterha]# ls
[root@server4 masterha]#
[root@server4 masterha]# vim master.cnf
[server default]
manager_workdir=/etc/masterha
manager_log=/var/log/masterha.log
master_binlog_dir=/etc/masterha
password=Szy+123en
user=root
ping_interval=1
remote_workdir=/tmp
repl_password=Szy+123en
repl_user=repl
ssh_user=root
[server1]
hostname=172.25.16.1
port=3306
[server2]
hostname=172.25.16.2
port=3306
candidate_master=1
check_repl_delay=0
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.16.3
port=3306
no_master=1
6.密鑰互相傳遞
[root@server4 masterha]# scp -r ~/.ssh server1:
id_rsa 100% 1679 5.8KB/s 00:00
id_rsa.pub 100% 394 6.6KB/s 00:00
known_hosts 100% 543 384.3KB/s 00:00
[root@server4 masterha]# scp -r ~/.ssh server2:
id_rsa 100% 1679 1.3MB/s 00:00
id_rsa.pub 100% 394 381.2KB/s 00:00
known_hosts 100% 543 58.7KB/s 00:00
[root@server4 masterha]# scp -r ~/.ssh server3:
id_rsa 100% 1679 1.1MB/s 00:00
id_rsa.pub 100% 394 349.5KB/s 00:00
known_hosts 100% 543 95.2KB/s 00:00
7.檢查ssh是否出錯
[root@server4 masterha]# masterha_check_ssh --conf=/etc/masterha/master.cnf
server1:
mysql> grant all on *.* to root@'%' identified by 'Szy+123en';
server2:
mysql> set global read_only=1;
server3:
mysql> set global read_only=1;
8.查看mysql的複製情況
[root@server4 masterha]# masterha_check_repl --conf=/etc/masterha/master.cnf
四.MHA測試
(1). 手動測試:
1.關閉server1的mysql
[root@server1 ~]# systemctl stop mysqld
2.手動將master節點轉換到server2上
[root@server4 masterha]# masterha_master_switch --master_state=dead --conf=/etc/masterha/master.cnf --dead_master_host=172.25.16.1 --dead_master_port=3306 --new_master_host=172.25.16.2 --new_master_port=3306
輸入yes yes
3.server2查看slave狀態爲空
4.server3查看slave狀態(master的ip轉到server2)
5.打開server1的mysql將slave添加進羣組
[root@server1 ~]# systemctl start mysqld
[root@server1 ~]# mysql -pSzy+123en
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='172.25.16.2', master_user='repl', master_password='Szy+123en', master_auto_position=1;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.16.2
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 3026
Relay_Log_File: server1-relay-bin.000002
Relay_Log_Pos: 942
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3026
Relay_Log_Space: 1151
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: ef766a82-b783-11e9-a2e0-52540039676f
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: ef766a82-b783-11e9-a2e0-52540039676f:1-2
Executed_Gtid_Set: c1d68221-b782-11e9-9293-5254004772f0:1-9,
ef766a82-b783-11e9-a2e0-52540039676f:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
6.手動將matser轉到server1上
[root@server4 masterha]# masterha_master_switch --master_state=alive --conf=/etc/masterha/master.cnf --new_master_host=172.25.16.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=1000
server1:
server2:
server3:
(2)自動轉換
1.在server4下創建一個檢測進程,來創建監控master的進程並查看進程,即執行自動轉換命令
[root@server4 masterha]# nohup masterha_manager --conf=/etc/masterha/master.cnf &> /dev/null &
[1] 1639
[root@server4 masterha]# ps a
PID TTY STAT TIME COMMAND
1108 tty1 Ss+ 0:00 -bash
1131 pts/0 Ss 0:00 -bash
1639 pts/0 S 0:00 perl /usr/bin/masterha_manager --conf=/etc/mas
1663 pts/0 R+ 0:00 ps a
2.關掉server1的mysql
[root@server1 ~]# systemctl stop mysqld
3.此時master服務自動調轉到server2
3.server4中的腳本進程掛掉了
(3)通過腳本控制(通過vip的漂移查看)
1.官網下載mha高可用的manager的安裝包並且解壓
2.編輯master_ip_failover文件(腳本文件一)
my $vip = '172.25.16.100/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
3.編輯master_ip_online_change文件(腳本文件二)
my $vip = '172.25.16.100/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
4.將兩個腳本文件拷貝到/usr/local/bin目錄下,並添加權限
在這裏插入代碼片[root@server4 scripts]# cp master_ip_* /usr/local/bin/
[root@server4 scripts]# chmod +x /usr/local/bin/master_ip_*
[root@server4 scripts]# ll /usr/local/bin/
total 16
-rwxr-xr-x 1 root root 3802 Aug 5 23:58 master_ip_failover
-rwxr-xr-x 1 root root 10041 Aug 5 23:58 master_ip_online_change
給server2添加vip172.25.16.100
手動轉換master到server1上
[root@server4 bin]# masterha_master_switch --conf=/etc/masterha/master.cnf --master_state=alive --new_master_host=172.25.16.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
在server1中查看ip,會查看到vip漂移到了server1上
在server4下創建監控master的進程,當master節點宕機,server4會自動執行/usr/local/bin/的兩個腳本,兩個腳本會自動選擇一個新的節點作爲master
[root@server4 masterha]# nohup masterha_manager --conf=/etc/masterha/master.cnf &> /dev/null &
[root@server4 masterha]# ps a
在server1中關閉mysqld
[root@server1 ~]# systemctl stop mysqld
在server3中查看master,發現此時master變成了server2,vip回到server2