這篇文章主要為大家詳細(xì)介紹MySQL MHA高可用群集的原理與配置,文中還介紹了MHA高可用群集的啟動(dòng)方法和查看MHA狀態(tài)的方法,希望大家通過這篇文章能有所收獲。
成都創(chuàng)新互聯(lián)是專業(yè)的桐廬網(wǎng)站建設(shè)公司,桐廬接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行桐廬網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
MHA——Master High Availability,目前在MySQL高可用方面是一個(gè)相對(duì)成熟的解決方案,是一套優(yōu)秀的MySQL故障切換和主從提升的高可用軟件。
這里我們提到了兩個(gè)個(gè)關(guān)鍵點(diǎn):“高可用”,“故障切換“。我們逐一簡(jiǎn)單介紹一下這兩者的含義。
高可用就是可用性強(qiáng),在一定條件下(某個(gè)服務(wù)器出錯(cuò)或宕機(jī))可以保證服務(wù)器可以正常運(yùn)行,在一定程度上不會(huì)影響業(yè)務(wù)的運(yùn)行。
當(dāng)主服務(wù)器出現(xiàn)錯(cuò)誤時(shí),被manager服務(wù)器監(jiān)控到主庫mysqld服務(wù)停止后,首先對(duì)主庫進(jìn)行SSH登錄檢查(save_binary_logs -command=test),然后對(duì)mysqld服務(wù)進(jìn)行健康檢查(PING(SELECT)每個(gè)3秒檢查一次,持續(xù)3次),最后作出Master is down!的判斷,master failover開始進(jìn)行對(duì)應(yīng)的處理,具體的過程可以參考網(wǎng)上的博客,這里給出一個(gè)鏈接:https://www.cnblogs.com/xiaoboluo768/p/5210820.html 大家可以參考這位朋友的文章,講的非常詳細(xì)。
在虛擬機(jī)環(huán)境下,需要四臺(tái)Centos7服務(wù)器(這里我使用的是Centos7,所以使用的mha版本是0.57的)
其中一臺(tái)作為mha服務(wù)器(manager)來監(jiān)控管理下面的MySQL服務(wù)器;
其余三臺(tái)一主兩從,其中從服務(wù)器中的其中一臺(tái)作為儲(chǔ)備主服務(wù)器,當(dāng)主服務(wù)器宕機(jī)或出錯(cuò)時(shí)提升為主服務(wù)器。(這里就是主從提升了)
主從復(fù)制在上一篇文章中已介紹了MySQL5.7.17版本的配置實(shí)例,MySQL5.6.36的配置原理及思路一致,只不過細(xì)節(jié)上有些許差別。
首先,分配一下ip,私網(wǎng)下為了做實(shí)驗(yàn)驗(yàn)證就自己定義就行
MHA服務(wù)器——manager:192.168.68.136
MySQL主服務(wù)器——master:192.168.68.129
MySQL儲(chǔ)備主服務(wù)器(起初是從服務(wù)器)——slave1:192.168.68.132
MySQL從服務(wù)器——slave2:192.168.68.133
相關(guān)軟件包鏈接:
鏈接:https://pan.baidu.com/s/1VNdEIYvT1g_xKbrhzYNI-A
提取碼:wmyg
安裝編譯環(huán)境——安裝MySQL5.6.36數(shù)據(jù)庫——配置時(shí)間同步(可以參考上篇博客的實(shí)驗(yàn))——配置主從復(fù)制與儲(chǔ)備MySQL服務(wù)器——安裝node工具——配置mha服務(wù)器——測(cè)試驗(yàn)證
MySQL服務(wù)器配置如下:
#1.安裝編譯環(huán)境
yum install -y ncurses-devel gcc-c++ perl-Module-Install
#2.安裝gmake編譯環(huán)境
tar zxf cmake-2.8.6.tar.gz -C /opt/
cd /opt/cmake-2.8.6/
./configure
gmake && gmake install
安裝MySQL5.6.36數(shù)據(jù)庫
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
相關(guān)優(yōu)化配置
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
這邊我們就不做防火墻規(guī)則了,直接進(jìn)行關(guān)閉
systemctl stop firewalld.service
setenforce 0
接著我們分別對(duì)三個(gè)MySQL數(shù)據(jù)庫進(jìn)行主配置文件配置
1.MySQL主服務(wù)器配置——vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-update = true
2.MySQL從服務(wù)器slave1、slave2配置——vim /etc/my.cnf
[mysqld]
server-id = 2
#開啟二進(jìn)制日志
log_bin = master-bin
#使用中繼日志進(jìn)行同步
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[mysqld]
server-id = 3
#開啟二進(jìn)制日志
log_bin = master-bin
#使用中繼日志進(jìn)行同步
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
3.master、slave1、slave2分別做兩個(gè)軟鏈接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
4.啟動(dòng)MySQL服務(wù)
/usr/local/mysql/bin/mysqld_safe --user=mysql &
5.查看驗(yàn)證端口
[root@master mysql-5.6.36]# netstat -natp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 103231/mysqld
MySQL服務(wù)器上配置——根據(jù)自己的網(wǎng)段設(shè)置
在所有數(shù)據(jù)庫節(jié)點(diǎn)上授權(quán)兩個(gè)用戶,一個(gè)是從庫同步使用用戶myslave,另一個(gè)是manager 使用用戶mha
#進(jìn)入所有的服務(wù)器數(shù)據(jù)庫,進(jìn)行授權(quán)設(shè)置
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)
#補(bǔ)充由于mha相關(guān)特性避免一些問題產(chǎn)生進(jìn)行的配置
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服務(wù)器上都要配置
查看MySQL主服務(wù)器的同步位置并且進(jìn)行記錄
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從服務(wù)器上設(shè)置同步
#設(shè)置同步配置命令
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)
#查看兩個(gè)線程是否為yes
mysql> show slave status\G
*************************** 1. row ***************************
...//省略部分內(nèi)容
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:
在主服務(wù)器上創(chuàng)建一個(gè)數(shù)據(jù)庫,在從服務(wù)器上驗(yàn)證是否存在同一數(shù)據(jù)庫;
注意?。?!必須設(shè)置兩個(gè)從庫為只讀模式
mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
以上就是MySQL5.6.36數(shù)據(jù)庫的手工編譯安裝與主從復(fù)制的流程,下面我們開始進(jìn)行mha的相關(guān)配置
1.所有服務(wù)器上都安裝MHA依賴的環(huán)境,首先安裝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軟件包對(duì)于每個(gè)操作系統(tǒng)版本不一樣,這里是centos7.4必須選擇0.57版本,
在所有服務(wù)器上必須安裝node組件,最后在MHA-manager節(jié)點(diǎn)上安裝manager組件,
因?yàn)閙anager依賴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 下面會(huì)生成幾個(gè)工具,主要包括以下幾個(gè):
masterha_check_ssh 檢查MHA的SSH的配置狀況
masterha_check_repl 檢查MySQL復(fù)制狀況
masterha_manager 啟動(dòng)manager腳本
masterha_check_status 檢查當(dāng)前MHA運(yùn)行狀態(tài)
masterha_master_monitor 檢測(cè)master是否宕機(jī)
masterha_master_switch 控制故障轉(zhuǎn)移(自動(dòng)或者手動(dòng))
masterha_conf_host 添加或刪除配置的server信息
masterha_stop 關(guān)閉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安裝后也會(huì)在/usr/local/bin 下面會(huì)生成一下幾個(gè)腳本(這些工具通常由MHA manager
的腳本觸發(fā),無需人為操作)主要如下:
save_binary_logs 保存和復(fù)制master的二進(jìn)制日志
apply_diff_relay_logs 識(shí)別差異的中繼日志事件并將其差異的事件應(yīng)用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用這個(gè)工具)
purge_relay_logs 清除中繼日志(不會(huì)阻塞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.配置無密碼認(rèn)證
4.1在 manager 上配置到所有數(shù)據(jù)庫節(jié)點(diǎn)的無密碼認(rèn)證
[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 上配置到數(shù)據(jù)庫節(jié)點(diǎn)Mysql2和Mysql3的無密碼認(rèn)證
[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 上配置到數(shù)據(jù)庫節(jié)點(diǎn)Mysql1和Mysql3的無密碼認(rèn)證
[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 上配置到數(shù)據(jù)庫節(jié)點(diǎn)Mysql1和Mysql2的無密碼認(rèn)證
[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 節(jié)點(diǎn)上復(fù)制相關(guān)腳本到/usr/local/bin 目錄。
[root@manager ~]# cp -ra /root/mha/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷貝后會(huì)有四個(gè)執(zhí)行文件
[root@managers ~]# ll /usr/local/bin/scripts/
總用量 32
-rwxr-xr-x 1 mysql mysql 3648 5 月 31 2015 master_ip_failover #自動(dòng)切換時(shí) VIP 管理的腳本
-rwxr-xr-x 1 mysql mysql 9872 5 月 25 09:07 master_ip_online_change #在線切換時(shí) vip 的管理
-rwxr-xr-x 1 mysql mysql 11867 5 月 31 2015 power_manager #故障發(fā)生后關(guān)閉主機(jī)的腳本
-rwxr-xr-x 1 mysql mysql 1360 5 月 31 2015 send_report #因故障切換后發(fā)送報(bào)警的腳本
[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/
修改該配置文件如下:主要是添加內(nèi)容的部分
[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
);
#############################添加內(nèi)容部分#########################################
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.創(chuàng)建 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#設(shè)置自動(dòng)failover時(shí)候的切換腳本,也就是上邊的哪個(gè)腳本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change#設(shè)置手動(dòng)切換時(shí)候的切換腳本
password=manager#設(shè)置mysql中root用戶的密碼,這個(gè)密碼是前文中創(chuàng)建監(jiān)控用戶的那個(gè)密碼
ping_interval=1#設(shè)置監(jiān)控主庫,發(fā)送ping包的時(shí)間間隔,默認(rèn)是3秒,嘗試三次沒有回應(yīng)的時(shí)候自動(dòng)進(jìn)行railover
remote_workdir=/tmp#設(shè)置遠(yuǎn)端mysql在發(fā)生切換時(shí)binlog的保存位置
repl_password=123#設(shè)置復(fù)制用戶的密碼
repl_user=myslave#設(shè)置復(fù)制用戶的用戶
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.68.132 -s 192.168.68.133
shutdown_script=""#設(shè)置故障發(fā)生后關(guān)閉故障主機(jī)腳本(該腳本的主要作用是關(guān)閉主機(jī)放在發(fā)生腦裂,這里沒有使用)
ssh_user=root#設(shè)置ssh的登錄用戶名
user=mha#設(shè)置監(jiān)控用戶root
[server1]
hostname=192.168.68.129
port=3306
[server2]
candidate_master=1#設(shè)置為候選master
check_repl_delay=0
hostname=192.168.68.132
port=3306
[server3]
hostname=192.168.68.133
port=3306
測(cè)試 ssh 無密碼認(rèn)證,如果正常最后會(huì)輸出 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.
本實(shí)驗(yàn)涉及的內(nèi)容還是比較多的,配置過程中出現(xiàn)的問題主要有如下幾點(diǎn):
這些都是最容易出錯(cuò)的地方,大家需要格外注意。
[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 該參數(shù)代表當(dāng)發(fā)生主從切換后,老的主庫的 ip 將會(huì)從配置文件中移除。
--manger_log 日志存放位置。
--ignore_last_failover 在缺省情況下,如果 MHA 檢測(cè)到連續(xù)發(fā)生宕機(jī),且兩次宕機(jī)間隔不足 8 小時(shí)的話,則不會(huì)進(jìn)行 Failover,之所以這樣限制是為了避免 ping-pong 效應(yīng)。該參數(shù)代表忽略上次 MHA 觸發(fā)切換產(chǎn)生的文件,默認(rèn)情況下,MHA 發(fā)生切換后會(huì)在日志記目錄,也就是上面設(shè)置的日志 app1.failover.complete 文件,下次再次切換的時(shí)候如果發(fā)現(xiàn)該目錄下存在該文件將不允許觸發(fā)切換,除非在第一次切換后收到刪除該文件,為了方便,這里設(shè)置為–ignore_last_failover。
可以看到當(dāng)前的master是主服務(wù)節(jié)點(diǎn),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
#或者查看日志,也可以發(fā)現(xiàn)主服務(wù)器節(jié)點(diǎn)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..
當(dāng)然第一次配置需要手動(dòng)配置VIP(virtual ip)
[root@master bin]# /sbin/ifconfig ens33:1 192.168.68.200/24
[root@master bin]# ifconfig
ens33: flags=4163 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
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 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 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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 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模擬主服務(wù)器宕機(jī)情況查看mysql從服務(wù)器slave1是否獲取vip 地址
[root@slave1 bin]# ifconfig
ens33: flags=4163 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
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 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 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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 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
我們可以進(jìn)入slave1的數(shù)據(jù)庫中查看其狀態(tài)
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)
表明從數(shù)據(jù)庫已經(jīng)成功切換為主服務(wù)了。
關(guān)于MySQL MHA高可用群集的原理與配置就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。