MySQL 搭建MHA架構部署的步驟
MAH
一:MAH架構介紹
- MHA (Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,它由日本人youshimaton開發,是一套優秀的作為MySQL高可用性環境下故障切換和主從提升的高可用軟件。在MySQL故障切換過程中,MHA能做到0~30秒之內自動完成數據庫的故障切換操作,並且在進行故障切換的過程中,MHA能最大程度上保證數據庫的一致性,以達到真正意義上的高可用。
- MHA由兩部分組成:MHA Manager (管理節點)和MHANode(數據節點)。MHA Manager可以獨立部署在一臺獨立的機器上管理多個Master-Slave集群,也可以部署在一臺Slave上。當Master 出現故障是,它可以自動將最新數據的Slave 提升為新的Master,然後將所有其他的Slave重新指向新的Master。整個故障轉移過程對應用程序是完全透明的。
二:適用場景
目前MHA主要支持一主多從的架構,要搭建MHA,要求一個復制集群必須最少有3臺數據庫服務器,一主二從,即一臺充當Master ,一臺充當備用Master,另一臺充當從庫。出於成本考慮,淘寶在此基礎上進行瞭改造,目前淘寶開發的 TMHA 已經支持一主一從。
三:MHA工作原理
1.從宕機崩潰的 Master保存二進制日志事件(binlog event) ;
2.識別含有最新更新的Slave;
3.應用差異的中繼日志(relay log)到其他Slave;
4.應用從 Master 保存的二進制日志事件;
5.提升—個 Slave為新的Master;
6.使其他的 Slave 連接新的 Master 進行復制;
四:MHA的組成
- Manager工具包
- Node工具包
1:Manager工具包
- masterha_check_ssh:檢查MHA的 SSH配置情況
- masterha_check_repl:檢查MySQL復制狀況
- masterha_manager:啟動MHA
- masterha_check_status:檢測當前MHA運行狀態
- masterha_master_monitor:檢測Master是否宕機
- masterha_master_switch:控制故障轉移(自動或手動)
- masterha_conf_host:添加或刪除配置的server 信息
2:Node工具包
通常由MHA Manager的腳本觸發,無需人工操作
- save_binary_logs:保存和復制Master 的 binlog日志
- apply_diff_relay_logs:識別差異的中級日志時間並將其應用到其他 Slave
- filter_mysqlbinlog:去除不必要的ROOLBACK事件(已經廢棄)
- purge_relay_logs:清除中繼日志(不阻塞SQL線程)
五:MHA 特點
- 自動故障切換過程中,MHA試圖從宕機的主服務器上保存進制日志,最大程度的保證數據不丟失
- 使用半同步復制,可以大大降低數據丟失的風險
- 目前MHA支持一主多從架構,最少三臺服務,即一主兩從
MHA 架構部署
一:拓撲圖
二:數據庫安裝
MySQL 版本使用 5.6.36、cmake 版本使用 2.8.6
1:安裝編譯依賴的環境
[root@master ~]# yum -y install ncurses-devel gcc-c++ perl-Module-Install
2.:安裝 gmake 編譯軟件
[root@master ~]# tar zxvf cmake-2.8.6.tar.gz [root@master ~]# cd cmake-2.8.6 [root@master cmake-2.8.6]# ./configure [root@master cmake-2.8.6]# gmake && gmake install
3:安裝 MySQL 數據庫
[root@master ~]# tar -zxvf mysql-5.6.36.tar.gz [root@master ~]# cd mysql-5.6.36 [root@master mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc [root@master mysql-5.6.36]# make && make install [root@master mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf [root@master mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@master ~]# chmod +x /etc/rc.d/init.d/mysqld [root@master ~]# chkconfig --add mysqld [root@master ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@master ~]# source /etc/profile chown -R mysql.mysql /usr/local/mysql groupadd mysql [root@master ~]# useradd -M -s /sbin/nologin mysql -g mysql [root@master ~]# chown -R mysql.mysql /usr/local/mysql [root@master ~]# mkdir -p /data/mysql [root@master ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
4:修改 Master 的主配置文件/etc/my.cnf 文件
將原來配置全部刪除
[root@master ~]# vi /etc/my.cnf [client] port = 3306 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 log_bin = master-bin log-slave-updates = true sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
另外兩臺 slave 數據庫
三臺服務器的 server-id 不能一樣,其餘一樣正常寫入
server-id = 2 log_bin = master-bin relay-log = relay-log-bin relay-log-index = slave-relay-bin.index
server-id = 3 log_bin = master-bin relay-log = relay-log-bin relay-log-index = slave-relay-bin.index
5:三臺數據庫分別做兩個軟鏈接,軟鏈接是為 HMA 服務的
[root@master ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/ [root@master ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
6:三臺數據庫啟動 MySQL
[root@master ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql & [root@master ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
三:數據庫配置主從同步
登錄數據庫
[root@master ~]# mysql
1:在所有數據庫節點上授權兩個用戶,一個是從庫同步使用,另外一個是 manager 使用
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123'; mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager'; mysql> flush privileges;
2:下面三條授權按理論是不用添加的,但是做案例實驗環境時候通過 MHA 檢查MySQL 主從有報錯,報兩個從庫通過主機名連接不上主庫,所以所有數據庫加上下面的授權。
mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager'; mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager'; mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
3:在 master 主機上查看二進制文件和同步點
mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 608 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
4:在 slave1 和 slave2 分別執行同步
mysql> change master to master_host='20.0.0.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=608 mysql> start slave;
5:查看 IO 和 SQL 線程都是 yes 代表同步是否正常
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 20.0.0.10 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 608 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB:
必須設置兩個從庫為隻讀模式
mysql> set global read_only=1;
6:在 master 主庫插入兩條數據,測試是否同步
mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; Database changed mysql> create table test(id int); Query OK, 0 rows affected (0.13 sec) mysql> insert into test(id) values (1); Query OK, 1 row affected (0.03 sec)
7:在兩個從庫分別查詢如下所示說明主從同步正常
mysql> select * from test_db.test; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec)
四:安裝 MHA 軟件
1:所有服務器上都安裝 MHA 依賴的環境,首先安裝 epel 源(3+1)
[root@master ~]# cd /etc/yum.repos.d/ [root@master yum.repos.d]# ll 總用量 20 drwxr-xr-x. 2 root root 187 10月 10 18:08 backup -rw-r--r--. 1 root root 1458 12月 28 23:07 CentOS7-Base-163.repo -rw-r--r--. 1 root root 951 12月 29 14:52 epel.repo -rw-r--r--. 1 root root 1050 11月 1 04:33 epel.repo.rpmnew -rw-r--r--. 1 root root 1149 11月 1 04:33 epel-testing.repo -rw-r--r--. 1 root root 228 10月 27 18:43 local.repo
三臺數據庫加上一臺 mha-manager
[root@mha-manager ~]# yum install epel-release --nogpgcheck [root@mha-manager ~]# yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-ParalExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
2:在所有服務器上必須先安裝 node 組件 (3+1)
[root@mha-manager ~]# tar zxvf mha4mysql-node-0.57.tar.gz [root@mha-manager ~]# cd mha4mysql-node-0.57 [root@mha-manager mha4mysql-node-0.57]# perl Makefile.PL [root@mha-manager mha4mysql-node-0.57]# make && make install
3:在 mha-manager 上安裝 manager 組件
[root@mha-manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz [root@mha-manager ~]# cd mha4mysql-manager-0.57/ [root@mha-manager mha4mysql-manager-0.57]# perl Makefile.PL [root@mha-manager mha4mysql-manager-0.57]# make && make install
manager 安裝後在/usr/local/bin 下面會生成幾個工具
masterha_check_ssh 檢查 MHA 的 SSH 配置狀況
masterha_check_repl 檢查 MySQL 復制狀況
masterha_manger 啟動 manager的腳本
masterha_check_status 檢測當前 MHA 運行狀態
masterha_master_monitor 檢測 master 是否宕機
masterha_master_switch 控制故障轉移(自動或者手動)
masterha_conf_host 添加或刪除配置的 server 信息
masterha_stop 關閉manager
node 安裝後也會在/usr/local/bin 下面會生成幾個腳本
save_binary_logs 保存和復制 master 的二進制日志
apply_diff_relay_logs 識別差異的中繼日志事件並將其差異的事件應用於其他的 slave
filter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用這個工具)
purge_relay_logs 清除中繼日志(不會阻塞 SQL 線程)
五:配置無密碼認證
1:在 manager 上配置到所有節點的無密碼認證
(1)生成密鑰
[root@mha-manager ~]# ssh-keygen -t rsa # 一直回車
(2)生成密鑰之後發送給其他3個數據庫
[root@mha-manager ~]# ssh-copy-id 20.0.0.10 # 輸入:yes 密碼:123456 [root@mha-manager ~]# ssh-copy-id 20.0.0.11 [root@mha-manager ~]# ssh-copy-id 20.0.0.12
(3)登錄測試
[root@mha-manager ~]# ssh [email protected] Last login: Tue Dec 29 14:52:09 2020 from 20.0.0.1 [root@master ~]# exit 登出 Connection to 20.0.0.10 closed. [root@mha-manager ~]# ssh [email protected] Last login: Tue Dec 29 13:20:07 2020 from 20.0.0.1 [root@slave1 ~]# exit 登出 Connection to 20.0.0.11 closed. [root@mha-manager ~]# ssh [email protected] Last login: Tue Oct 27 19:45:24 2020 from 20.0.0.1 [root@slave2 ~]# exit 登出 Connection to 20.0.0.12 closed.
2:在 master 上配置到數據庫節點的無密碼認證
(1)生成密鑰
[root@master ~]# ssh-keygen -t rsa
(2)生成密鑰之後發送給其他2個數據庫
[root@master ~]# ssh-copy-id 20.0.0.11 [root@master ~]# ssh-copy-id 20.0.0.12
(3)登錄測試
[root@master ~]# ssh [email protected] Last login: Tue Dec 29 16:40:06 2020 from 20.0.0.13 [root@slave1 ~]# exit 登出 Connection to 20.0.0.11 closed. [root@master ~]# ssh [email protected] Last login: Tue Oct 27 23:05:20 2020 from 20.0.0.13 [root@slave2 ~]# exit 登出 Connection to 20.0.0.12 closed.
3:在 slave1 上配置到數據庫節點的無密碼認證
(1)生成密鑰
[root@slave1 ~]# ssh-keygen -t rsa
(2)生成密鑰之後發送給其他2個數據庫
[root@slave1 ~]# ssh-copy-id 20.0.0.10 [root@slave1 ~]# ssh-copy-id 20.0.0.12
(3)登錄測試
[root@slave1 ~]# ssh [email protected] Last login: Tue Dec 29 16:39:55 2020 from 20.0.0.13 [root@master ~]# exit 登出 Connection to 20.0.0.10 closed. [root@slave1 ~]# ssh [email protected] Last login: Tue Oct 27 23:14:06 2020 from 20.0.0.10 [root@slave2 ~]# exit 登出 Connection to 20.0.0.12 closed.
4:在 slave2 上配置到數據庫節點的無密碼認證
(1)生成密鑰
[root@slave2 ~]# ssh-keygen -t rsa
(2)生成密鑰之後發送給其他2個數據庫
[root@slave2 ~]# ssh-copy-id 20.0.0.10 [root@slave2 ~]# ssh-copy-id 20.0.0.11
(3)登錄測試
[root@slave2 ~]# ssh [email protected] Last login: Tue Dec 29 16:59:43 2020 from 20.0.0.11 [root@master ~]# exit 登出 Connection to 20.0.0.10 closed. [root@slave2 ~]# ssh [email protected] Last login: Tue Dec 29 16:48:51 2020 from 20.0.0.10 [root@slave1 ~]# exit 登出 Connection to 20.0.0.11 closed.
六:配置 MHA
1:在 manager 節點上復制相關腳本到/usr/local/bin 目錄
(1)拷貝
[root@mha-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/
(2)拷貝後會有四個執行文件
master_ip_failover #自動切換時 VIP 管理的腳本
master_ip_online_change #在線切換時 vip 的管理
power_manager #故障發生後關閉主機的腳本
send_report #因故障切換後發送報警的腳本
(3)復制上述的自動切換時 VIP 管理的腳本到/usr/local/bin 目錄,這裡使用腳本管理 VIP
[root@mha-manager scripts]# cp master_ip_failover /usr/local/bin/ [root@mha-manager scripts]# cd .. [root@mha-manager bin]# ll 總用量 88
2:修改自動切換腳本
[root@mha-manager ~]# vi /usr/local/bin/master_ip_failover # 刪除裡面所有內容 #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); #############################添加內容部分######################################### my $vip = '20.0.0.200'; my $brdc = '20.0.0.255'; my $ifdev = 'ens33'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; my $exit_code = 0; #my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;"; #my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key"; ################################################################################## GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, ); exit &main(); sub main { print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; if ( $command eq "stop" || $command eq "stopssh" ) { my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host \n"; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host \n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK \n"; exit 0; } else { &usage(); exit 1; } } sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } # A simple system call that disable the VIP on the old_master sub stop_vip() { `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
3:創建 MHA 軟件目錄並拷貝配置文件
[root@mha-manager ~]# mkdir /etc/mha [root@mha-manager ~]# cp mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/mha [root@mha-manager ~]# vi /etc/mha/app1.cnf [server default] manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/manager.log master_binlog_dir=/usr/local/mysql/data master_ip_failover_script=/usr/local/bin/master_ip_failover master_ip_online_change_script=/usr/local/bin/master_ip_online_change password=manager user=mha ping_interval=1 remote_workdir=/tmp repl_password=123 repl_user=myslave secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.11 -s 20.0.0.12 shutdown_script="" ssh_user=root [server1] hostname=20.0.0.10 port=3306 [server2] hostname=20.0.0.11 port=3306 candidate_master=1 check_repl_delay=0 [server3] hostname=20.0.0.12 port=3306
七:健康檢查
1:測試 ssh 無密碼認證,如果正常最後會輸出 successfully
[root@mha-manager ~]# masterha_check_ssh --conf=<server_config_file> must be set. [root@mha-manager ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf Tue Dec 29 20:19:16 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Dec 29 20:19:16 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf.. Tue Dec 29 20:19:16 2020 - [info] Reading server configuration from /etc/mha/app1.cnf.. Tue Dec 29 20:19:16 2020 - [info] Starting SSH connection tests.. Tue Dec 29 20:19:17 2020 - [debug] Tue Dec 29 20:19:16 2020 - [debug] Connecting via SSH from [email protected](20.0.0.10:22) to [email protected](20.0.0.11:22).. Tue Dec 29 20:19:16 2020 - [debug] ok. Tue Dec 29 20:19:16 2020 - [debug] Connecting via SSH from [email protected](20.0.0.10:22) to [email protected](20.0.0.12:22).. Tue Dec 29 20:19:17 2020 - [debug] ok. Tue Dec 29 20:19:18 2020 - [debug] Tue Dec 29 20:19:17 2020 - [debug] Connecting via SSH from [email protected](20.0.0.12:22) to [email protected](20.0.0.10:22).. Tue Dec 29 20:19:17 2020 - [debug] ok. Tue Dec 29 20:19:17 2020 - [debug] Connecting via SSH from [email protected](20.0.0.12:22) to [email protected](20.0.0.11:22).. Tue Dec 29 20:19:18 2020 - [debug] ok. Tue Dec 29 20:19:18 2020 - [debug] Tue Dec 29 20:19:16 2020 - [debug] Connecting via SSH from [email protected](20.0.0.11:22) to [email protected](20.0.0.10:22).. Tue Dec 29 20:19:17 2020 - [debug] ok. Tue Dec 29 20:19:17 2020 - [debug] Connecting via SSH from [email protected](20.0.0.11:22) to [email protected](20.0.0.12:22).. Tue Dec 29 20:19:17 2020 - [debug] ok. Tue Dec 29 20:19:18 2020 - [info] All SSH connection tests passed successfully.
2:測試 MySQL 主從連接情況,最後出現 MySQL Replication Health is OK 字樣說明
[root@mha-manager ~]# masterha_check_repl --conf=/etc/mha/app1.cnf Tue Dec 29 20:30:29 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Dec 29 20:30:29 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf.. Tue Dec 29 20:30:29 2020 - [info] Reading server configuration from /etc/mha/app1.cnf.. Tue Dec 29 20:30:29 2020 - [info] MHA::MasterMonitor version 0.57. Tue Dec 29 20:30:30 2020 - [info] GTID failover mode = 0 Tue Dec 29 20:30:30 2020 - [info] Dead Servers: Tue Dec 29 20:30:30 2020 - [info] Alive Servers: Tue Dec 29 20:30:30 2020 - [info] 20.0.0.10(20.0.0.10:3306) Tue Dec 29 20:30:30 2020 - [info] 20.0.0.11(20.0.0.11:3306) Tue Dec 29 20:30:30 2020 - [info] 20.0.0.12(20.0.0.12:3306) Tue Dec 29 20:30:30 2020 - [info] Alive Slaves: Tue Dec 29 20:30:30 2020 - [info] 20.0.0.11(20.0.0.11:3306) Version=5.6.36-log (oldest major version between slaves) log-bin:enabled .......省略 Checking the Status of the script.. OK Tue Dec 29 20:30:55 2020 - [info] OK. Tue Dec 29 20:30:55 2020 - [warning] shutdown_script is not defined. Tue Dec 29 20:30:55 2020 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
八:查看 master1 的 VIP 地址
查看 20.0.0.200 是否存在
這個 VIP 地址不會因為manager 節點停止 MHA 服務而消失
第一次啟動mha,主庫上並不會主動的生成vip地址,需要手動開啟
[root@master ~]# ifconfig ens33:1 20.0.0.200/24 up [root@master ~]# ip addr 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:8d:e2:af brd ff:ff:ff:ff:ff:ff inet 20.0.0.10/24 brd 20.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet 20.0.0.200/24 brd 20.0.0.255 scope global secondary ens33:1 valid_lft forever preferred_lft forever inet6 fe80::a6c1:f3d4:160:102a/64 scope link valid_lft forever preferred_lft forever
九:啟動 MHA 並查看狀態
[root@mha-manager ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & [1] 57152 [root@mha-manager ~]# masterha_check_status --conf=/etc/mha/app1.cnf app1 (pid:57152) is running(0:PING_OK), master:20.0.0.10
故障模擬與修復
一:故障模擬
1:將 master 主服務器 down 掉
[root@master ~]# pkill mysqld
2:查看日志信息
[root@mha-manager ~]# cat /var/log/masterha/app1/manager.log master 20.0.0.10(20.0.0.10:3306) is down! # 20.0.0.10 以停掉 Check MHA Manager logs at mha-manager:/var/log/masterha/app1/manager.log for details. Started automated(non-interactive) failover. Invalidated master IP address on 20.0.0.10(20.0.0.10:3306) The latest slave 20.0.0.11(20.0.0.11:3306) has all relay logs for recovery. Selected 20.0.0.11(20.0.0.11:3306) as a new master. # 20.0.0.11 成為主服務器 20.0.0.11(20.0.0.11:3306): OK: Applying all logs succeeded. 20.0.0.11(20.0.0.11:3306): OK: Activated master IP address. 20.0.0.12(20.0.0.12:3306): This host has the latest relay log events. Generating relay diff files from the latest slave succeeded.
3:查看虛擬地址
虛擬地址已到 20.0.0.11 上面
[root@slave1 ~]# ip addr 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:49:77:39 brd ff:ff:ff:ff:ff:ff inet 20.0.0.11/24 brd 20.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet 20.0.0.200/8 brd 20.255.255.255 scope global ens33:1 valid_lft forever preferred_lft forever inet6 fe80::5cbb:1621:4281:3b24/64 scope link valid_lft forever preferred_lft forever
4:查看主從狀態
查看主服務器的二進制文件
[root@slave1 ~]# mysql mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000003 | 120 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
查看從2的狀態
[root@slave2 ~]# mysql mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 20.0.0.11 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 120 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB:
二:故障修復
1:開啟 down 掉的數據庫
[root@master ~]# systemctl start mysqld [root@master ~]# systemctl status mysqld ● mysqld.service - LSB: start and stop MySQL Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled) Active: active (running) since 二 2020-12-29 21:50:03 CST; 25s ago Docs: man:systemd-sysv-generator(8) Process: 977 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS) CGroup: /system.slice/mysqld.service ├─1026 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-fi... └─1358 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/m
2:down 掉的數據庫進行主從復制
主從復制
[root@master ~]# mysql mysql> change master to master_host='20.0.0.11',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120; Query OK, 0 rows affected, 2 warnings (0.01 sec) # 20.0.0.11 是主服務器 down 掉之後成為的主服務器 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: 20.0.0.11 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 120 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB:
3:修改 mha 配置文件
[root@mha-manager ~]# vi /etc/mha/app1.cnf secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.10 -s 20.0.0.12 # 由於 20.0.0.11 成為主服務器,所以要添加 20.0.0.10 和 20.0.0.12 為從服務器 [server1] hostname=20.0.0.10 candidate_master=1 check_repl_delay=0 port=3306 [server2] hostname=20.0.0.11 port=3306 # 由於 20.0.0.10 down 掉,所以 server1 文件自動刪除,重新添加 server1,並設為備選主服務器,server2 修改
4:進入數據庫重新授權
[root@master ~]# mysql mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
5:再次啟動 mha
[root@mha-manager ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & [1] 58927 [root@mha-manager ~]# masterha_check_status --conf=/etc/mha/app1.cnf app1 (pid:58927) is running(0:PING_OK), master:20.0.0.11
6:再次查看日志
[root@mha-manager ~]# cat /var/log/masterha/app1/manager.log ...... Tue Dec 29 22:16:53 2020 - [info] Dead Servers: # 停掉的服務 Tue Dec 29 22:16:53 2020 - [info] Alive Servers: # 存活的服務 Tue Dec 29 22:16:53 2020 - [info] 20.0.0.10(20.0.0.10:3306) Tue Dec 29 22:16:53 2020 - [info] 20.0.0.11(20.0.0.11:3306) Tue Dec 29 22:16:53 2020 - [info] 20.0.0.12(20.0.0.12:3306) .......
7:主數據庫寫入數據同步並查看
其他數據庫都可以查到
mysql> create database ooo; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | ooo | | performance_schema | | test | | test_db | +--------------------+ 6 rows in set (0.00 sec)
到此這篇關於MySQL 搭建MHA架構部署的步驟的文章就介紹到這瞭,更多相關MySQL 搭建MHA架構部署內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- MySQL之MHA高可用配置及故障切換實現詳細部署步驟
- MySQL高可用架構之MHA架構全解
- Docker部署mysql一主一從的操作方法
- MySQL 配置主從復制實踐分享
- mysql主從復制的實現步驟