Linux系統——MHA-Atlas-MySQL高可用集羣

MHAnode

MHA介紹
MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,是一套優秀的做爲MySQL高可用性環境下故障切換和主從提高的高可用軟件。在MySQL故障切換過程當中,MHA能作到0~30秒以內自動完成數據庫的故障切換操做,而且在進行故障切換過程當中,MHA能最大程度上保證數據庫的一致性,以達到真正意義上的高可用。mysql

MHA組成
MHA由兩部分組成:MHA Manager(管理節點)和MHA Node(數據節點)。MHA Manager能夠獨立部署在一臺獨立的機器上管理多個Master-Slave集羣,也能夠部署在一臺Slave上,管理一臺Slave。當Master出現故障時,它能夠自動將最新數據的Slave提高爲新的Master,而後將全部其餘的Slave從新指向新的Master。整個故障轉移過程對應程序是徹底透明的。linux

MHA工做流程
(1)從宕機崩潰的master保存二進制日誌事件(binlog events);
(2)識別含有最新更新的slave;
(3)應用差別的中繼日誌(relay log)到其餘的slave;
(4)應用從master保存的二進制日誌事件(binlog events);
(5)提高一個slave爲新的master;
(6)使其餘的slave鏈接新的master進行復制;
若是主數據庫宕機,複製主數據庫的二進制日誌出來,而後去找出三個從庫的中繼日誌relay log最全最新的一個從庫,將最全的relay log日誌在全部從庫中同步(第一次數據同步);而後將以前找到的最全的從庫提高爲主數據庫,將複製出來的bin-log日誌放到新提高的主數據庫裏,再將其餘全部從庫從新指向新提高的主庫,繼續主從複製。web

MHA架構圖sql

 


MHA工具介紹
MHA軟件由兩部分組成,Manager工具包和Node工具包數據庫

#Manager工具包主要包括如下幾個工具:vim

masterha_check_ssh #檢查MHA的SSH配置情況
masterha_check_repl #檢查MySQL複製情況
masterha_check_status #檢測當前MHA運行狀態
masterha_master_monitor #檢測master是否宕機
masterha_manger #啓動MHA
masterha_master_switch #控制故障轉移(自動或者手動)
masterha_conf_host #添加或刪除配置的server信息
masterha_secondary_check #試圖創建TCP鏈接從遠程服務器
masterha_stop #中止MHA安全

#Node工具包主要包括如下幾個工具:服務器

save_binary_logs #保存和複製master的二進制日誌
apply_diff_relay_logs #識別差別的中繼日誌事件
filter_mysqlbinlog #去除沒必要要的ROLLBACK事件
purge_relay_logs #清除中繼日誌

MySQL環境準備網絡

實驗環境:
mysql-master eth0:192.168.239.128
mysql-slaveA eth0:192.168.239.129
mysql-slaveB eth0:192.168.239.130

須要的軟件包
1) mha管理節點安裝包:
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56.tar.gz
2) mha node節點安裝包:
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56.tar.gz
3) mysql中間件:
Atlas-2.2.1.el6.x86_64.rpm
4) mysql源碼安裝包
mysql-5.6.17-linux-glibc2.5-x86_64.tar

安裝MySQL

主機名映射
[root@localhost ~]# ls
anaconda-ks.cfg mha4mysql-manager-0.56.tar.gz
Atlas-2.2.1.el6.x86_64.rpm mha4mysql-node-0.56-0.el6.noarch.rpm
install.log mha4mysql-node-0.56.tar.gz
install.log.syslog mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz
mha4mysql-manager-0.56-0.el6.noarch.rpm yum.sh
[root@localhost ~]# ll mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz
-rw-r--r--. 1 root root 305102088 Sep 4 2017 mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost ~]# echo "192.168.239.128 MySQL-Master" >> /etc/hosts
[root@localhost ~]# echo "192.168.239.129 MySQL-SlaveA" >> /etc/hosts
[root@localhost ~]# echo "192.168.239.130 MySQL-SlaveB" >> /etc/hosts
[root@localhost ~]# scp /etc/hosts root@192.168.239.129:/etc/
The authenticity of host '192.168.239.129 (192.168.239.129)' can't be established.
RSA key fingerprint is 2d:76:57:08:d0:37:08:9e:bd:f2:2c:c6:30:28:d7:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.239.129' (RSA) to the list of known hosts.
root@192.168.239.129's password:
hosts 100% 245 0.2KB/s 00:00
[root@localhost ~]# scp /etc/hosts root@192.168.239.130:/etc/
The authenticity of host '192.168.239.130 (192.168.239.130)' can't be established.
RSA key fingerprint is 2d:76:57:08:d0:37:08:9e:bd:f2:2c:c6:30:28:d7:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.239.130' (RSA) to the list of known hosts.
root@192.168.239.130's password:
hosts 100% 245 0.2KB/s 00:00


安裝MySQL(3臺都裝)
[root@localhost ~]# mount /dev/sr0 /media/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# yum -y install ncurses-devel libaio
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
[root@localhost ~]# tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql
[root@localhost ~]# useradd mysql -s /sbin/nologin -M
[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
[root@localhost ~]# /bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
[root@localhost ~]# /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost ~]# which mysqladmin
/usr/local/bin/mysqladmin
[root@localhost ~]# mysqladmin -uroot password '123456'
#加入開機自啓動並啓動mysql
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig mysqld --list
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!


配置基於GTID的主從複製

先決條件

(1)主庫和從庫都要開啓binlog
(2)主庫和從庫server-id不一樣
(3)要有主從複製用戶

主庫操做(MySQL-Master)

修改配置文件
[root@localhost mysql]# vim /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock

[mysqld]
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock

log_bin = mysql-bin #開啓binlog日誌
server_id = 1 #設置server_id

innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
#重啓動MySQL服務
[root@localhost mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
#將主庫的配置文件複製到從庫
[root@localhost mysql]# scp /etc/my.cnf root@192.168.239.129:/etc/
root@192.168.239.129's password:
my.cnf 100% 595 0.6KB/s 00:00
[root@localhost mysql]# scp /etc/my.cnf root@192.168.239.130:/etc/
root@192.168.239.130's password:
my.cnf 100% 595 0.6KB/s 00:00


建立主從複製用戶
[root@localhost mysql]# mysql -uroot -p123456
mysql> grant replication slave on *.* to rep@'192.168.239.%' identified by '123456';
Query OK, 0 rows affected (0.04 sec)

mysql> select user,host from mysql.user;
+------+-----------------------+
| user | host |
+------+-----------------------+
| root | 127.0.0.1 |
| rep | 192.168.239.% |
| root | ::1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
7 rows in set (0.08 sec)

mysql> show grants for rep@'192.168.239.%';
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for rep@192.168.239.% |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.239.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

從庫操做(mysql-slaveA和mysql-slaveB)

修改配置文件
#修改mysql-slaveA配置文件(和mysql-master配置文件一致)
#只須要修改server-id = 5選項
[root@localhost ~]# vim /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock

[mysqld]
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock

log_bin = mysql-bin #從庫的binlog也要打開
server_id = 5 #僅需修改此項

innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL................... SUCCESS!

#修改mysql-slaveB配置文件(和mysql-master配置文件一致)
#只須要修改server-id = 10選項
[root@localhost ~]# vim /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock

[mysqld]
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock

log_bin = mysql-bin #從庫的binlog也要打開
server_id = 10 #僅需修改此項

innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL................... SUCCESS!


在MySQL5.6版本里多了一個Gtid的功能,能夠自動記錄主從複製位置點的信息,並在日誌中輸出出來。

 


開啓GTID

#沒開啓以前先看一下GTID狀態
mysql> show global variables like '%gtid%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | OFF |
| gtid_executed | |
| gtid_mode | OFF |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
5 rows in set (0.01 sec)


編輯mysql配置文件(主庫從庫都須要修改)

[root@localhost ~]# vim /etc/my.cnf

#在[mysqld]模塊下面加入這三行
gtid_mode = ON
log_slave_updates #做用:雙主模式必須加入的參數
enforce_gtid_consistency
[root@localhost mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!


再次在MySQL-Master中查看GTID狀態

[root@localhost mysql]# mysql -uroot -p123456
mysql> show global variables like '%gtid%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
5 rows in set (0.00 sec)


配置主從複製(mysql-slaveA,mysql-slaveB)

[root@localhost ~]# mysql -uroot -p123456
mysql> change master to master_host='192.168.239.128',master_user='rep',master_password='123456',master_auto_position=1; #主庫IP,主庫複製用戶,主庫複製用密碼,GTID位置點(自動追蹤須要同步的position)
Query OK, 0 rows affected, 2 warnings (0.22 sec)


開啓從庫的主從複製功能(mysql-slaveA,mysql-slaveB)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.47 sec)

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.128
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 151
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 151
Relay_Log_Space: 569
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 9a7e8455-f90b-11e8-b581-000c29e4bd28
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 1
1 row in set (0.05 sec)


GTID

GTID介紹
GTID(Global Transaction)全局事務標識符:是一個惟一的標識符,它建立並與源服務器(主)上提交的每一個事務相關聯。此標識符不只對其發起的服務器是惟一的,並且在給定複製設置中的全部服務器上都是惟一的。全部交易和全部GTID之間都有1對1的映射。
GTID其實是由UUID+TID組成的。其中UUID是一個MySQL實例的惟一標識。TID表明了該實例上已經提交的事務數量,而且隨着事務提交單調遞增。
這是一個GTID的具體形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23
GTID的新特性
(1)支持多線程複製:事實上是針對每一個database開啓相應的獨立線程,即每一個庫有一個單獨的(sql thread)
(2)支持啓用GTID,在配置主從複製,傳統的方式裏,你須要找到binlog和POS點,而後change master to 指向。在mysql5.6裏,無須再知道binlog和POS點,只須要知道master的IP/端口/帳號密碼便可,由於同步複製是自動的,MySQL經過內部機制GTID自動找點同步。
(3)基於Row複製只保存改變的列,大大節省磁盤空間,網絡,內存等
(4)支持把Master和Slave的相關信息記錄在Table中;原來是記錄在文件裏,如今則記錄在表裏,加強可用性
(5)支持延遲複製
開啓方法
#mysql配置文件:
[mysqld]
gtid_mode=ON
enforce_gtid_consistency

#查看
show global variables like ‘%gtid%’;

配置從庫,禁止本身刪除MySQL中繼日誌
方法一:臨時禁止
#登錄從庫
[root@localhost ~]# mysql -uroot -p123456

#臨時禁用自動刪除relay log功能
mysql> set global relay_log_purge = 0;
Query OK, 0 rows affected (0.00 sec)

#設置只讀
mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)

方法二:永久禁止
編輯從庫配置文件/etc/my.cnf

[root@localhost ~]# vim /etc/my.cnf
#在[mysqld]模塊下加入一行,永久禁止自動刪除relay log功能
relay_log_purge = 0
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!


部署MHA

環境準備(全部節點mysql-master,mysql-slaveA,mysql-slaveB)

#光盤安裝依賴包
[root@localhost ~]# yum -y install perl-DBD-MySQL
#安裝mha4mysql-node-0.56-0.el6.noarch.rpm
[root@localhost ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]

庫上建立mha管理帳號

[root@localhost ~]# mysql -uroot -p123456
mysql> grant all privileges on *.* to mha@'192.168.239.%' identified by '123456';
Query OK, 0 rows affected (0.10 sec)

mysql> select user,host from mysql.user where user='mha';
+------+---------------+
| user | host |
+------+---------------+
| mha | 192.168.239.% |
+------+---------------+
1 row in set (0.13 sec)
#主庫上建立從庫會自動複製

1
從庫上建立主從複製用戶(MySQL-slaveA和MySQL-slaveB)

[root@localhost ~]# mysql -uroot -p123456

mysql> grant replication slave on *.* to rep@'192.168.239.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for rep@'192.168.239.%';
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for rep@192.168.239.% |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.239.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

部署管理節點(mha-manager)

在mysql-slaveB上部署管理節點
#使用阿里雲源+epel源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo

#安裝manager依賴包(須要公網源)
[root@localhost ~]# yum -y install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

#安裝manager包
[root@localhost ~]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-manager ########################################### [100%]


編輯配置文件
#建立配置文件目錄
[root@localhost ~]# mkdir -p /etc/mha
#建立日誌目錄
[root@localhost ~]# mkdir -p /var/log/mha/mha1 #若是有多組主從複製架構能夠延mha二、mha3...
#建立配置文件(默認沒有)
[root@localhost ~]# cd /etc/mha/
[root@localhost mha]# ls
[root@localhost mha]# vim /etc/mha/mha1.cnf
[server default]
manager_log=/var/log/mha/mha1/manager #manager管理日誌存放路徑
manager_workdir=/var/log/mha/mha1 #manager管理日誌的目錄路徑
master_binlog_dir=/usr/local/mysql/data #binlog日誌的存放路徑
user=mha #管理帳戶
password=123456 #管理帳戶密碼
ping_interval=2 #存活檢查的間隔時間
repl_user=rep #主從複製的受權帳戶
repl_password=123456 #主從複製的受權帳戶密碼
ssh_user=root #用於ssh鏈接的帳戶

[server1]
hostname=192.168.239.128
port=3306

[server2]
#candidate_master=1 #此條暫時註釋掉,設置爲候選master,若是設置該參數之後,發生主從切換之後會將此從庫提高爲主庫,即便這個主庫不是集羣中事件最新的slave
#check_repl_delay=0 #此條暫時註釋掉,默認狀況下若是一個slave落後master 100M的relay logs 的話,MHA將不會選擇該slave做爲一個新的master,由於對於這個slave的恢復須要花費很長時間,經過設置check_repl_delay=0,MHA觸發切換在選擇一個新的master的時候將會忽略複製延時,這個參數對於設置了candidate_master=1的主機很是有用,由於這個候選主在切換的過程當中必定是新的master
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306


配置ssh信任(全部節點mysql-master,mysql-slaveA,mysql-slaveB)
#建立密鑰對
[root@localhost mha]# ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa
Generating public/private dsa key pair.
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
6d:1c:e1:9f:35:ff:6e:5d:69:ca:08:69:79:9f:f9:80 root@localhost.localdomain
The key's randomart image is:
+--[ DSA 1024]----+
| . |
| . . |
| o o |
| o o o o |
| S * o o|
| * .. oo|
| . oE+.= +|
| . *..o|
| .o.|
+-----------------+
#發送mysql-master公鑰,包括本身
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.128
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.129
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.130

#發送mysql-slaveA公鑰,包括本身
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.128
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.129
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.130

#發送mysql-slaveB公鑰,包括本身
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.128
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.129
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.239.130


啓動測試
(1)ssh檢查檢測(在mha-master這臺服務器上進行檢查)
[root@localhost mha]# which masterha_check_ssh
/usr/bin/masterha_check_ssh
[root@localhost mha]# masterha_check_ssh --conf=/etc/mha/mha1.cnf #ssh檢查命令
Thu Dec 6 17:26:01 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Dec 6 17:26:01 2018 - [info] Reading application default configuration from /etc/mha/mha1.cnf..
Thu Dec 6 17:26:01 2018 - [info] Reading server configuration from /etc/mha/mha1.cnf..
Thu Dec 6 17:26:01 2018 - [info] Starting SSH connection tests..
...
Thu Dec 6 17:26:02 2018 - [debug] Connecting via SSH from root@192.168.239.130(192.168.239.130:22) to root@192.168.239.128(192.168.239.128:22)..
Thu Dec 6 17:26:03 2018 - [debug] ok.
Thu Dec 6 17:26:03 2018 - [debug] Connecting via SSH from root@192.168.239.130(192.168.239.130:22) to root@192.168.239.129(192.168.239.129:22)..
Thu Dec 6 17:26:04 2018 - [debug] ok.
Thu Dec 6 17:26:04 2018 - [info] All SSH connection tests passed successfully. #出現這個就表示成功


(2) 主從複製檢測

[root@localhost mha]# masterha_check_repl --conf=/etc/mha/mha1.cnf
Thu Dec 6 17:36:23 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
...
Thu Dec 6 17:36:24 2018 - [info] Checking replication health on 192.168.239.129..
Thu Dec 6 17:36:24 2018 - [info] ok.
Thu Dec 6 17:36:24 2018 - [info] Checking replication health on 192.168.239.130..
Thu Dec 6 17:36:24 2018 - [info] ok.
Thu Dec 6 17:36:24 2018 - [warning] master_ip_failover_script is not defined.
Thu Dec 6 17:36:24 2018 - [warning] shutdown_script is not defined.
Thu Dec 6 17:36:24 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.


啓動MHA
#啓動
[root@localhost mha]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 4090
[root@localhost mha]# ps -ef | grep perl | grep -v grep
root 4090 1631 0 17:57 pts/1 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

#說明:
nohup:啓動命令
--conf:指定配置文件位置
--remove_dead_master_conf:若是有master down了,就去掉配置文件裏該master的部分。

進行mha自動切換master的測試

初始狀態:

 

(1)登錄mysql-slaveA(192.168.239.129)查看信息狀態

[root@localhost ~]# mysql -uroot -p123456
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.128 #這是主庫IP地址
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 405
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 615
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:


(2)停掉mysql-master(192.168.239.128)上的MySQL服務

[root@localhost ~]# /etc/init.d/mysqld stop
Shutting down MySQL...... SUCCESS!


(3)查看mysql-slaveB上的MySQL從庫同步狀態

[root@localhost mha]# mysql -uroot -p123456 -e 'show slave status\G'
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.129 #如今的主庫IP地址
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003 #binlog日誌
Read_Master_Log_Pos: 662 #binlog日誌位置
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 665
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:


(4)查看mysql-slaveA上的MySQL,主庫同步狀態。

[root@localhost ~]# mysql -uroot -p123456 -e 'show master status'
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+--------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+--------------------------------------------------------------------------------+
| mysql-bin.000003 | 662 | | | 9a7e8455-f90b-11e8-b581-000c29e4bd28:1,
9c940ddb-f90b-11e8-b581-000c29ca6c91:1 |
+------------------+----------+--------------+------------------+--------------------------------------------------------------------------------+


(5)查看mysql-slaveB上的mha進程狀態

[root@localhost mha]# ps -ef | grep perl | grep -v grep #查詢發現mha進程已經沒了
[root@localhost mha]#


6)查看mha配置文件信息

[root@localhost mha]# cat /etc/mha/mha1.cnf
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server2]
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306

#此時[server1]模塊的信息已經消失了,看成爲主庫的mysql-master上的MySQL宕機之後,mha經過檢測發現mysql-master宕機,那麼會將binlog日誌最全的從庫馬上提高爲主庫,而其餘的從庫會指向新的主庫進行再次同步。

進行mha的故障還原測試
mha的進程只能完成1次切換,不能持續切換(完成一次後,後臺進程就當掉了,須要從新手動啓動)
因爲mysql-master的MySQL服務宕機,所以mha將mysql-slaveA提高爲了主庫。所以,咱們須要將宕機的mysql-master的MySQL服務啓動,而後做爲主庫mysql-slaveA的從庫。

初始狀態:

 


(1)將故障宕機的mysql-master的MySQL服務啓動並受權進行從同步

[root@localhost mha]# cat /var/log/mha/mha1/manager
#找到這句話,複製到文本中
CHANGE MASTER TO MASTER_HOST='192.168.239.129', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='xxx';

 


[root@localhost ~]# mysql -uroot -p123456

mysql> CHANGE MASTER TO MASTER_HOST='192.168.239.129', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='123456'; #複製/var/log/mha/mha1/manager日誌中的這句話,只需修改密碼
Query OK, 0 rows affected, 2 warnings (0.18 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.43 sec)

mysql> show slave status\G #查看從同步狀態
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.129
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 662
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 665
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:


(2)將mha配置文件裏缺失的部分補全(mysql-slaveB服務器)

[root@localhost mha]# vim /etc/mha/mha1.cnf

[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1] #添加此模塊
hostname=192.168.239.128
port=3306

[server2]
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306
[root@localhost mha]# pkill perl
[root@localhost mha]# ps -ef | grep mha
root 8641 1631 1 21:02 pts/1 00:00:00 grep mha
[1]+ Exit 1 nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1
[root@localhost mha]# ps -ef | grep mha
root 8643 1631 0 21:02 pts/1 00:00:00 grep mha


(3)啓動mha進程

[root@localhost mha]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 5215
[root@localhost mha]# ps -ef | grep perl | grep -v grep
root 5215 1631 3 19:14 pts/1 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

1
2
3
4
5
(4)停掉mysql-slaveA上的MySQL服務

[root@localhost ~]# /etc/init.d/mysqld stop
Shutting down MySQL....... SUCCESS!

1
2
3
(5)查看mysql-slaveB上的主從同步狀態:

[root@localhost mha]# mysql -uroot -p123456 -e 'show slave status\G'
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.128 #此時主庫爲mysql-master
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 448
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 408
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:


(6)啓動mysql-slaveA上的MySQL服務

[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL.................. SUCCESS!
[root@localhost ~]# mysql -uroot -p123456

mysql> CHANGE MASTER TO MASTER_HOST='192.168.239.128', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='123456'; #這條信息到mysql-slaveB中的/var/log/mha/mha1/manager中查找
Query OK, 0 rows affected, 2 warnings (0.07 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.15 sec)

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.128 #主庫爲mysql-master
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 448
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 408
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:


(7)再次補全mha配置文件後,啓動mha進程

[root@localhost mha]# vim /etc/mha/mha1.cnf

[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1]
hostname=192.168.239.128
port=3306

[server2] #添加此模塊
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306
[root@localhost mha]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 8730
[root@localhost mha]# ps -ef | grep perl | grep -v grep
root 8730 1631 1 21:19 pts/1 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover


此時的初始狀態還原爲下圖:

 


源碼安裝mha的方法

node節點的源碼安裝方法:

[root@localhost ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

[root@localhost ~]# tar xf mha4mysql-node-0.56.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/mha4mysql-node-0.56/

[root@localhost mha4mysql-node-0.56]# perl Makefile.PL

[root@localhost mha4mysql-node-0.56]# make && make install

manager節點的源碼安裝方法:

[root@localhost ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

[root@localhost ~]# tar xf mha4mysql-manager-0.56.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/mha4mysql-manager-0.56/

[root@localhost mha4mysql-manager-0.56]# perl Makefile.PL

[root@localhost mha4mysql-manager-0.56]# make && make install

配置VIP漂移

IP漂移的兩種方式
(1)經過keepalived的方式,管理虛擬IP的漂移
(2)經過MHA自帶腳本方式,管理虛擬IP的漂移
MHA腳本管理方式
(1)獲取管理腳本master_ip_failover
[root@localhost ~]# ls
anaconda-ks.cfg mha4mysql-manager-0.56-0.el6.noarch.rpm
Atlas-2.2.1.el6.x86_64.rpm mha4mysql-manager-0.56.tar.gz
install.log mha4mysql-node-0.56-0.el6.noarch.rpm
install.log.syslog mha4mysql-node-0.56.tar.gz
master_ip_failover mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz
mha4mysql-manager-0.56 yum.sh
[root@localhost scripts]# cp master_ip_failover /usr/local/bin/ #複製到/usr/local/bin目錄下
[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# chmod +x master_ip_failover
[root@localhost bin]# which master_ip_failover
/usr/local/bin/master_ip_failover


(2)修改mha配置文件

[root@localhost ~]# vim /etc/mha/mha1.cnf

[server default]
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover #添加腳本位置
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

#candidate_master=1 #留着備用的註釋項
#check_repl_delay=0 #留着備用的註釋項
[server1]
hostname=192.168.239.128
port=3306

[server2]
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306


(3)修改VIP漂移腳本(master_ip_failover腳本)

[root@localhost ~]# 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
);

my $vip = '192.168.239.60/24'; #定義VIP
my $key = '0'; #定義網卡後綴數值,若是eth0就是0,eth1就是1
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip"; #綁定VIP的命令
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down"; #關閉VIP的命令

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 {
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";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}

sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}

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";
}

(4)重啓動mha管理端

[root@localhost ~]# pkill perl
[root@localhost ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 10984

#提示:
若是啓動mha進程失敗,須要進行mha的鏈接檢測
masterha_check_ssh --conf=/etc/mha/mha1.cnf ssh鏈接檢測
masterha_check_repl --conf=/etc/mha/mha1.cnf 主從複製檢測

VIP漂移腳本驗證測試
架構初始狀態:

 

(1)查看mysql-master網絡狀態
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:E4:BD:28
inet addr:192.168.239.128 Bcast:192.168.239.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fee4:bd28/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:249193 errors:0 dropped:0 overruns:0 frame:0
TX packets:74203 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:334114738 (318.6 MiB) TX bytes:6310911 (6.0 MiB)

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:E4:BD:28 #出現VIP
inet addr:192.168.239.60 Bcast:192.168.239.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:50 errors:0 dropped:0 overruns:0 frame:0
TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8386 (8.1 KiB) TX bytes:8386 (8.1 KiB)


(2)停掉mysql-master的MySQL數據庫服務

[root@localhost ~]# /etc/init.d/mysqld stop
Shutting down MySQL..... SUCCESS!
[root@localhost ~]# ifconfig #VIP消失了
eth0 Link encap:Ethernet HWaddr 00:0C:29:59:7C:EF
inet addr:192.168.239.128 Bcast:192.168.239.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe59:7cef/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:316130 errors:0 dropped:0 overruns:0 frame:0
TX packets:58058 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:363635227 (346.7 MiB) TX bytes:6462008 (6.1 MiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:69 errors:0 dropped:0 overruns:0 frame:0
TX packets:69 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:9752 (9.5 KiB) TX bytes:9752 (9.5 KiB)

(3)查看mysql-slaveA

[root@localhost ~]# ifconfig #VIP出現了
eth0 Link encap:Ethernet HWaddr 00:0C:29:4C:D5:92
inet addr:192.168.239.129 Bcast:192.168.239.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4c:d592/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:287225 errors:0 dropped:0 overruns:0 frame:0
TX packets:47133 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:361247254 (344.5 MiB) TX bytes:5159560 (4.9 MiB)

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:4C:D5:92
inet addr:192.168.239.60 Bcast:192.168.239.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:45 errors:0 dropped:0 overruns:0 frame:0
TX packets:45 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7718 (7.5 KiB) TX bytes:7718 (7.5 KiB)

(4)查看mysql-slaveB的主從同步狀況

[root@localhost ~]# mysql -uroot -p123456 -e 'show slave status\G'
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.129 #mysql-slaveB的主庫已經切換爲mysql-slaveA
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 271
Relay_Log_File: mysql-db03-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

(5)mysql-master故障恢復

[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@localhost ~]# mysql -uroot -p123456
mysql> CHANGE MASTER TO MASTER_HOST='192.168.239.129', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='123456';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.129
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 271
Relay_Log_File: mysql-db01-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

(6)補上缺失的mha配置文件

[root@localhost mha]# vim /etc/mha/mha1.cnf

[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1] #添加此模塊
hostname=192.168.239.128
port=3306

[server2]
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306

(7)啓動mha管理進程

[root@localhost ~]# ps -ef | grep mha | grep -v grep
[root@localhost ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 14921
[root@localhost ~]# ps -ef | grep mha | grep -v grep
root 14921 13211 3 20:11 pts/3 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

此時的架構狀態

 


配置binlog-server備份服務器

主庫宕機,也許會形成主庫binlog複製不及時而致使數據丟失的狀況出現,所以配置binlog-server進行時時同步備份,是必要的一種安全手段。

修改mha配置文件
[root@localhost mha]# vim /etc/mha/mha1.cnf

[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data #全局的binlog存放位置
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1] #添加此模塊
hostname=192.168.239.128
port=3306

[server2]
hostname=192.168.239.129
port=3306

[server3]
hostname=192.168.239.130
port=3306

[binlog1] #添加binlog模塊
no_master=1 #不容許切換爲主
hostname=192.168.239.130 #存放IP
master_binlog_dir=/data/mysql/binlog/ #binlog存放位置優先級比全局的高


拉取主庫(mysql-slaveB)上的binlog日誌到mysql-slaveB的存放目錄裏
[root@localhost ~]# mkdir -p /data/mysql/binlog #建立存放目錄
[root@localhost ~]# cd /data/mysql/binlog/ #進入存放目錄
[root@localhost binlog]# ll
total 0
[root@localhost binlog]# which mysqlbinlog
/usr/local/bin/mysqlbinlog
[root@localhost binlog]# mysqlbinlog -R --host=192.168.239.60 --user=mha --password=123456 --raw --stop-never mysql-bin.000001 & #拉取VIP的binlog
[2] 15694
[root@localhost binlog]# ps -ef | grep mysqlbinlog | grep -v grep
root 16061 12786 0 20:53 pts/2 00:00:00 mysqlbinlog -R --host=192.168.239.60 --user=mha --password=x xxxx --raw --stop-never mysql-bin.000001

啓動mha管理進程
[root@localhost binlog]# pkill perl
[root@localhost binlog]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[root@localhost binlog]# ps -ef | grep perl | grep -v grep
root 15697 13211 0 20:42 pts/3 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

mysql中間件Atlas

Atlas簡介

Atlas是由Qihoo-360公司web平臺部基礎架構團隊開發維護的一個基於MySQL協議的數據中間層項目。它在MySQL官方推出的MySQL-Proxy-0.8.2版本的基礎上,修改了大量bug,添加了不少功能特性。它在MySQL官方推出的MySQL-Proxy0.8.2版本的基礎上,修改了大量bug,添加了不少功能特性。

Atlas主要功能

讀寫分離
從庫負載均衡
IP過濾
自動分表
DBA可平滑上下線DB
自動摘除宕機的DB
Atlas相對於官方MySQL-Proxy的優點

將主流程中全部Lua代碼用C重寫,Lua僅用於管理接口
重寫網絡模型,線程模型
實現了真正意義上的鏈接池
優化了鎖機制,性能提升數十倍
安裝Atlas

在mysql-slaveB上操做

[root@localhost ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ########################################### [100%]
package Atlas-2.2.1-1.x86_64 is already installed

配置Atlas

配置文件裏須要作以下修改:
在mysql-slaveB上操做
#須要修改的內容一覽:
[root@localhost ~]# cat -n /usr/local/mysql-proxy/conf/test.cnf.bak | egrep -w "12|15|18|30|36|48"
12 proxy-backend-addresses = 127.0.0.1:3306
15 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1
18 pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
30 log-level = message
36 #sql-log = OFF
45 proxy-address = 0.0.0.0:1234
48 admin-address = 0.0.0.0:2345

#將上邊內容修改成以下配置:
[root@localhost ~]# cat -n /usr/local/mysql-proxy/conf/test.cnf | egrep -w "12|15|18|30|36|48"
12 proxy-backend-addresses = 192.168.239.60:3307 #這裏添加VIP地址
15 proxy-read-only-backend-addresses = 192.168.239.128:3307@1,192.168.239.130:3307@1 #從庫地址
18 pwds = root:++gAN07C/Q0=,mha:++gAN07C/Q0= #MySQL受權用戶帳號密碼(密碼要加密)
30 log-level = error #日誌級別
36 sql-log = ON #打開日誌
45 proxy-address = 192.168.239.130:3306 #Atlas的工做監聽端口(提供代理服務)
48 admin-address = 192.168.239.130:1234 #Atlas的管理監聽端口


> 提示:
> 主從複製的主從數據庫的默認3306端口須要修改爲3307
> 第18行爲登錄MySQL的受權帳號信息,其中密碼須要加密(經過/usr/local/mysql-proxy/bin/encrypt加密工具來加密)

修改每一個數據庫的服務端口爲3307
在mysql-slaveB上操做
#先殺掉mha管理進程
[root@localhost conf]# ps -ef | grep mha | grep -v grep
root 15697 1 0 Sep05 ? 00:00:09 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
[root@localhost conf]# kill -9 15697
[root@localhost conf]# ps -ef | grep mha | grep -v grep

#修改mysql-master,mysql-slaveA,mysql-slaveB上的MySQL服務端口爲3307
[root@localhost conf]# sed -n '13p' /etc/my.cnf
port = 3307
[root@localhost conf]# /etc/init.d/mysqld restart

將從庫mysql-master和mysql-slaveB指向主庫mysql-slaveA進行同步
#mysql-master和mysql-slaveB的操做步驟相同

mysql> stop slave; #中止從同步
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.239.129', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='123123'; #受權同步信息
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave; #開始同步
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slavee' at line 1
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.239.129
Master_User: rep
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 271
Relay_Log_File: mysql-db01-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes #同步成功
Slave_SQL_Running: Yes #同步成功
Replicate_Do_DB:

修改mha配置文件
[root@localhost mha]# vim /etc/mha/mha1.cnf

 

 

[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data #全局的binlog存放位置
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1]
hostname=192.168.239.128
port=3307 #修改端口號

[server2]
hostname=192.168.239.129
port=3307

[server3]
hostname=192.168.239.130
port=3307

[binlog1] #添加binlog模塊
no_master=1 #不容許切換爲主
hostname=192.168.239.130 #存放IP
master_binlog_dir=/data/mysql/binlog/ #binlog存放位置優先級比全局的高


啓動mysqlbinlog日誌備份的進程
[root@localhost binlog]# ps -ef | grep mysqlbinlog | grep -v grep
[root@localhost binlog]# mysqlbinlog -R --host=192.168.239.129 --port=3307 --user=mha --password=123456 --raw --stop-never mysql-bin.000001 &
[1] 24880
[root@localhost binlog]# ps -ef | grep mysqlbinlog | grep -v grep
root 24880 22198 0 01:38 pts/1 00:00:00 mysqlbinlog -R --host=192.168.239.129 --port=3307 --user=mha --password=x xxxx --raw --stop-never mysql-bin.000001

啓動mha管理進程
[root@localhost conf]# ps -ef | grep perl | grep -v grep
[root@localhost conf]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 24895
[root@localhost conf]# ps -ef | grep perl | grep -v grep
root 24895 17795 3 01:41 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover

啓動Atlas服務
[root@localhost conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
[root@localhost conf]# ps -ef | grep mysql-proxy | grep -v grep
root 25005 1 0 01:43 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 25006 25005 0 01:43 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
[root@localhost conf]#

#說明:
#爲什麼啓動服務須要加test,由於在Atlas配置文件裏定義了一個實例名字爲test
#Atlas實際是啓動了某個實例(固然也能夠多實例)

[root@localhost conf]# netstat -antup | grep mysql-proxy
tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 25006/mysql-proxy
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 25006/mysql-proxy

#說明:
#監聽3306的爲Atlas對外提供代理的端口;監聽1234是Atlas的管理監聽端口

登錄Atlas管理端口

[root@localhost conf]# mysql -uuser -ppwd -P1234 -h 192.168.239.130 #登錄Atlas管理端
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases; #看全部庫,報錯,並不讓看
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
mysql> select * from help; #按提示輸入命令
+----------------------------+---------------------------------------------------------+
| command | description |
+----------------------------+---------------------------------------------------------+
| SELECT * FROM help | shows this help |
| SELECT * FROM backends | lists the backends and their state |
| SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndxs id |
| SET ONLINE $backend_id | online backend server, ... |
| ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... |
| ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... |
| REMOVE BACKEND $backend_id | example: "remove backend 1", ... |
| SELECT * FROM clients | lists the clients |
| ADD CLIENT $client | example: "add client 192.168.1.2", ... |
| REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... |
| SELECT * FROM pwds | lists the pwds |
| ADD PWD $pwd | example: "add pwd user:raw_password", ... |
| ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... |
| REMOVE PWD $pwd | example: "remove pwd user", ... |
| SAVE CONFIG | save the backends to config file |
| SELECT VERSION | display the version of Atlas |
+----------------------------+---------------------------------------------------------+
16 rows in set (0.00 sec)
mysql> select * from backends; #輸入上邊列表裏的命令,出現下表
+-------------+-------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+-------------------+-------+------+
| 1 | 192.168.239.60:3307 | up | rw |
| 2 | 192.168.239.128:3307 | up | ro |
| 3 | 192.168.239.130:3307 | up | ro |
+-------------+-------------------+-------+------+
3 rows in set (0.00 sec)

進行讀寫分離及讀負載均衡測試

在mysql-master和mysql-slaveB上的本地MySQL(3307端口)建立一個庫
#在mysql-slaveB上的本地3307端口的數據庫裏建立一個庫
[root@localhost binlog]# mysql -uroot -p123456 -P3307 #登錄本地MySQL
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.6.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| benet |
| mysql |
| performance_schema |
| test |
| yunjisuan |
+--------------------+
6 rows in set (0.00 sec)

mysql> create database slaveB; #建立一個庫slaveB
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| benet |
| slaveB |
| mysql |
| performance_schema |
| test |
| yunjisuan |
+--------------------+
7 rows in set (0.00 sec)


#在mysql-master上的本地3307端口的數據庫裏建立一個庫
[root@mysql-db01 ~]# mysql -uroot -p123456 -P3307
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.6.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| benet |
| mysql |
| performance_schema |
| test |
| yunjisuan |
+--------------------+
6 rows in set (0.00 sec)

mysql> create database master;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| benet |
| master |
| mysql |
| performance_schema |
| test |
| yunjisuan |
+--------------------+
7 rows in set (0.00 sec)

登錄mysql-slaveA(主庫)進行查看
[root@localhost ~]# mysql -uroot -p123456 -P3307
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| benet |
| mysql | #主庫192.168.239.129裏並無以前建立的master或slaveB
| performance_schema |
| test |
| yunjisuan |
+--------------------+
6 rows in set (0.00 sec)

在mysql-slaveB的本地登錄Atlas代理的3306端口
[root@localhost ~]# mysql -uroot -p123456 -h 192.168.239.130

4. 在mysql-db03的Atlas代理的MySQL服務端口裏進行寫入操做,測試讀寫分離

[root@localhost ~]# mysql -uroot -p123456 -h 192.168.239.130

 

因而可知,mysql-db01和mysql-db3的庫裏都出現了test20170910的庫。只有一個可能,數據是被寫入了主庫192.168.0.52裏,而後同步到的從庫。所以,讀寫分離測試完畢。

相關文章
相關標籤/搜索