您好,登錄后才能下訂單哦!
MHA(Master High Availability)目前在MySQL高可用方面是一個(gè)相對(duì)成熟的解決方案,它由日本DeNA公司youshimaton(現(xiàn)就職于Facebook公司)開(kāi)發(fā),是一套優(yōu)秀的作為MySQL高可用性環(huán)境下故障切換和主從提升的高可用軟件。在MySQL故障切換過(guò)程中,MHA能做到在0~30秒之內(nèi)自動(dòng)完成數(shù)據(jù)庫(kù)的故障切換操作,并且在進(jìn)行故障切換的過(guò)程中,MHA能在最大程度上保證數(shù)據(jù)的一致性,以達(dá)到真正意義上的高可用。 MHA里有兩個(gè)角色一個(gè)是MHA Node(數(shù)據(jù)節(jié)點(diǎn))另一個(gè)是MHA Manager(管理節(jié)點(diǎn))。
- MHA Manager可以單獨(dú)部署在一臺(tái)獨(dú)立的機(jī)器上管理多個(gè)master-slave集群,也可以部署在一臺(tái)slave節(jié)點(diǎn)上。
- MHA Node運(yùn)行在每臺(tái)MySQL服務(wù)器上,MHA Manager會(huì)定時(shí)探測(cè)集群中的master節(jié)點(diǎn),當(dāng)master出現(xiàn)故障時(shí),它可以自動(dòng)將最新數(shù)據(jù)的slave提升為新的master,然后將所有其他的slave重新指向新的master。整個(gè)故障轉(zhuǎn)移過(guò)程對(duì)應(yīng)用程序完全透明。
MHA自動(dòng)故障切換過(guò)程中,MHA試圖從宕機(jī)的主服務(wù)器上保存二進(jìn)制日志,最大程度的保證數(shù)據(jù)的不丟失,但這并不總是可行的。例如,如果主服務(wù)器硬件故障或無(wú)法通過(guò)ssh訪問(wèn),MHA沒(méi)法保存二進(jìn)制日志,只進(jìn)行故障轉(zhuǎn)移而丟失了最新的數(shù)據(jù)。使用MySQL 5.5的半同步復(fù)制,可以大大降低數(shù)據(jù)丟失的風(fēng)險(xiǎn)。MHA可以與半同步復(fù)制結(jié)合起來(lái)。如果只有一個(gè)slave已經(jīng)收到了最新的二進(jìn)制日志,MHA可以將最新的二進(jìn)制日志應(yīng)用于其他所有的slave服務(wù)器上,因此可以保證所有節(jié)點(diǎn)的數(shù)據(jù)一致性。
- 異步復(fù)制(Asynchronous replication) MySQL默認(rèn)的復(fù)制即是異步的,主庫(kù)在執(zhí)行完客戶端提交的事務(wù)后會(huì)立即將結(jié)果返給給客戶端,并不關(guān)心從庫(kù)是否已經(jīng)接收并處理,這樣就會(huì)有一個(gè)問(wèn)題,主如果crash掉了,此時(shí)主上已經(jīng)提交的事務(wù)可能并沒(méi)有傳到從上,如果此時(shí),強(qiáng)行將從提升為主,可能導(dǎo)致新主上的數(shù)據(jù)不完整。
- 全同步復(fù)制(Fully synchronous replication) 指當(dāng)主庫(kù)執(zhí)行完一個(gè)事務(wù),所有的從庫(kù)都執(zhí)行了該事務(wù)才返回給客戶端。因?yàn)樾枰却袕膸?kù)執(zhí)行完該事務(wù)才能返回,所以全同步復(fù)制的性能必然會(huì)收到嚴(yán)重的影響。
- 半同步復(fù)制(Semisynchronous replication) 介于異步復(fù)制和全同步復(fù)制之間,主庫(kù)在執(zhí)行完客戶端提交的事務(wù)后不是立刻返回給客戶端,而是等待至少一個(gè)從庫(kù)接收到并寫到relay log中才返回給客戶端。相對(duì)于異步復(fù)制,半同步復(fù)制提高了數(shù)據(jù)的安全性,同時(shí)它也造成了一定程度的延遲,這個(gè)延遲最少是一個(gè)TCP/IP往返的時(shí)間。所以,半同步復(fù)制最好在低延時(shí)的網(wǎng)絡(luò)中使用。
總結(jié):
異步與半同步異同 默認(rèn)情況下MySQL的復(fù)制是異步的,Master上所有的更新操作寫入Binlog之后并不確保所有的更新都被復(fù)制到Slave之上。異步操作雖然效率高,但是在Master/Slave出現(xiàn)問(wèn)題的時(shí)候,存在很高數(shù)據(jù)不同步的風(fēng)險(xiǎn),甚至可能丟失數(shù)據(jù)。 MySQL5.5引入半同步復(fù)制功能的目的是為了保證在master出問(wèn)題的時(shí)候,至少有一臺(tái)Slave的數(shù)據(jù)是完整的。在超時(shí)的情況下也可以臨時(shí)轉(zhuǎn)入異步復(fù)制,保障業(yè)務(wù)的正常使用,直到一臺(tái)salve追趕上之后,繼續(xù)切換到半同步模式。
相較于其它HA軟件,MHA的目的在于維持MySQL Replication中Master庫(kù)的高可用性,其最大特點(diǎn)是可以修復(fù)多個(gè)Slave之間的差異日志,最終使所有Slave保持?jǐn)?shù)據(jù)一致,然后從中選擇一個(gè)充當(dāng)新的Master,并將其它Slave指向它。 從宕機(jī)崩潰的master保存二進(jìn)制日志事件(binlogevents)。 識(shí)別含有最新更新的slave。應(yīng)用差異的中繼日志(relay log)到其它slave。 應(yīng)用從master保存的二進(jìn)制日志事件(binlogevents)。 提升一個(gè)slave為新master。 使其它的slave連接新的master進(jìn)行復(fù)制。
目前MHA主要支持一主多從的架構(gòu),要搭建MHA,要求一個(gè)復(fù)制集群中必須最少有三臺(tái)數(shù)據(jù)庫(kù)服務(wù)器,一主二從,即一臺(tái)充當(dāng)master,一臺(tái)充當(dāng)備用master,另外一臺(tái)充當(dāng)從庫(kù),因?yàn)橹辽傩枰_(tái)服務(wù)器。
主機(jī) | 操作系統(tǒng) | IP地址 |
---|---|---|
master1 | CentOS 7.3 | 192.168.1.1 |
master2(備主) | CentOS 7.3 | 192.168.1.8 |
slave1 | CentOS 7.3 | 192.168.1.9 |
manager | CentOS 7.3 | 192.168.1.3 |
其中master對(duì)外提供寫服務(wù),備選master2提供讀服務(wù),slave也提供相關(guān)的讀服務(wù),一旦master1宕機(jī),將會(huì)把master2提升為新的master,slave指向新的master,manager作為管理服務(wù)器。
案例中關(guān)閉防火墻、selinux
# systemctl stop firewalld
# setenforce 0
修改hosts文件,并傳到其他主機(jī)
[root@master1 ~]# vim /etc/hosts
192.168.1.1 master1
192.168.1.8 master2
192.168.1.9 slave1
192.168.1.3 manager
[root@master1 ~]# for i in master2 slave1 manager ; do scp /etc/hosts $i:/etc/hosts ; done
配置NTP時(shí)間同步(master1):
[root@master1 ~]# vim /etc/chrony.conf
在其他服務(wù)器的配置文件中指向master1的IP地址
配置完成后,重新啟動(dòng)chronyd服務(wù),配置開(kāi)機(jī)自啟
# systemctl restart chronyd
# systemctl enable chronyd
https://developer.aliyun.com/mirror/
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# ssh-keygen -t rsa
# for host in master1 master2 slave1 manager ;do ssh-copy-id -i ~/.ssh/id_rsa.pub $host;done
測(cè)試ssh無(wú)交互登錄
在其他主機(jī)上執(zhí)行同樣的測(cè)試操作。
為了盡可能的減少主庫(kù)硬件損壞宕機(jī)造成的數(shù)據(jù)丟失,因此在配置MHA的同時(shí)建議配置成MySQL的半同步復(fù)制。
PS:mysql半同步插件是由谷歌提供,具體位置/usr/local/mysql/lib/plugin/下,一個(gè)是master用的semisync_master.so,一個(gè)是slave用的semisync_slave.so。
下面就來(lái)具體配置一下。 如果不清楚Plugin的目錄,用如下查找:
[root@master1 ~]# mysql -uroot -p123.com
mysql> show variables like '%plugin_dir%';
mysql> show variables like '%have_dynamic%';
所有MySQL數(shù)據(jù)庫(kù)服務(wù)器,安裝半同步插件(semisync_master.so,semisync_slave.so)
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.01 sec)
其他mysql主機(jī)采用同樣的方法安裝
檢查Plugin是否已正確安裝:
mysql> show plugins\G
或:
mysql> select * from information_schema.plugins\G;
查看半同步相關(guān)信息
mysql> show variables like '%rpl_semi_sync%';
可以看到半同復(fù)制插件已經(jīng)安裝,只是還沒(méi)有啟用,所以是off
PS:若主MYSQL服務(wù)器已經(jīng)存在,只是后期才搭建從MYSQL服務(wù)器,在置配數(shù)據(jù)同步前應(yīng)先將主MYSQL服務(wù)器的要同步的數(shù)據(jù)庫(kù)拷貝到從MYSQL服務(wù)器上(如先在主MYSQL上備份數(shù)據(jù)庫(kù),再用備份在從MYSQL服務(wù)器上恢復(fù))
在DB的配置文件中添加:
master1主機(jī):
[root@master1 ~]# vim /etc/my.cnf
server-id=1
log-bin=mysql-bin //二進(jìn)制日志
binlog_format=mixed //binlog日志格式,mysql默認(rèn)采用statement,建議使用mixed
log-bin-index=mysql-bin.index
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000
rpl_semi_sync_slave_enabled=1
relay_log_purge=0
relay-log = relay-bin //中繼日志,存儲(chǔ)所有主庫(kù)TP過(guò)來(lái)的binlog事件
relay-log-index=slave-relay-bin.index
PS:
- rpl_semi_sync_master_enabled=1 //1表是啟用,0表示關(guān)閉
- rpl_semi_sync_master_timeout=10000:毫秒單位 ,該參數(shù)主服務(wù)器等待確認(rèn)消息10秒后,不再等待,變?yōu)楫惒椒绞健?/li>
- relay_log_purge=0,禁止 SQL 線程在執(zhí)行完一個(gè) relay log 后自動(dòng)將其刪除,對(duì)于MHA場(chǎng)景下,對(duì)于某些滯后從庫(kù)的恢復(fù)依賴于其他從庫(kù)的relay log,因此采取禁用自動(dòng)刪除功能
master2主機(jī):
[root@master2 ~]# vim /etc/my.cnf
server-id=2
log-bin=mysql-bin
binlog_format=mixed
log-bin-index=mysql-bin.index
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=10000
rpl_semi_sync_slave_enabled=1
relay_log_purge=0
relay-log = relay-bin
relay-log-index=slave-relay-bin.index
slave1主機(jī):
[root@slave1 ~]# vim /etc/my.cnf
server-id=3
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=slave-relay-bin.index
read_only=1
rpl_semi_sync_slave_enabled=1
重啟服務(wù),查看半同步相關(guān)信息
# systemctl restart mysqld
[root@master1 ~]# mysql -uroot -p123.com
mysql> show variables like '%rpl_semi_sync%';
查看半同步狀態(tài):
mysql> show status like '%rpl_semi_sync%';
有幾個(gè)狀態(tài)參數(shù)值得關(guān)注的:
- rpl_semi_sync_master_status:顯示主服務(wù)是異步復(fù)制模式還是半同步復(fù)制模式
- rpl_semi_sync_master_clients :顯示有多少個(gè)從服務(wù)器配置為半同步復(fù)制模式
- rpl_semi_sync_master_yes_tx :顯示從服務(wù)器確認(rèn)成功提交的數(shù)量
- rpl_semi_sync_master_no_tx :顯示從服務(wù)器確認(rèn)不成功提交的數(shù)量
- rpl_semi_sync_master_tx_avg_wait_time :事務(wù)因開(kāi)啟 semi_sync ,平均需要額外等待的時(shí)間
- rpl_semi_sync_master_net_avg_wait_time :事務(wù)進(jìn)入等待隊(duì)列后,到網(wǎng)絡(luò)平均等待時(shí)間
在DB上創(chuàng)建授權(quán)用戶
master1:
創(chuàng)建用于主從復(fù)制的賬號(hào)(master1,master2都要?jiǎng)?chuàng)建):
mysql> grant replication slave on *.* to mharep@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (1.01 sec)
創(chuàng)建MHA管理賬號(hào)(三臺(tái)DB服務(wù)器)MHA會(huì)在配置文件里要求能遠(yuǎn)程登錄到數(shù)據(jù)庫(kù),所以要進(jìn)行必要的賦權(quán):
mysql> grant all privileges on *.* to manager@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
master2主機(jī):
[root@master2 ~]# mysql -uroot -p123.com
mysql> grant replication slave on *.* to mharep@'192.168.1.%' identified by '123.com'; //主從復(fù)制賬號(hào)
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to manager@'192.168.1.%' identified by '123.com'; //MHA管理賬號(hào)
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.1.1',master_port=3306,master_user='mharep',master_password='123.com',master_log_file='mysql-bin.000001',master_log_pos=742;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave; //開(kāi)啟主從同步
Query OK, 0 rows affected (0.01 sec)
查看從的狀態(tài),以下兩個(gè)值必須為yes,代表從服務(wù)器能正常連接主服務(wù)器
mysql> show slave status\G;
slave1主機(jī):
[root@slave1 ~]# mysql -uroot -p123.com
mysql> grant all privileges on *.* to manager@'192.168.1.%' identified by '123.com'; //MHA管理賬號(hào)
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.1.1',master_port=3306,master_user='mharep',master_password='123.com',master_log_file='mysql-bin.000001',master_log_pos=742;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看從的狀態(tài),以下兩個(gè)值必須為yes,代表從服務(wù)器能正常連接主服務(wù)器
mysql> show slave status\G
查看master服務(wù)器的半同步狀態(tài):
mysql> show status like '%rpl_semi_sync%';
MHA包括manager節(jié)點(diǎn)和data節(jié)點(diǎn),data節(jié)點(diǎn)包括原有的MySQL復(fù)制結(jié)構(gòu)中的主機(jī),至少3臺(tái),即1主2從,當(dāng)masterfailover后,還能保證主從結(jié)構(gòu);只需安裝node包。
- manager server:運(yùn)行監(jiān)控腳本,負(fù)責(zé)monitoring
- auto-failover:需要安裝node包和manager包
# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Config-IniFiles ncftp perl-Params-Validate perl-CPAN perl-Test-Mock-LWP.noarch perl-LWP-Authen-Negotiate.noarch perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
更改當(dāng)前cpan源,在案例中可能遇到?jīng)]有安裝的依賴庫(kù),使用國(guó)內(nèi)源安裝時(shí)快
[root@manager ~]# cpan
刪除之前的源:
cpan[1]> o conf urllist
cpan[2]> o conf urllist pop http://mirrors.nxthost.com/CPAN/
cpan[3]> o conf urllist pop http://mirrors.nic.cz/CPAN/
cpan[4]> o conf urllist pop http://cpan.panu.it/
cpan[5]> o conf urllist
添加國(guó)內(nèi)源:
cpan[6]> o conf urllist push http://mirrors.aliyun.com/CPAN/
cpan[7]> o conf urllist push http://mirrors.163.com/cpan/
cpan[8]> o conf urllist
提交保存退出
cpan[10]> o conf commit
cpan[11]> o conf urllist
cpan[12]> exit
軟件下載 https://github.com/yoshinorim
在所有數(shù)據(jù)庫(kù)節(jié)點(diǎn)上安裝mha4mysql-node-0.58.tar.gz(在管理節(jié)點(diǎn)需要node和manager都要安裝都安裝)
# wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gzls
# tar zxf mha4mysql-node-0.58.tar.gz
# cd mha4mysql-node-0.58/
# perl Makefile.PL
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI ...loaded. (1.627)
- DBD::mysql ...loaded. (4.023)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::node
# make && make install
在manager安裝mha4mysql-manager-0.58.tar.gz(node使用上面的方法安裝)
[root@manager ~]# cd /usr/local/src/
[root@manager src]# wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz
[root@manager src]# tar zxf mha4mysql-manager-0.58.tar.gz
[root@manager src]# cd mha4mysql-manager-0.58/
[root@manager mha4mysql-manager-0.58]# perl Makefile.PL
[root@manager mha4mysql-manager-0.58]# perl Makefile.PL
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI ...loaded. (1.627)
- DBD::mysql ...loaded. (4.023)
- Time::HiRes ...loaded. (1.9725)
- Config::Tiny ...loaded. (2.14)
- Log::Dispatch ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst ...missing.
==> Auto-install the 1 mandatory module(s) from CPAN? [y] y
*** Dependencies will be installed the next time you type 'make'.
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Warning: prerequisite MHA::NodeConst 0 not found.
Writing Makefile for mha4mysql::manager
[root@manager mha4mysql-manager-0.58]# make && make install
創(chuàng)建關(guān)于服務(wù)的相關(guān)目錄,復(fù)制腳本,配置文件(存放命令、配置文件、腳本…)
[root@manager mha4mysql-manager-0.58]# mkdir /etc/masterha
[root@manager mha4mysql-manager-0.58]# mkdir -p /masterha/app1
[root@manager mha4mysql-manager-0.58]# mkdir /scripts
[root@manager mha4mysql-manager-0.58]# cp samples/conf/* /etc/masterha/
[root@manager mha4mysql-manager-0.58]# cp samples/scripts/* /scripts/
與絕大多數(shù)Linux應(yīng)用程序類似,MHA的正確使用依賴于合理的配置文件。MHA的配置文件與mysql的my.cnf文件配置相似,采取的是param=value的方式來(lái)配置,配置文件位于管理節(jié)點(diǎn),通常包括每一個(gè)mysql server的主機(jī)名,mysql用戶名,密碼,工作目錄等等。
編輯/etc/masterha/app1.conf,內(nèi)容如下:
[root@manager mha4mysql-manager-0.58]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/masterha/app1 //設(shè)置manager的工作目錄
manager_log=/masterha/app1/manager.log //設(shè)置manager的日志 user=manager
user=manager //設(shè)置監(jiān)控用戶manager
password=123.com //監(jiān)控用戶manager的密碼
ssh_user=root //ssh連接用戶
repl_user=mharep //主從復(fù)制用戶
repl_password=123.com //主從復(fù)制用戶密碼
ping_interval=1 //設(shè)置監(jiān)控主庫(kù),發(fā)送ping包的時(shí)間間隔,默認(rèn)是3秒,嘗試三次沒(méi)有回應(yīng)的時(shí)候自動(dòng)進(jìn)行railover
[server1]
hostname=192.168.1.1
port=3306
master_binlog_dir=/usr/local/mysql/data //設(shè)置master 保存binlog的位置,以便MHA可以找到master的日志,這里的也就是mysql的數(shù)據(jù)目錄
candidate_master=1 //設(shè)置為候選master,如果設(shè)置該參數(shù)以后,發(fā)生主從切換以后將會(huì)將此從庫(kù)提升為主庫(kù)
[server2]
hostname=192.168.1.8
port=3306
master_binlog_dir=/usr/local/mysql/data
candidate_master=1
[server3]
hostname=192.168.1.9
port=3306
master_binlog_dir=/usr/local/mysql/data
no_master=1
[root@manager mha4mysql-manager-0.58]# >/etc/masterha/masterha_default.cnf //清空文件內(nèi)容
SSH 有效性驗(yàn)證:
[root@manager mha4mysql-manager-0.58]# masterha_check_ssh --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf
集群復(fù)制的有效性驗(yàn)證: mysql必須都啟動(dòng)
[root@manager mha4mysql-manager-0.58]# masterha_check_repl --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf
Sat Feb 22 15:05:26 2020 - [info] Reading default configuration from /etc/masterha/masterha_default.cnf..
Sat Feb 22 15:05:26 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sat Feb 22 15:05:26 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sat Feb 22 15:05:26 2020 - [info] MHA::MasterMonitor version 0.58.
Sat Feb 22 15:05:27 2020 - [info] GTID failover mode = 0
Sat Feb 22 15:05:27 2020 - [info] Dead Servers:
Sat Feb 22 15:05:27 2020 - [info] Alive Servers:
Sat Feb 22 15:05:27 2020 - [info] 192.168.1.1(192.168.1.1:3306)
Sat Feb 22 15:05:27 2020 - [info] 192.168.1.8(192.168.1.8:3306)
Sat Feb 22 15:05:27 2020 - [info] 192.168.1.9(192.168.1.9:3306)
Sat Feb 22 15:05:27 2020 - [info] Alive Slaves:
Sat Feb 22 15:05:27 2020 - [info] 192.168.1.8(192.168.1.8:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Sat Feb 22 15:05:27 2020 - [info] Replicating from 192.168.1.1(192.168.1.1:3306)
Sat Feb 22 15:05:27 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Sat Feb 22 15:05:27 2020 - [info] 192.168.1.9(192.168.1.9:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Sat Feb 22 15:05:27 2020 - [info] Replicating from 192.168.1.1(192.168.1.1:3306)
Sat Feb 22 15:05:27 2020 - [info] Not candidate for the new Master (no_master is set)
Sat Feb 22 15:05:27 2020 - [info] Current Alive Master: 192.168.1.1(192.168.1.1:3306)
Sat Feb 22 15:05:27 2020 - [info] Checking slave configurations..
Sat Feb 22 15:05:27 2020 - [info] read_only=1 is not set on slave 192.168.1.8(192.168.1.8:3306).
Sat Feb 22 15:05:27 2020 - [warning] relay_log_purge=0 is not set on slave 192.168.1.9(192.168.1.9:3306).
Sat Feb 22 15:05:27 2020 - [info] Checking replication filtering settings..
Sat Feb 22 15:05:27 2020 - [info] binlog_do_db= , binlog_ignore_db=
Sat Feb 22 15:05:27 2020 - [info] Replication filtering check ok.
Sat Feb 22 15:05:27 2020 - [info] GTID (with auto-pos) is not supported
Sat Feb 22 15:05:27 2020 - [info] Starting SSH connection tests..
Sat Feb 22 15:05:29 2020 - [info] All SSH connection tests passed successfully.
Sat Feb 22 15:05:29 2020 - [info] Checking MHA Node version..
Sat Feb 22 15:05:29 2020 - [info] Version check ok.
Sat Feb 22 15:05:29 2020 - [info] Checking SSH publickey authentication settings on the current master..
Sat Feb 22 15:05:29 2020 - [info] HealthCheck: SSH to 192.168.1.1 is reachable.
Sat Feb 22 15:05:29 2020 - [info] Master MHA Node version is 0.58.
Sat Feb 22 15:05:29 2020 - [info] Checking recovery script configurations on 192.168.1.1(192.168.1.1:3306)..
Sat Feb 22 15:05:29 2020 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/var/tmp/save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000001
Sat Feb 22 15:05:29 2020 - [info] Connecting to root@192.168.1.1(192.168.1.1:22)..
Creating /var/tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /usr/local/mysql/data, up to mysql-bin.000001
Sat Feb 22 15:05:29 2020 - [info] Binlog setting check done.
Sat Feb 22 15:05:29 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Sat Feb 22 15:05:29 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='manager' --slave_host=192.168.1.8 --slave_ip=192.168.1.8 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.28-log --manager_version=0.58 --relay_log_info=/usr/local/mysql/data/relay-log.info --relay_dir=/usr/local/mysql/data/ --slave_pass=xxx
Sat Feb 22 15:05:29 2020 - [info] Connecting to root@192.168.1.8(192.168.1.8: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-bin.000002
Temporary relay log file is /usr/local/mysql/data/relay-bin.000002
Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
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.
Sat Feb 22 15:05:29 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='manager' --slave_host=192.168.1.9 --slave_ip=192.168.1.9 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.28-log --manager_version=0.58 --relay_log_info=/usr/local/mysql/data/relay-log.info --relay_dir=/usr/local/mysql/data/ --slave_pass=xxx
Sat Feb 22 15:05:29 2020 - [info] Connecting to root@192.168.1.9(192.168.1.9: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-bin.000002
Temporary relay log file is /usr/local/mysql/data/relay-bin.000002
Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
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.
Sat Feb 22 15:05:30 2020 - [info] Slaves settings check done.
Sat Feb 22 15:05:30 2020 - [info]
192.168.1.1(192.168.1.1:3306) (current master)
+--192.168.1.8(192.168.1.8:3306)
+--192.168.1.9(192.168.1.9:3306)
Sat Feb 22 15:05:30 2020 - [info] Checking replication health on 192.168.1.8..
Sat Feb 22 15:05:30 2020 - [info] ok.
Sat Feb 22 15:05:30 2020 - [info] Checking replication health on 192.168.1.9..
Sat Feb 22 15:05:30 2020 - [info] ok.
Sat Feb 22 15:05:30 2020 - [warning] master_ip_failover_script is not defined.
Sat Feb 22 15:05:30 2020 - [warning] shutdown_script is not defined.
Sat Feb 22 15:05:30 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
驗(yàn)證成功的話會(huì)自動(dòng)識(shí)別出所有服務(wù)器和主從狀況
PS:驗(yàn)證成功的話會(huì)自動(dòng)識(shí)別出所有服務(wù)器和主從狀況 在驗(yàn)證時(shí),若遇到這個(gè)錯(cuò)誤:Can't exec "mysqlbinlog" ...... 解決方法是在所有服務(wù)器上執(zhí)行:
*ln -s /usr/local/mysql/bin/ /usr/local/bin/**
啟動(dòng) manager
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf &>/tmp/mha_manager.log &
[1] 12451
PS:在應(yīng)用Unix/Linux時(shí),一般想讓某個(gè)程序在后臺(tái)運(yùn)行,常會(huì)用 & 在程序結(jié)尾來(lái)讓程序自動(dòng)運(yùn)行。比如要運(yùn)行mysql在后臺(tái): /usr/local/mysql/bin/mysqld_safe –user=mysql &??墒怯泻芏喑绦虿⒉幌駇ysqld一樣,這樣就需要nohup(后臺(tái)運(yùn)行的進(jìn)程,并不會(huì)隨著shell環(huán)境的關(guān)閉而殺死進(jìn)程)命令
狀態(tài)檢查:
[root@manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:12451) is running(0:PING_OK), master:192.168.1.1
(自動(dòng)failover) master dead后,MHA當(dāng)時(shí)已經(jīng)開(kāi)啟,候選Master庫(kù)(Slave)會(huì)自動(dòng)failover為Master.
驗(yàn)證方式:
先停掉 master1,因?yàn)橹暗呐渲梦募?,把master2作為了候選人,那么就到 slave1上查看 master 的 IP 是否變?yōu)榱?master2的IP
[root@master1 mha4mysql-node-0.58]# systemctl stop mysqld
[root@manager ~]# cat /masterha/app1/manager.log
從日志信息中可以看到 master failover 已經(jīng)成功了,并可以看出故障轉(zhuǎn)移的大體流程
登錄 slave1的Mysql,查看 slave 狀態(tài)
[root@slave1 mha4mysql-node-0.58]# mysql -uroot -p123.com
mysql> show slave status\G
可以看到 master 的 IP 現(xiàn)在為 192.168.1.8, 已經(jīng)切換到和192.168.1.8同步了,本來(lái)是和192.168.1.1同步的,說(shuō)明 MHA 已經(jīng)把master2提升為了新的 master,IO線程和SQL線程也正確運(yùn)行,MHA搭建成功
發(fā)生主從切換后,MHAmanager服務(wù)會(huì)自動(dòng)停掉,且在manager_workdir(/masterha/app1)目錄下面生成文件app1.failover.complete(若要啟動(dòng)MHA,必須先確保無(wú)此文件) 如果有這個(gè)提示,那么刪除此文件
/ masterha/app1/app1.failover.complete [error]
[/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm, ln298] Last failover was done at 2015/01/09 10:00:47.
Current time is too early to do failover again. If you want to do failover, manually remove /
masterha/app1/app1.failover.complete and run this script again.
mysql> show master status;
[root@master1 ~]# systemctl start mysqld
[root@master1 ~]# mysql -uroot -p123.com
mysql> change master to master_host='192.168.1.8',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=742,master_user='mharep',master_password='123.com';
mysql> start slave;
在manager檢查集群狀態(tài):
[root@manager ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
[root@manager ~]# masterha_stop --conf/etc/masterha/app1.cnf
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf &>/tmp/mha_manager.log &
當(dāng)有slave 節(jié)點(diǎn)宕掉時(shí),默認(rèn)是啟動(dòng)不了的,加上 --ignore_fail_on_start 即使有節(jié)點(diǎn)宕掉也能啟動(dòng)MHA,如下:
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --ignore_fail_on_start&>/tmp/mha_manager.log &
[root@manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:13739) is running(0:PING_OK), master:192.168.1.8
PS:如果正常,會(huì)顯示"PING_OK",否則會(huì)顯示"NOT_RUNNING",這代表MHA監(jiān)控沒(méi)有開(kāi)啟。
[root@manager ~]# tail -f /masterha/app1/manager.log
重構(gòu):重構(gòu)就是主服務(wù)器宕機(jī)了,切換到備主上,備主變成了主。
因此重構(gòu)的一種方案:
原主庫(kù)修復(fù)成一個(gè)新的slave,主庫(kù)切換后,把原主庫(kù)修復(fù)成新從庫(kù)。然后重新執(zhí)行以上5步。
原主庫(kù)數(shù)據(jù)文件完整的情況下,可通過(guò)以下方式找出最后執(zhí)行的CHANGE MASTER命令:
[root@manager ~]# grep "CHANGE MASTER TO MASTER" /masterha/app1/manager.log | tail -1
Sat Feb 22 15:25:21 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.8', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=742, MASTER_USER='mharep', MASTER_PASSWORD='xxx';
定期刪除中繼日志 在配置主從復(fù)制中,slave上設(shè)置了參數(shù)relay_log_purge=0,所以slave節(jié)點(diǎn)需要定期刪除中繼日志,建議每個(gè)slave節(jié)點(diǎn)刪除中繼日志的時(shí)間錯(cuò)開(kāi)。
# crontab -e
0 5 * * * /usr/local/bin/purge_relay_logs - -user=root --password=123.com --port=3306 --disable_relay_log_purge >> /var/log/purge_relay.log 2>&1
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。