本文主要給大家簡(jiǎn)單講講MySQL高可用MHA講析,相關(guān)專業(yè)術(shù)語(yǔ)大家可以上網(wǎng)查查或者找一些相關(guān)書籍補(bǔ)充一下,這里就不涉獵了,直奔主題,希望可以給大家?guī)硪恍?shí)際幫助。
創(chuàng)新互聯(lián)公司專注于貴德網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供貴德營(yíng)銷型網(wǎng)站建設(shè),貴德網(wǎng)站制作、貴德網(wǎng)頁(yè)設(shè)計(jì)、貴德網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造貴德網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供貴德網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。MHA(MySQL|Master High Availability)是一種基于主從模型的相當(dāng)成熟的一種解決方案,我們對(duì)于master做一個(gè)高可用,使得哪怕在master數(shù)據(jù)庫(kù)云服務(wù)器宕機(jī)時(shí),我們的slave可以及時(shí)頂上,直接變成master主機(jī),保證服務(wù)可靠的運(yùn)行;
由于mha4mysql不存在于鏡像倉(cāng)庫(kù)和epel中,所以需要下載rpm包,我下載的是
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
建議mha服務(wù)器和master服務(wù)器分開;否則master物理宕機(jī)的話,mha也會(huì)宕機(jī);
Master和slave上安裝mha4mysql-node節(jié)點(diǎn);
mha服務(wù)器兩個(gè)rpm包都需要安裝;
MHA: CentOS 7.5B 172.16.75.2
Master: CentOS 7.5D 172.16.75.4
Slave: CentOS 7.5C 172.16.75.3
三臺(tái)主機(jī)都需要進(jìn)行SSH免密通信;
因?yàn)槿绻鹠aster宕機(jī),slave需要頂上,所以slave和master都需要開啟二進(jìn)制日志和中繼日志;
Master配置文件:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd innodb_file_per_table=ON skip_name_resolve=ON server_id=401 log_bin=/var/lib/mysql/binlog sync_binlog=1 innodb_flush_log_at_trx_commit=1 relay_log_purge=0 relay_log=relay_log [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.dSlave配置文件:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd innodb_file_per_table=ON skip_name_resolve=ON server_id=301 read_only=ON relay_log=slavelog relay_log_purge=0 log_bin=binlog [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d然后其他配置不變,搭建主從模型;
1.下載完mha的rpm包后,分別在對(duì)應(yīng)的節(jié)點(diǎn)安裝對(duì)應(yīng)的安裝包,通過查看mha4mysql-manager配置文件,全是二進(jìn)制執(zhí)行腳本。所以mha的配置文件需要自己寫;
[root@slave1 ~]# rpm -ql mha4mysql-manager /usr/bin/masterha_check_repl /usr/bin/masterha_check_ssh /usr/bin/masterha_check_status /usr/bin/masterha_conf_host /usr/bin/masterha_manager /usr/bin/masterha_master_monitor /usr/bin/masterha_master_switch /usr/bin/masterha_secondary_check /usr/bin/masterha_stop /usr/share/man/man1/masterha_check_repl.1.gz /usr/share/man/man1/masterha_check_ssh.1.gz /usr/share/man/man1/masterha_check_status.1.gz /usr/share/man/man1/masterha_conf_host.1.gz /usr/share/man/man1/masterha_manager.1.gz /usr/share/man/man1/masterha_master_monitor.1.gz /usr/share/man/man1/masterha_master_switch.1.gz /usr/share/man/man1/masterha_secondary_check.1.gz /usr/share/man/man1/masterha_stop.1.gz /usr/share/perl5/vendor_perl/MHA/Config.pm /usr/share/perl5/vendor_perl/MHA/DBHelper.pm /usr/share/perl5/vendor_perl/MHA/FileStatus.pm /usr/share/perl5/vendor_perl/MHA/HealthCheck.pm /usr/share/perl5/vendor_perl/MHA/ManagerAdmin.pm /usr/share/perl5/vendor_perl/MHA/ManagerAdminWrapper.pm /usr/share/perl5/vendor_perl/MHA/ManagerConst.pm /usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm /usr/share/perl5/vendor_perl/MHA/MasterFailover.pm /usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm /usr/share/perl5/vendor_perl/MHA/MasterRotate.pm /usr/share/perl5/vendor_perl/MHA/SSHCheck.pm /usr/share/perl5/vendor_perl/MHA/Server.pm /usr/share/perl5/vendor_perl/MHA/ServerManager.pm2.創(chuàng)建mha相對(duì)應(yīng)的配置文件;
[root@slave1 ~]# mkdir /etc/mha [root@slave1 ~]# vim /etc/mha/app1.cnf [root@slave1 ~]# [root@slave1 ~]# cat /etc/mha/app1.cnf [server default] user=mha #登錄用戶 password=mhapass manager_workdir=/data/masterha/app1 manager_log=/data/masterha/app1/manager.log remote_workdir=/data/masterha/app1 ssh_user=root repl_user=repuser #master做replication slave授權(quán)的用戶 repl_password=123456 ping_interval=1 [server1] hostname=172.16.75.4 candidate_master=1 [server2] hostname=172.16.75.3 candidate_master=1 [root@slave1 ~]# [root@slave1 ~]# mkdir -pv /data/masterha/app1 mkdir: 已創(chuàng)建目錄 "/data" mkdir: 已創(chuàng)建目錄 "/data/masterha" mkdir: 已創(chuàng)建目錄 "/data/masterha/app1"3.根據(jù)配置文件內(nèi)容,master還需要對(duì)mha做一個(gè)授權(quán)用戶。
Master mysql:
MariaDB [(none)]> grant all on *.* to 'mha'@'%' identified by 'mhapass'; Query OK, 0 rows affected (0.08 sec)4.在mha服務(wù)器上進(jìn)行ssh測(cè)試和repl測(cè)試(最后顯示 OK即可);
[root@slave1 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf Wed Nov 7 20:46:17 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Wed Nov 7 20:46:17 2018 - [info] Reading application default configuration from /etc/mastermha/app1.cnf.. Wed Nov 7 20:46:17 2018 - [info] Reading server configuration from /etc/mastermha/app1.cnf.. Wed Nov 7 20:46:17 2018 - [info] Starting SSH connection tests.. Wed Nov 7 20:46:18 2018 - [debug] Wed Nov 7 20:46:17 2018 - [debug] Connecting via SSH from root@172.16.75.4(172.16.75.4:22) to root@172.16.75.3(172.16.75.3:22).. Wed Nov 7 20:46:17 2018 - [debug] ok. Wed Nov 7 20:46:18 2018 - [debug] Wed Nov 7 20:46:17 2018 - [debug] Connecting via SSH from root@172.16.75.3(172.16.75.3:22) to root@172.16.75.4(172.16.75.4:22).. Wed Nov 7 20:46:18 2018 - [debug] ok. Wed Nov 7 20:46:18 2018 - [info] All SSH connection tests passed successfully.[root@slave1 ~]# masterha_check_repl --conf=/etc/mha/app.cnf … 172.16.75.4(172.16.75.4:3306) (current master) +--172.16.75.3(172.16.75.3:3306) Thu Nov 8 09:37:35 2018 - [info] Checking replication health on 172.16.75.3.. Thu Nov 8 09:37:35 2018 - [info] ok. Thu Nov 8 09:37:35 2018 - [warning] master_ip_failover_script is not defined. Thu Nov 8 09:37:35 2018 - [warning] shutdown_script is not defined. Thu Nov 8 09:37:35 2018 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.5.啟動(dòng)mha4mysql進(jìn)程;
[root@slave1 ~]# nohup masterha_manager --conf=/etc/mha/app.cnf > /data/masterha/app1/manager.log 2>&1 & [1] 851546.檢測(cè)mha的狀態(tài);
[root@slave1 ~]# masterha_check_status --conf=/etc/mha/app.cnf app (pid:85154) is running(0:PING_OK), master:172.16.75.47.測(cè)試:
把master的mysql進(jìn)程關(guān)掉;然后查看manager上的mha日志:
Master:
[root@slave2 ~]# systemctl stop mariadbManager:
[root@slave1 ~]# masterha_check_status --conf=/etc/mha/app.cnf app master is down and failover is running(50:FAILOVER_RUNNING). master:172.16.75.4 [root@slave1 ~]# cat /data/masterha/app1/manager.log … … ----- Failover Report ----- app: MySQL Master failover 172.16.75.4(172.16.75.4:3306) to 172.16.75.3(172.16.75.3:3306) succeeded Master 172.16.75.4(172.16.75.4:3306) is down! Check MHA Manager logs at slave1.ljy.com:/data/masterha/app1/manager.log for details. Started automated(non-interactive) failover. The latest slave 172.16.75.3(172.16.75.3:3306) has all relay logs for recovery. Selected 172.16.75.3(172.16.75.3:3306) as a new master. 172.16.75.3(172.16.75.3:3306): OK: Applying all logs succeeded. Generating relay diff files from the latest slave succeeded. 172.16.75.3(172.16.75.3:3306): Resetting slave info succeeded. Master failover to 172.16.75.3(172.16.75.3:3306) completed successfully.最后,當(dāng)出現(xiàn)Master failover to 172.16.75.3(172.16.75.3:3306) completed successfully.
就意味著我們的master已經(jīng)轉(zhuǎn)到slave上了,雖然之前的Master數(shù)據(jù)庫(kù)服務(wù)器down掉,但是我們通過mha及時(shí)的切換到slave主機(jī)上,保證數(shù)據(jù)庫(kù)服務(wù)器的可靠性。
遇到的大坑:Checking if super_read_only is defined and turned on..DBD::mysql::st execute failed: Unknown system variable 'super_read_only' at /usr/share/perl5/vendor_perl/MHA/SlaveUtil.pm line 245.
一開始用的0.58版本的mha,結(jié)果在進(jìn)行repl檢測(cè)的時(shí)候,出現(xiàn)了“super read only”的錯(cuò)誤,這是因?yàn)槲覀兊膍ysql版本和mha版本有不兼容的情況,換用0.56的版本就可以,因?yàn)?.58版本的mha兼容的是mariadb-10以后版本有”super_read_only”選項(xiàng),我們正常CentOS-7系列主機(jī)上安裝的MariaDB-5.56沒有”super_read_only”選項(xiàng),所以檢測(cè)repl權(quán)限時(shí),會(huì)說我們沒有設(shè)置此選項(xiàng);
總之,mha在生產(chǎn)環(huán)境中有很高的必要性,我們需要Proxysql做讀寫分離提升服務(wù)器性能的同時(shí),又需要mha對(duì)master做高可用來保證服務(wù)器的可靠性,保障我們的數(shù)據(jù)庫(kù)不間斷的運(yùn)行;
MySQL高可用MHA講析就先給大家講到這里,對(duì)于其它相關(guān)問題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會(huì)捕捉一些行業(yè)新聞及專業(yè)知識(shí)分享給大家的。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。