前期準備:
mysql先部署好GTID主從,而後才部署MHA
1)環境準備(全部節點)
#安裝依賴包
yum install perl-DBD-MySQL -y
#進入安裝包存放目錄
[root@mysql-db01 ~]# cd /root/tools
#上傳mha安裝包
[root@mysql-db01 tools]# rz -be
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56.tar.gz
#安裝node包
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
#登陸數據庫
mysql -uroot -p
#添加mha管理帳號
grant all privileges on *.* to mha@'192.168.1.%' identified by 'mha';
flush privileges;
#查看是否添加成功
select user,host from mysql.user;
2)命令軟鏈接(全部節點)
#若是不建立命令軟鏈接,檢測mha複製狀況的時候會報錯
ln -s /usr/local/mysql/bin//mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin//mysql /usr/bin/mysql
3)部署管理節點(mha-manager:mysql-db03)
#使用epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
#安裝manager依賴包
yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
#安裝manager包
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
4)編輯配置文件
#建立配置文件目錄
mkdir -p /etc/mha
#建立日誌目錄
mkdir -p /var/log/mha/app1
#編輯mha配置文件
vim /etc/mha/app1.cnf
==================================
[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/data/mysql/data/
master_ip_failover_script=/usr/local/mha/bin/master_ip_failover
password=mha
ping_interval=2
repl_password=1234
repl_user=replication
ssh_user=root
user=mha
[server1]
hostname=192.168.1.17
port=3306
[server2]
hostname=192.168.1.151
port=3306
[server3]
hostname=192.168.1.219
port=3306
====================================
5)配置ssh信任(全部節點)
#建立祕鑰對
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
#發送公鑰,包括本身
ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.1.219
ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.1.17
ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.1.151
測試登入是須要密碼
ssh 192.168.1.219
ssh 192.168.1.17
ssh 192.168.1.151
6)解壓安裝包
mkdir /usr/local/mha
tar -xf mha4mysql-manager-0.56.tar.gz
mv mha4mysql-manager-0.56 /usr/local
cd /usr/local
mv mha4mysql-manager-0.56 mha
7)啓動測試
# 在MHA manage節點測試ssh
cd /usr/local/mha/bin
/usr/local/mha/bin/masterha_check_ssh --conf=/etc/mha/app1.cnf
#看到以下字樣,則測試成功
Tue Mar 7 01:03:33 2017 - [info] All SSH connection tests passed successfully.
# 在MHA manage節點測試複製
/usr/local/mha/bin/masterha_check_repl --conf=/etc/mha/app1.cnf
#看到以下字樣,則測試成功
MySQL Replication Health is OK.
8)啓動MHA
#啓動
cd /usr/local/mha/bin
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
若是在作項目時建議寫成腳本,不然在當shell中提示了nohup成功後,還須要按終端上鍵盤任意鍵退回到shell輸入命令窗口,而後經過在shell中輸入exit來退出終端;若是在nohup執行成功後直接點關閉程序按鈕關閉終端的話,這時候會斷掉該命令所對應的session,致使nohup對應的進程被通知須要一塊兒shutdown,起不到關掉終端後調用程序繼續後臺運行的做用。
vim start_mha.sh
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
9)檢查MHA運行狀態
/usr/local/mha/bin/masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:22623) is running(0:PING_OK), master:192.168.1.17
六切換測試
1)切換master測試
#登陸數據庫(db02)
mysql -uroot -p
#檢查複製狀況
show slave status\G
#手動中止主庫
/etc/init.d/mysqld stop
Shutting down MySQL..... SUCCESS!
再中止數據的同時查看日誌信息的變化
tailf /var/log/mha/app1/manager