MySQL MHA高可用

首先準備三臺虛擬機,安裝MySQL,首先備份主庫並恢復到兩個從庫

個人數據庫5.7版本,系統CentOS7,系統優化關閉防火牆和SELinux,主節點IP10.0.0.11,兩個從節點IP10.0.0.12和10.0.0.13

構建主從

開啓binlog

主節點執行
從節點binlog可以暫時先不開啓,主節點一定要,從節點需要添加server-id

vim /etc/my.cnf

#server-id不能重複,主節點ID最大.
server_id=5
#binlog存放位置
log_bin=/data/binlog/mysql-bin
#binlog模式
binlog_format=row

創建目錄並授權

mkdir -p /data/binlog
chown -R mysql. /data

重啓數據庫生效

systemctl restart mysqld

如果重啓報錯,可以檢查下是否是selinux或者防火牆問題

報錯:
2019-11-23T14:12:00.003065Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-11-23T14:12:00.005669Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.28-log) starting as process 2326 ...
mysqld: File '/data/binlog/mysql-bin.index' not found (Errcode: 13 - Permission denied)
2019-11-23T14:12:00.008251Z 0 [ERROR] Aborting


#檢查防火牆
systemctl status firewalld
#關閉並取消開機自啓動
systemctl stop firewalld
systemctl disable firewalld
#檢查SELinux
getenforce
#關閉SELinux
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config 

構建主從
登陸主庫,創建用戶:

grant replication slave on *.* to repl@'10.0.0.%' identified by 'Qaz@123456';

登陸從節點,修改/etc/my.cnf文件,添加server_id:

MySQL02 ID改爲6,MySQL03 ID改爲7

登陸數據庫,執行:

CHANGE MASTER TO
MASTER_HOST='10.0.0.11',
MASTER_USER='repl',
MASTER_PASSWORD='Qaz@123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=0,
MASTER_CONNECT_RETRY=10;

MASTER_HOST=‘主庫IP地址’,
MASTER_USER=‘主庫創建的用於複製的用戶名’,
MASTER_PASSWORD=‘創建用戶的密碼’,
MASTER_PORT=‘主庫MySQL端口號’,
MASTER_LOG_FILE=‘binlog執行到的位置’,
MASTER_LOG_POS=‘執行binlog的位置,0默認自動匹配’,
MASTER_CONNECT_RETRY=‘連接不上主庫自動嘗試的秒數’;

查看狀態

從節點數據庫執行:

start slave;
show slave status \G

檢查線程狀態
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
我是安裝了一臺,複製了兩臺,所以UUID都是相同的,然後IO線程一開始是NO,解決方法:

vim /var/lib/mysql/auto.cnf
#修改MySQL的UUID,任意修改一位使其不重複即可

主庫創建庫測試

mysql> create database test charset=utf8mb4;
Query OK, 1 row affected (0.00 sec)

mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

從庫檢查:

mysql> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)

都出現了test庫,成功

MHA

配置個節點互信

主節點操作:

rm -rf /root/.ssh 
ssh-keygen
cd /root/.ssh 
mv id_rsa.pub authorized_keys
scp  -r  /root/.ssh  10.0.0.52:/root 
scp  -r  /root/.ssh  10.0.0.53:/root

測試

#各節點都要執行(ssh連接取時間)
ssh 10.0.0.11 date
ssh 10.0.0.12 date
ssh 10.0.0.13 date
下載
mha官網:https://code.google.com/archive/p/mysql-master-ha/
github下載地址:https://github.com/yoshinorim/mha4mysql-manager/wiki/Downloads

或者點擊:個人博客版

#下載解壓
[root@mysql-master ~]# unzip MHA.zip
[root@mysql-master ~]# ls
anaconda-ks.cfg  mha4mysql-manager-0.56-0.el6.noarch.rpm  mha4mysql-node-0.56-0.el6.noarch.rpm  MHA.zip

所有節點安裝:

rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

主庫創建所需用戶

grant all privileges on *.* to mha@'10.0.0.%' identified by 'Qaz@123456';

選擇一臺從節點或一臺獨立服務器作爲MHA調度,安裝依賴(再次安裝在了10.0.0.12)

yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
#安裝MHA主
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

創建目錄

mkdir -p /etc/mha && mkdir -p /var/log/mha/app1 && mkdir -p /data/binlog && chown -R mysql. /data/binlog

從節點開啓binlog日誌

vim /etc/my.cnf

log_bin=/data/binlog/mysql-bin
binlog_format=row

編寫配置文件

vim /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager        
manager_workdir=/var/log/mha/app1            
master_binlog_dir=/data/binlog       
user=mha                                   
password=Qaz@123456                              
ping_interval=2
repl_password=Qaz@123456
repl_user=repl
ssh_user=root                               
[server1]                                   
hostname=10.0.0.11
port=3306                                  
[server2]            
hostname=10.0.0.12
port=3306
[server3]
hostname=10.0.0.13
port=3306

MHA節點執行(檢查各節點互信):

[root@mysql2 ~]# masterha_check_ssh  --conf=/etc/mha/app1.cnf
Sun Nov 24 10:36:34 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Nov 24 10:36:34 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Sun Nov 24 10:36:34 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..
Sun Nov 24 10:36:34 2019 - [info] Starting SSH connection tests..
Sun Nov 24 10:36:35 2019 - [debug] 
Sun Nov 24 10:36:34 2019 - [debug]  Connecting via SSH from [email protected](10.0.0.11:22) to [email protected](10.0.0.12:22)..
Sun Nov 24 10:36:34 2019 - [debug]   ok.
Sun Nov 24 10:36:34 2019 - [debug]  Connecting via SSH from [email protected](10.0.0.11:22) to [email protected](10.0.0.13:22)..
Sun Nov 24 10:36:35 2019 - [debug]   ok.
Sun Nov 24 10:36:36 2019 - [debug] 
Sun Nov 24 10:36:35 2019 - [debug]  Connecting via SSH from [email protected](10.0.0.13:22) to [email protected](10.0.0.11:22)..
Sun Nov 24 10:36:35 2019 - [debug]   ok.
Sun Nov 24 10:36:35 2019 - [debug]  Connecting via SSH from [email protected](10.0.0.13:22) to [email protected](10.0.0.12:22)..
Sun Nov 24 10:36:36 2019 - [debug]   ok.
Sun Nov 24 10:36:36 2019 - [debug] 
Sun Nov 24 10:36:34 2019 - [debug]  Connecting via SSH from [email protected](10.0.0.12:22) to [email protected](10.0.0.11:22)..
Sun Nov 24 10:36:35 2019 - [debug]   ok.
Sun Nov 24 10:36:35 2019 - [debug]  Connecting via SSH from [email protected](10.0.0.12:22) to [email protected](10.0.0.13:22)..
Sun Nov 24 10:36:36 2019 - [debug]   ok.
Sun Nov 24 10:36:36 2019 - [info] All SSH connection tests passed successfully.
[root@mysql2 ~]# 

檢查狀態

[root@mysql2 ~]# masterha_check_repl  --conf=/etc/mha/app1.cnf 
Sun Nov 24 10:45:15 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Nov 24 10:45:15 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Sun Nov 24 10:45:15 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..
Sun Nov 24 10:45:15 2019 - [info] MHA::MasterMonitor version 0.56.
Sun Nov 24 10:45:16 2019 - [info] GTID failover mode = 0
Sun Nov 24 10:45:16 2019 - [info] Dead Servers:
Sun Nov 24 10:45:16 2019 - [info] Alive Servers:
Sun Nov 24 10:45:16 2019 - [info]   10.0.0.11(10.0.0.11:3306)
Sun Nov 24 10:45:16 2019 - [info]   10.0.0.12(10.0.0.12:3306)
Sun Nov 24 10:45:16 2019 - [info]   10.0.0.13(10.0.0.13:3306)
Sun Nov 24 10:45:16 2019 - [info] Alive Slaves:
Sun Nov 24 10:45:16 2019 - [info]   10.0.0.12(10.0.0.12:3306)  Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Sun Nov 24 10:45:16 2019 - [info]     Replicating from 10.0.0.11(10.0.0.11:3306)
Sun Nov 24 10:45:16 2019 - [info]   10.0.0.13(10.0.0.13:3306)  Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Sun Nov 24 10:45:16 2019 - [info]     Replicating from 10.0.0.11(10.0.0.11:3306)
Sun Nov 24 10:45:16 2019 - [info] Current Alive Master: 10.0.0.11(10.0.0.11:3306)
Sun Nov 24 10:45:16 2019 - [info] Checking slave configurations..
Sun Nov 24 10:45:16 2019 - [info]  read_only=1 is not set on slave 10.0.0.12(10.0.0.12:3306).
Sun Nov 24 10:45:16 2019 - [warning]  relay_log_purge=0 is not set on slave 10.0.0.12(10.0.0.12:3306).
Sun Nov 24 10:45:16 2019 - [info]  read_only=1 is not set on slave 10.0.0.13(10.0.0.13:3306).
Sun Nov 24 10:45:16 2019 - [warning]  relay_log_purge=0 is not set on slave 10.0.0.13(10.0.0.13:3306).
Sun Nov 24 10:45:16 2019 - [info] Checking replication filtering settings..
Sun Nov 24 10:45:16 2019 - [info]  binlog_do_db= , binlog_ignore_db= 
Sun Nov 24 10:45:16 2019 - [info]  Replication filtering check ok.
Sun Nov 24 10:45:16 2019 - [info] GTID (with auto-pos) is not supported
Sun Nov 24 10:45:16 2019 - [info] Starting SSH connection tests..
Sun Nov 24 10:45:18 2019 - [info] All SSH connection tests passed successfully.
Sun Nov 24 10:45:18 2019 - [info] Checking MHA Node version..
Sun Nov 24 10:45:19 2019 - [info]  Version check ok.
Sun Nov 24 10:45:19 2019 - [info] Checking SSH publickey authentication settings on the current master..
Sun Nov 24 10:45:19 2019 - [info] HealthCheck: SSH to 10.0.0.11 is reachable.
Sun Nov 24 10:45:19 2019 - [info] Master MHA Node version is 0.56.
Sun Nov 24 10:45:19 2019 - [info] Checking recovery script configurations on 10.0.0.11(10.0.0.11:3306)..
Sun Nov 24 10:45:19 2019 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/binlog --output_file=/var/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000004 
Sun Nov 24 10:45:19 2019 - [info]   Connecting to [email protected](10.0.0.11:22).. 
  Creating /var/tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /data/binlog, up to mysql-bin.000004
Sun Nov 24 10:45:20 2019 - [info] Binlog setting check done.
Sun Nov 24 10:45:20 2019 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Sun Nov 24 10:45:20 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=10.0.0.12 --slave_ip=10.0.0.12 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.28-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Sun Nov 24 10:45:20 2019 - [info]   Connecting to [email protected](10.0.0.12:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mysql2-relay-bin.000009
    Temporary relay log file is /var/lib/mysql/mysql2-relay-bin.000009
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Sun Nov 24 10:45:21 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=10.0.0.13 --slave_ip=10.0.0.13 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.28-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Sun Nov 24 10:45:21 2019 - [info]   Connecting to [email protected](10.0.0.13:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mysql3-relay-bin.000010
    Temporary relay log file is /var/lib/mysql/mysql3-relay-bin.000010
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Sun Nov 24 10:45:21 2019 - [info] Slaves settings check done.
Sun Nov 24 10:45:21 2019 - [info] 
10.0.0.11(10.0.0.11:3306) (current master)
 +--10.0.0.12(10.0.0.12:3306)
 +--10.0.0.13(10.0.0.13:3306)

Sun Nov 24 10:45:21 2019 - [info] Checking replication health on 10.0.0.12..
Sun Nov 24 10:45:21 2019 - [info]  ok.
Sun Nov 24 10:45:21 2019 - [info] Checking replication health on 10.0.0.13..
Sun Nov 24 10:45:21 2019 - [info]  ok.
Sun Nov 24 10:45:21 2019 - [warning] master_ip_failover_script is not defined.
Sun Nov 24 10:45:21 2019 - [warning] shutdown_script is not defined.
Sun Nov 24 10:45:21 2019 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

開啓MHA

[root@mysql2 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover  < /dev/null> /var/log/mha/app1/manager.log 2>&1 &
[1] 4192

檢查狀態

[root@mysql2 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:4192) is running(0:PING_OK), master:10.0.0.11
#獲取主節點ID
[root@mysql2 ~]# mysql -umha -pQaz@123456 -h 10.0.0.11 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 5     |
+---------------+-------+
#獲取從節點ID
[root@mysql2 ~]# mysql -umha -pQaz@123456 -h 10.0.0.12 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 6     |
+---------------+-------+
#獲取從節點ID
[root@mysql2 ~]# mysql -umha -pQaz@123456 -h 10.0.0.13 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 7     |
+---------------+-------+
[root@mysql2 ~]# 

MHA主節點監控日誌:

tail -f /var/log/mha/app1/manager

停掉MySQL主節點

systemctl stop mysqld

MHA主節點日誌輸出(最後兩行):

10.0.0.12(10.0.0.12:3306): Resetting slave info succeeded.
Master failover to 10.0.0.12(10.0.0.12:3306) completed successfully.
#看到successfully表示成功切換

此時,12成爲主節點,13變成12的從節點

#13執行
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.12
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql3-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

恢復主節點:

啓動數據庫,執行:
CHANGE MASTER TO
MASTER_HOST='10.0.0.12',
MASTER_USER='repl',
MASTER_PASSWORD='Qaz@123456',
MASTER_PORT=3306;

start slave ;

此時12變爲主節點,11和13都是從節點

修改MHA配置文件(主節點宕掉後會自動刪除主節點配置)

vim /etc/mha/app1.cnf
[server1]
hostname=10.0.0.51
port=3306

重啓MHA

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover  < /dev/null> /var/log/mha/app1/manager.log 2>&1 &
MHA的VIP功能

修改配置文件,添加參數:

master_ip_failover_script=/usr/local/bin/master_ip_failover
#腳本路徑

下載腳本
腳本加執行權限

chmod +x /usr/local/bin/master_ip_failover

需要修改虛擬IP地址和網卡名

my $vip = '10.0.0.14/24'; #虛擬IP地址
my $key = '1'; #key值,生成虛擬IP的值
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";

生成虛擬IP

ifconfig ens33:1 10.0.0.14/24

檢查

[root@mysql2 bin]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:77:dd:55 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.12/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.14/24 brd 10.0.0.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::d2ce:fe9c:1b21:c659/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

重啓MHA

masterha_stop --conf=/etc/mha/app1.cnf
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &

停掉主庫測試

#日誌輸出成功後檢查節點是否多出了IP地址
Master failover to 10.0.0.11(10.0.0.11:3306) completed successfully.
#11上多出了IP地址
[root@mysql-master ~]# hostname -I
10.0.0.11 10.0.0.14 
發佈了130 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章