關(guān)于基礎(chǔ)介紹及原理略過,直接上操作步驟吧。
mysql:5.7
操作系統(tǒng):centos7.4
地址規(guī)劃:
MHA manage:10.21.20.89
mysql主:10.21.20.71
mysql從1:10.21.20.131
mysql從2:10.21.20.132
vip:10.21.20.77
- 安裝好mysql及主從同步建立以后,先在主庫上創(chuàng)建監(jiān)聽用戶。
在各個(gè)主機(jī)上建立互信,為了方便直接一路回車到底。然后將私鑰文件及authorized_keys文件復(fù)制給其他主機(jī)。四臺(tái)服務(wù)器都要執(zhí)行。create user 'admin'@'%' identified by 'admin'; grant all on *.* to 'admin'@'10.21.20.%' identified by 'admin';# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:wyMb8WZ6yJ0jxXN1FgWOwbVAPwW/GJRaKHfaefvXak0 The key's randomart image is: +---[RSA 2048]----+ | o=o*+o| | . ooX = | | . o O.O .| | = + =oo.| | o S . ....| | . @ * .E| | * = oo| | o . . =| | ....| +----[SHA256]-----+ # ssh-copy-id -i .ssh/id_rsa.pub migumysql@10.21.20.71 # ssh-copy-id -i .ssh/id_rsa.pub migumysql@10.21.20.131 # ssh-copy-id -i .ssh/id_rsa.pub migumysql@10.21.20.132 - 安裝MHA
首先下載mha4mysql-manager、mha4mysql-node。
由于我的安裝環(huán)境沒有公網(wǎng),因此我將下載好的MHA安裝包及依賴包做成了本地yum源。具體方法如下:# wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm # wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
(1)找到一臺(tái)可以連接公網(wǎng)的服務(wù)器,利用yum工具將依賴包下載到服務(wù)器。做成yum源以后,打包上傳到安裝的服務(wù)器。
(2) 將mha4mysql-manager、mha4mysql-node復(fù)制到/home/mysql/MHA/mharepo目錄下,然后創(chuàng)建yum源。# yum install yum-utils -y # yumdownloader mha4mysql-manager mha4mysql-node --resolve --destdir=/home/mysql/MHA/mharepo # yum install -y createrepo
# createrepo -pdo /home/mysql/MHA/mharepo
# cd /home/mysql/MHA/
# tar -czvf mharepo.tar.gz mharepo
(3)將mharepo.tar.gz上傳四臺(tái)需要安裝的服務(wù)器解壓。然后新建一個(gè)yum文件,指向解壓的目錄。
# cat /etc/yum.repos.d/abc.repo
[abc]
name=test
baseurl=file:///root/mharepo
gpgcheck=0
enabled=1
(4) 安裝MHA,在3臺(tái)mysql上安裝node,在管理節(jié)點(diǎn)安裝manager。
# yum install mha4mysql-manager -y
# yum install mha4mysql-node -y
- 在管理節(jié)點(diǎn)編輯MHA的配置文件,并啟動(dòng)MHA manager
測(cè)試ssh及集群狀態(tài)# cat conf/mha.cnf [server default] manager_workdir=/home/mysql/MHA manager_log=/home/mysql/MHA/log/manager.log user=admin password=admin ssh_user=mysql ssh_port=22 repl_user=slave repl_password=abc123 ping_interval=1 shutdown_script="" master_ip_online_change_script="" report_script="" master_ip_failover_script="/home/mysql/MHA/scripts/master_ip_failover" [server1] hostname=10.21.20.71 port=13306 candidate_master=1 master_binlog_dir="/home/mysql/mysql/logs/" [server2] hostname=10.21.20.131 port=13306 candidate_master=1 master_binlog_dir="/home/mysql/mysql/logs/" [server3] hostname=10.21.20.132 port=13306# masterha_check_ssh --conf=/home/mysql/MHA/conf/mha.cnf # masterha_check_repl --conf=/home/mysql/MHA/conf/mha.cnf 忽略部分內(nèi)容 10.21.20.71(10.21.20.71:13306) (current master) +--10.21.20.131(10.21.20.131:13306) +--10.21.20.132(10.21.20.132:13306) Sat Apr 11 11:45:12 2020 - [info] Checking replication health on 10.21.20.131.. Sat Apr 11 11:45:12 2020 - [info] ok. Sat Apr 11 11:45:12 2020 - [info] Checking replication health on 10.21.20.132.. Sat Apr 11 11:45:12 2020 - [info] ok. Sat Apr 11 11:45:12 2020 - [warning] master_ip_failover_script is not defined. Sat Apr 11 11:45:12 2020 - [warning] shutdown_script is not defined. Sat Apr 11 11:45:12 2020 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
直接nohup masterha_manager -conf=conf/mha.cnf > log/mha-0318.log &啟動(dòng),當(dāng)crt關(guān)閉時(shí)MHA manager可能會(huì)自動(dòng)停止,網(wǎng)上找了下資料,可能是crt的bug,將啟動(dòng)命令寫到shell里可以避免此情況。有待驗(yàn)證。# ln -s /home/migumysql/mysql/bin/mysql /usr/local/bin/mysql # ln -s /home/migumysql/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog# cat start_mah.sh #!/bin/bash cd `dirname $0` nohup masterha_manager -conf=conf/mha.cnf > log/mha-0410.log & # ./start_mah.sh - 在數(shù)據(jù)庫節(jié)點(diǎn)安裝keepalived(本例中為10.21.20.71、10.21.20.131)。主庫的priority要比從庫的高。
# yum install keepalived -y # cat /etc/keepalived/keepalived.conf global_defs { notification_email { 123456.qq.com } notification_email_from 123456.qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL-HA script_user root enable_script_security } vrrp_script check_mysql_alived { script "/etc/keepalived/mysql_monitor.sh" interval 5 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 191 priority 120 nopreempt advert_int 2 authentication { auth_type PASS auth_pass Cmcc } track_script { check_mysql_alived } virtual_ipaddress { 10.21.20.77/24 } } # # cat /etc/keepalived/mysql_monitor.sh #!/bin/bash check(){ /home/migumysql/mysql/bin/mysql -u'root' -p'Migu123!' -S /home/migumysql/mysql/logs/mysql.sock -e "select user()" > /dev/null 2>&1 r1=$? r2=$(/usr/sbin/ss -nltp | grep 3306 | wc -l) if [[ $r1 -ne 0 ]] || [[ $r2 -eq 0 ]];then echo "error" else echo "ok" fi } if [ $(check) = "ok" ];then echo "MySQL OK!" exit 0 else sleep 2 if [ $(check) = "error" ];then systemctl stop keepalived pkill -9 keepalived echo "MySQL Error! Keepalived Stop." fi fi # # chmod a+x /etc/keepalived/mysql_monitor.sh # systemctl start keepalived - 此時(shí)查看vip是否在主庫上,從庫沒有vip。驗(yàn)證正確后,停止主庫查看主從是否自動(dòng)切換,vip是否會(huì)漂移到從庫。
# tailf log/manager.log 忽略部分內(nèi)容 ----- Failover Report ----- mha: MySQL Master failover 10.21.20.71(10.21.20.71:13306) to 10.21.20.131(10.21.20.131:13306) succeeded Master 10.21.20.71(10.21.20.71:13306) is down! Check MHA Manager logs at localhost:/home/migumysql/MHA/log/manager.log for details. Started automated(non-interactive) failover. The latest slave 10.21.20.131(10.21.20.131:13306) has all relay logs for recovery. Selected 10.21.20.131(10.21.20.131:13306) as a new master. 10.21.20.131(10.21.20.131:13306): OK: Applying all logs succeeded. 10.21.20.132(10.21.20.132:13306): This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. 10.21.20.132(10.21.20.132:13306): OK: Applying all logs succeeded. Slave started, replicating from 10.21.20.131(10.21.20.131:13306) 10.21.20.131(10.21.20.131:13306): Resetting slave info succeeded. Master failover to 10.21.20.131(10.21.20.131:13306) completed successfully.
在主庫和從庫上觀察ip發(fā)現(xiàn),vip已經(jīng)自動(dòng)漂移到從庫上。至此高可用完成。