您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關MySQL MHA高可用架構部署配置實例是怎樣的,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
MHA——Master High Availability,目前在MySQL高可用方面是一個相對成熟的解決方案,是一套優秀的MySQL故障切換和主從提升的高可用軟件。
這里我們提到了兩個個關鍵點:“高可用”,“故障切換“。我們逐一簡單介紹一下這兩者的含義。
高可用就是可用性強,在一定條件下(某個服務器出錯或宕機)可以保證服務器可以正常運行,在一定程度上不會影響業務的運行。
當主服務器出現錯誤時,被manager服務器監控到主庫mysqld服務停止后,首先對主庫進行SSH登錄檢查(save_binary_logs -command=test),然后對mysqld服務進行健康檢查(PING(SELECT)每個3秒檢查一次,持續3次),最后作出Master is down!的判斷,master failover開始進行對應的處理,具體的過程可以參考網上的博客,這里給出一個鏈接:https://www.cnblogs.com/xiaoboluo768/p/5210820.html 大家可以參考這位朋友的文章,講的非常詳細。
在虛擬機環境下,需要四臺Centos7服務器(這里我使用的是Centos7,所以使用的mha版本是0.57的)
其中一臺作為mha服務器(manager)來監控管理下面的MySQL服務器;
其余三臺一主兩從,其中從服務器中的其中一臺作為儲備主服務器,當主服務器宕機或出錯時提升為主服務器。(這里就是主從提升了)
主從復制在上一篇文章中已介紹了MySQL5.7.17版本的配置實例,MySQL5.6.36的配置原理及思路一致,只不過細節上有些許差別。
首先,分配一下ip,私網下為了做實驗驗證就自己定義就行
MHA服務器——manager:192.168.68.136
MySQL主服務器——master:192.168.68.129
MySQL儲備主服務器(起初是從服務器)——slave1:192.168.68.132
MySQL從服務器——slave2:192.168.68.133
相關軟件包鏈接:
鏈接:https://pan.baidu.com/s/1VNdEIYvT1g_xKbrhzYNI-A
提取碼:wmyg
安裝編譯環境——安裝MySQL5.6.36數據庫——配置時間同步(可以參考上篇博客的實驗)——配置主從復制與儲備MySQL服務器——安裝node工具——配置mha服務器——測試驗證
MySQL服務器配置如下:
#1.安裝編譯環境 yum install -y ncurses-devel gcc-c++ perl-Module-Install #2.安裝gmake編譯環境 tar zxf cmake-2.8.6.tar.gz -C /opt/ cd /opt/cmake-2.8.6/ ./configure gmake && gmake install
安裝MySQL5.6.36數據庫
tar zxf mysql-5.6.36.tar.gz -C /opt/ cd /opt/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 make make install
相關優化配置
cp support-files/my-default.cnf /etc/my.cnf #主配置文件 cp support-files/mysql.server /etc/rc.d/init.d/mysqld chmod +x /etc/rc.d/init.d/mysqld chkconfig --add mysqld echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile source /etc/profile useradd -M -s /sbin/nologin mysql chown -R mysql.mysql /usr/local/mysql /usr/local/mysql/scripts/mysql_install_db \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data \ --user=mysql
這邊我們就不做防火墻規則了,直接進行關閉
systemctl stop firewalld.service setenforce 0
接著我們分別對三個MySQL數據庫進行主配置文件配置
1.MySQL主服務器配置——vim /etc/my.cnf
[mysqld] server-id = 1 log_bin = master-bin log-slave-update = true
2.MySQL從服務器slave1、slave2配置——vim /etc/my.cnf
[mysqld] server-id = 2 #開啟二進制日志 log_bin = master-bin #使用中繼日志進行同步 relay-log = relay-log-bin relay-log-index = slave-relay-bin.index
[mysqld] server-id = 3 #開啟二進制日志 log_bin = master-bin #使用中繼日志進行同步 relay-log = relay-log-bin relay-log-index = slave-relay-bin.index
3.master、slave1、slave2分別做兩個軟鏈接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/ ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
4.啟動MySQL服務
/usr/local/mysql/bin/mysqld_safe --user=mysql &
5.查看驗證端口
[root@master mysql-5.6.36]# netstat -natp | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 103231/mysqld
MySQL服務器上配置——根據自己的網段設置
在所有數據庫節點上授權兩個用戶,一個是從庫同步使用用戶myslave,另一個是manager 使用用戶mha
#進入所有的服務器數據庫,進行授權設置 mysql> grant replication slave on *.* to 'myslave'@'192.168.68.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on *.* to 'mha'@'192.168.68.%' identified by 'manager'; Query OK, 0 rows affected (0.00 sec) #補充由于mha相關特性避免一些問題產生進行的配置 mysql>grant all privileges on *.* to 'mha'@'master' identified by 'manager'; Query OK, 0 rows affected (0.00 sec) mysql>grant all privileges on *.* to 'mha'@'slave1' identified by 'manager'; Query OK, 0 rows affected (0.00 sec) mysql>grant all privileges on *.* to 'mha'@'slave2' identified by 'manager'; Query OK, 0 rows affected (0.00 sec) mysql>flush privileges; Query OK, 0 rows affected (0.00 sec)
注意:該步驟在所有MySQL服務器上都要配置
查看MySQL主服務器的同步位置并且進行記錄
show master status; mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 120 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
MySQL從服務器上設置同步
#設置同步配置命令 mysql> change master to master_host='192.168.68.129',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=120; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) #查看兩個線程是否為yes mysql> show slave status\G *************************** 1. row *************************** ...//省略部分內容 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 120 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: Master_SSL_CA_File:
在主服務器上創建一個數據庫,在從服務器上驗證是否存在同一數據庫;
注意!!!必須設置兩個從庫為只讀模式
mysql> set global read_only=1; Query OK, 0 rows affected (0.00 sec)
以上就是MySQL5.6.36數據庫的手工編譯安裝與主從復制的流程,下面我們開始進行mha的相關配置
1.所有服務器上都安裝MHA依賴的環境,首先安裝epel源。
yum -y install epel-release --nogpgcheck yum -y install perl-DBD-MySQL \ perl-Config-Tiny \ perl-Log-Dispatch \ perl-Parallel-ForkManager \ perl-ExtUtils-CBuilder \ perl-ExtUtils-MakeMaker \ perl-CPAN
2.MHA軟件包對于每個操作系統版本不一樣,這里是centos7.4必須選擇0.57版本,
在所有服務器上必須安裝node組件,最后在MHA-manager節點上安裝manager組件,
因為manager依賴node組件,下面都是在master上操作演示安裝node組件
tar zxf mha4mysql-node-0.57.tar.gz cd mha4mysql-node-0.57 perl Makefile.PL make && make install
3.在MHA-manager上安裝manager組件
tar -zxvf mha4mysql-manager-0.57.tar.gz cd mha4mysql-manager-0.57 perl Makefile.PL make && make install
manager 安裝后在/usr/local/bin 下面會生成幾個工具,主要包括以下幾個:
masterha_check_ssh 檢查MHA的SSH的配置狀況
masterha_check_repl 檢查MySQL復制狀況
masterha_manager 啟動manager腳本
masterha_check_status 檢查當前MHA運行狀態
masterha_master_monitor 檢測master是否宕機
masterha_master_switch 控制故障轉移(自動或者手動)
masterha_conf_host 添加或刪除配置的server信息
masterha_stop 關閉manager
[root@manager mha4mysql-manager-0.57]# cd /usr/local/bin/ [root@manager bin]# ls apply_diff_relay_logs masterha_check_status mysql mysql_embedded mysql_upgrade filter_mysqlbinlog masterha_conf_host mysqladmin mysqlimport mysqlxtest innochecksum masterha_manager mysqlbinlog mysql_install_db perror libmysqlclient.a masterha_master_monitor mysqlcheck mysql_plugin pkgconfig libmysqlclient.so masterha_master_switch mysql_client_test mysqlpump plugin libmysqlclient.so.20 masterha_secondary_check mysql_client_test_embedded mysql_secure_installation purge_relay_logs libmysqlclient.so.20.3.4 masterha_stop mysql_config mysqlshow replace libmysqld.a myisamchk mysql_config_editor mysqlslap resolveip libmysqlservices.a myisam_ftdump mysqld mysql_ssl_rsa_setup resolve_stack_dump lz4_decompress myisamlog mysqld_pre_systemd mysqltest save_binary_logs masterha_check_repl myisampack mysqldump mysqltest_embedded zlib_decompress masterha_check_ssh my_print_defaults mysqldumpslow mysql_tzinfo_to_sql [root@manager bin]#
node安裝后也會在/usr/local/bin 下面會生成一下幾個腳本(這些工具通常由MHA manager
的腳本觸發,無需人為操作)主要如下:
save_binary_logs 保存和復制master的二進制日志
apply_diff_relay_logs 識別差異的中繼日志事件并將其差異的事件應用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用這個工具)
purge_relay_logs 清除中繼日志(不會阻塞SQL線程)
[root@slave2 mha4mysql-node-0.57]# cd /usr/local/bin/ [root@slave2 bin]# ls apply_diff_relay_logs ccmake cmake cpack ctest filter_mysqlbinlog purge_relay_logs save_binary_logs
4.配置無密碼認證
4.1在 manager 上配置到所有數據庫節點的無密碼認證
[root@manager ~]# ssh-keygen -t rsa //一路按回車鍵 [root@manager ~]# ssh-copy-id 192.168.68.129 [root@manager ~]# ssh-copy-id 192.168.68.132 [root@manager ~]# ssh-copy-id 192.168.68.133
4.2在 Mysql1 上配置到數據庫節點Mysql2和Mysql3的無密碼認證
[root@master ~]# ssh-keygen -t rsa [root@master ~]# ssh-copy-id 192.168.68.132 [root@master ~]# ssh-copy-id 192.168.68.133
4.3在 Mysql2 上配置到數據庫節點Mysql1和Mysql3的無密碼認證
[root@slave1 ~]# ssh-keygen -t rsa [root@slave1 ~]# ssh-copy-id 192.168.68.129 [root@slave1 ~]# ssh-copy-id 192.168.68.133
4.4在 Mysql3 上配置到數據庫節點Mysql1和Mysql2的無密碼認證
[root@slave2 ~]# ssh-keygen -t rsa [root@slave2 ~]# ssh-copy-id 192.168.68.129 [root@slave2 ~]# ssh-copy-id 192.168.68.132
5.配置MHA腳本
在 manager 節點上復制相關腳本到/usr/local/bin 目錄。
[root@manager ~]# cp -ra /root/mha/mha4mysql-manager-0.57/samples/scripts /usr/local/bin //拷貝后會有四個執行文件 [root@managers ~]# ll /usr/local/bin/scripts/ 總用量 32 -rwxr-xr-x 1 mysql mysql 3648 5 月 31 2015 master_ip_failover #自動切換時 VIP 管理的腳本 -rwxr-xr-x 1 mysql mysql 9872 5 月 25 09:07 master_ip_online_change #在線切換時 vip 的管理 -rwxr-xr-x 1 mysql mysql 11867 5 月 31 2015 power_manager #故障發生后關閉主機的腳本 -rwxr-xr-x 1 mysql mysql 1360 5 月 31 2015 send_report #因故障切換后發送報警的腳本
復制上述的自動切換時 VIP 管理的腳本到/usr/local/bin 目錄,這里使用腳本管理 VIP.
[root@manager bin]# cd scripts/ [root@managerscripts]# ls master_ip_failover master_ip_online_change power_manager send_report [root@manager scripts]# cp master_ip_failover /usr/local/bin/
修改該配置文件如下:主要是添加內容的部分
[root@manager ~]#vim /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 = '192.168.68.200'; my $brdc = '192.168.68.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"; }
6.創建 MHA 軟件目錄并拷貝配置文件。
[root@manager scripts]# mkdir /etc/masterha [root@manager scripts]# cd ~/mha/mha4mysql-manager-0.57/ [root@manager mha4mysql-manager-0.57]# ls AUTHORS blib debian lib Makefile.PL META.yml README samples tests bin COPYING inc Makefile MANIFEST pm_to_blib rpm t [root@manager mha4mysql-manager-0.57]# cd samples/ [root@manager samples]# cd conf/ [root@manager conf]# ls [root@manager conf]# cp app1.cnf /etc/masterha/ [root@manager conf]# vim /etc/masterha/app1.cnf
編輯修改app1.cnf
[server default] manager_log=/var/log/masterha/app1/manager.log##manager工作目錄 manager_workdir=/var/log/masterha/app1#manager日志 master_binlog_dir=/usr/local/mysql/data#master保存binlog的位置,這里的路徑要與master里配置的binlog的路徑一致,以便mha能找到 master_ip_failover_script=/usr/local/bin/master_ip_failover#設置自動failover時候的切換腳本,也就是上邊的哪個腳本 master_ip_online_change_script=/usr/local/bin/master_ip_online_change#設置手動切換時候的切換腳本 password=manager#設置mysql中root用戶的密碼,這個密碼是前文中創建監控用戶的那個密碼 ping_interval=1#設置監控主庫,發送ping包的時間間隔,默認是3秒,嘗試三次沒有回應的時候自動進行railover remote_workdir=/tmp#設置遠端mysql在發生切換時binlog的保存位置 repl_password=123#設置復制用戶的密碼 repl_user=myslave#設置復制用戶的用戶 secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.68.132 -s 192.168.68.133 shutdown_script=""#設置故障發生后關閉故障主機腳本(該腳本的主要作用是關閉主機放在發生腦裂,這里沒有使用) ssh_user=root#設置ssh的登錄用戶名 user=mha#設置監控用戶root [server1] hostname=192.168.68.129 port=3306 [server2] candidate_master=1#設置為候選master check_repl_delay=0 hostname=192.168.68.132 port=3306 [server3] hostname=192.168.68.133 port=3306
測試 ssh 無密碼認證,如果正常最后會輸出 successfully,如下所示。
[root@manager conf]# masterha_check_ssh -conf=/etc/masterha/app1.cnf Tue Jan 14 16:44:58 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Jan 14 16:44:58 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf.. Tue Jan 14 16:44:58 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf.. Tue Jan 14 16:44:58 2020 - [info] Starting SSH connection tests.. Tue Jan 14 16:45:00 2020 - [debug] Tue Jan 14 16:44:58 2020 - [debug] Connecting via SSH from root@192.168.68.129(192.168.68.129:22) to root@192.168.68.132(192.168.68.132:22).. Tue Jan 14 16:44:59 2020 - [debug] ok. Tue Jan 14 16:44:59 2020 - [debug] Connecting via SSH from root@192.168.68.129(192.168.68.129:22) to root@192.168.68.133(192.168.68.133:22).. Tue Jan 14 16:45:00 2020 - [debug] ok. Tue Jan 14 16:45:01 2020 - [debug] Tue Jan 14 16:44:59 2020 - [debug] Connecting via SSH from root@192.168.68.133(192.168.68.133:22) to root@192.168.68.129(192.168.68.129:22).. Tue Jan 14 16:45:00 2020 - [debug] ok. Tue Jan 14 16:45:00 2020 - [debug] Connecting via SSH from root@192.168.68.133(192.168.68.133:22) to root@192.168.68.132(192.168.68.132:22).. Tue Jan 14 16:45:01 2020 - [debug] ok. Tue Jan 14 16:45:01 2020 - [debug] Tue Jan 14 16:44:59 2020 - [debug] Connecting via SSH from root@192.168.68.132(192.168.68.132:22) to root@192.168.68.129(192.168.68.129:22).. Tue Jan 14 16:45:00 2020 - [debug] ok. Tue Jan 14 16:45:00 2020 - [debug] Connecting via SSH from root@192.168.68.132(192.168.68.132:22) to root@192.168.68.133(192.168.68.133:22).. Tue Jan 14 16:45:00 2020 - [debug] ok. Tue Jan 14 16:45:01 2020 - [info] All SSH connection tests passed successfully.
健康檢查如下:
[root@manager conf]# masterha_check_repl -conf=/etc/masterha/app1.cnf Tue Jan 14 16:45:21 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Jan 14 16:45:21 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf.. Tue Jan 14 16:45:21 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf.. Tue Jan 14 16:45:21 2020 - [info] MHA::MasterMonitor version 0.57. Creating directory /var/log/masterha/app1.. done. Tue Jan 14 16:45:22 2020 - [info] GTID failover mode = 0 Tue Jan 14 16:45:22 2020 - [info] Dead Servers: Tue Jan 14 16:45:22 2020 - [info] Alive Servers: Tue Jan 14 16:45:22 2020 - [info] 192.168.68.129(192.168.68.129:3306) Tue Jan 14 16:45:22 2020 - [info] 192.168.68.132(192.168.68.132:3306) Tue Jan 14 16:45:22 2020 - [info] 192.168.68.133(192.168.68.133:3306) Tue Jan 14 16:45:22 2020 - [info] Alive Slaves: Tue Jan 14 16:45:22 2020 - [info] 192.168.68.132(192.168.68.132:3306) Version=5.6.36-log (oldest major version between slaves) log-bin:enabled Tue Jan 14 16:45:22 2020 - [info] Replicating from 192.168.68.129(192.168.68.129:3306) Tue Jan 14 16:45:22 2020 - [info] Primary candidate for the new Master (candidate_master is set) Tue Jan 14 16:45:22 2020 - [info] 192.168.68.133(192.168.68.133:3306) Version=5.6.36-log (oldest major version between slaves) log-bin:enabled Tue Jan 14 16:45:22 2020 - [info] Replicating from 192.168.68.129(192.168.68.129:3306) Tue Jan 14 16:45:22 2020 - [info] Current Alive Master: 192.168.68.129(192.168.68.129:3306) Tue Jan 14 16:45:22 2020 - [info] Checking slave configurations.. Tue Jan 14 16:45:22 2020 - [warning] relay_log_purge=0 is not set on slave 192.168.68.132(192.168.68.132:3306). Tue Jan 14 16:45:22 2020 - [warning] relay_log_purge=0 is not set on slave 192.168.68.133(192.168.68.133:3306). Tue Jan 14 16:45:22 2020 - [info] Checking replication filtering settings.. Tue Jan 14 16:45:22 2020 - [info] binlog_do_db= , binlog_ignore_db= Tue Jan 14 16:45:22 2020 - [info] Replication filtering check ok. Tue Jan 14 16:45:22 2020 - [info] GTID (with auto-pos) is not supported Tue Jan 14 16:45:22 2020 - [info] Starting SSH connection tests.. Tue Jan 14 16:45:25 2020 - [info] All SSH connection tests passed successfully. Tue Jan 14 16:45:25 2020 - [info] Checking MHA Node version.. Tue Jan 14 16:45:26 2020 - [info] Version check ok. Tue Jan 14 16:45:26 2020 - [info] Checking SSH publickey authentication settings on the current master.. Tue Jan 14 16:45:26 2020 - [info] HealthCheck: SSH to 192.168.68.129 is reachable. Tue Jan 14 16:45:26 2020 - [info] Master MHA Node version is 0.57. Tue Jan 14 16:45:26 2020 - [info] Checking recovery script configurations on 192.168.68.129(192.168.68.129:3306).. Tue Jan 14 16:45:26 2020 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/tmp/save_binary_logs_test --manager_version=0.57 --start_file=master-bin.000001 Tue Jan 14 16:45:26 2020 - [info] Connecting to root@192.168.68.129(192.168.68.129:22).. Creating /tmp if not exists.. ok. Checking output directory is accessible or not.. ok. Binlog found at /usr/local/mysql/data, up to master-bin.000001 Tue Jan 14 16:45:26 2020 - [info] Binlog setting check done. Tue Jan 14 16:45:26 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers.. Tue Jan 14 16:45:26 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.68.132 --slave_ip=192.168.68.132 --slave_port=3306 --workdir=/tmp --target_version=5.6.36-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info --relay_dir=/usr/local/mysql/data/ --slave_pass=xxx Tue Jan 14 16:45:26 2020 - [info] Connecting to root@192.168.68.132(192.168.68.132:22).. Checking slave recovery environment settings.. Opening /usr/local/mysql/data/relay-log.info ... ok. Relay log found at /usr/local/mysql/data, up to relay-log-bin.000002 Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000002 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Tue Jan 14 16:45:44 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.68.133 --slave_ip=192.168.68.133 --slave_port=3306 --workdir=/tmp --target_version=5.6.36-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info --relay_dir=/usr/local/mysql/data/ --slave_pass=xxx Tue Jan 14 16:45:44 2020 - [info] Connecting to root@192.168.68.133(192.168.68.133:22).. Checking slave recovery environment settings.. Opening /usr/local/mysql/data/relay-log.info ... ok. Relay log found at /usr/local/mysql/data, up to relay-log-bin.000002 Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000002 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Tue Jan 14 16:46:08 2020 - [info] Slaves settings check done. Tue Jan 14 16:46:08 2020 - [info] 192.168.68.129(192.168.68.129:3306) (current master) +--192.168.68.132(192.168.68.132:3306) +--192.168.68.133(192.168.68.133:3306) Tue Jan 14 16:46:08 2020 - [info] Checking replication health on 192.168.68.132.. Tue Jan 14 16:46:08 2020 - [info] ok. Tue Jan 14 16:46:08 2020 - [info] Checking replication health on 192.168.68.133.. Tue Jan 14 16:46:08 2020 - [info] ok. Tue Jan 14 16:46:08 2020 - [info] Checking master_ip_failover_script status: Tue Jan 14 16:46:08 2020 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.68.129 --orig_master_ip=192.168.68.129 --orig_master_port=3306 IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.68.200=== Checking the Status of the script.. OK Tue Jan 14 16:46:08 2020 - [info] OK. Tue Jan 14 16:46:08 2020 - [warning] shutdown_script is not defined. Tue Jan 14 16:46:08 2020 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
本實驗涉及的內容還是比較多的,配置過程中出現的問題主要有如下幾點:
主從復制:需要先進行服務器的時間同步;
秘鑰推送:IP地址要注意是否準確;
MHA的兩個配置文件的關鍵之處是否正確;
這些都是最容易出錯的地方,大家需要格外注意。
[root@manager conf]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & [1] 104076
--remove_dead_master_conf 該參數代表當發生主從切換后,老的主庫的 ip 將會從配置文件中移除。
--manger_log 日志存放位置。
--ignore_last_failover 在缺省情況下,如果 MHA 檢測到連續發生宕機,且兩次宕機間隔不足 8 小時的話,則不會進行 Failover,之所以這樣限制是為了避免 ping-pong 效應。該參數代表忽略上次 MHA 觸發切換產生的文件,默認情況下,MHA 發生切換后會在日志記目錄,也就是上面設置的日志 app1.failover.complete 文件,下次再次切換的時候如果發現該目錄下存在該文件將不允許觸發切換,除非在第一次切換后收到刪除該文件,為了方便,這里設置為–ignore_last_failover。
可以看到當前的master是主服務節點,ip地址是192.168.68.129
[root@manager conf]# masterha_check_status --conf=/etc/masterha/app1.cnf app1 (pid:104076) is running(0:PING_OK), master:192.168.68.129 #或者查看日志,也可以發現主服務器節點ip地址 [root@manager conf]# tail -2 /var/log/masterha/app1/manager.log Tue Jan 14 17:12:48 2020 - [info] Starting ping health check on 192.168.68.129(192.168.68.129:3306).. Tue Jan 14 17:12:48 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
當然第一次配置需要手動配置VIP(virtual ip)
[root@master bin]# /sbin/ifconfig ens33:1 192.168.68.200/24 [root@master bin]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.68.129 netmask 255.255.255.0 broadcast 192.168.68.255 inet6 fe80::bdab:b59b:d041:d8b0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:e6:6d:eb txqueuelen 1000 (Ethernet) RX packets 775098 bytes 1083035145 (1.0 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 413307 bytes 31715433 (30.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.68.200 netmask 255.255.255.0 broadcast 192.168.68.255 ether 00:0c:29:e6:6d:eb txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 20 bytes 1568 (1.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 20 bytes 1568 (1.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:43:58:6e txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用pkill -9 mysql模擬主服務器宕機情況查看mysql從服務器slave1是否獲取vip 地址
[root@slave1 bin]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.68.132 netmask 255.255.255.0 broadcast 192.168.68.255 inet6 fe80::4d95:1de7:d0a5:25c4 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:0d:06:80 txqueuelen 1000 (Ethernet) RX packets 766652 bytes 1074024643 (1.0 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 379004 bytes 29740586 (28.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.68.200 netmask 255.255.255.0 broadcast 192.168.68.255 ether 00:0c:29:0d:06:80 txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 146 bytes 12570 (12.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 146 bytes 12570 (12.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:95:86:1a txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
我們可以進入slave1的數據庫中查看其狀態
mysql> show slave status\G Empty set (0.00 sec) mysql> show master status -> ; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 1213 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
表明從數據庫已經成功切換為主服務了。
以上就是MySQL MHA高可用架構部署配置實例是怎樣的,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。