關於MySQL-HA,目前有多種解決方案,比如heartbeat、drbd、mmm、共享存儲,但是它們各有優缺點。heartbeat、drbd配置較爲複雜,需要自己寫腳本才能實現MySQL自動切換,對於不會腳本語言的人來說,這無疑是一種腦裂問題;對於mmm,生產環境中很少有人用,且mmm 管理端需要單獨運行一臺服務器上,要是想實現高可用,就得對mmm管理端做HA,這樣無疑又增加了硬件開支;對於共享存儲,個人覺得MySQL數據還是放在本地較爲安全,存儲設備畢竟存在單點隱患。使用MySQL雙master+keepalived是一種非常好的解決方案,在MySQL-HA環境中,MySQL互爲主從關係,這樣就保證了兩臺MySQL數據的一致性,然後用keepalived實現虛擬IP,通過keepalived自帶的服務監控功能來實現MySQL故障時自動切換。
下面,我把即將上線的一個生產環境中的架構與大家分享一下,看一下這個架構中,MySQL-HA是如何實現的,環境拓撲如下
- MySQL-VIP:192.168.1.200
- MySQL-master1:192.168.1.201
- MySQL-master2:192.168.1.202
- OS版本:CentOS 5.4
- MySQL版本:5.0.89
- Keepalived版本:1.1.20
一、MySQL master-master配置
1、修改MySQL配置文件
兩臺MySQL均如要開啓binlog日誌功能,開啓方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin選項
兩臺MySQL的server-ID不能一樣,默認情況下兩臺MySQL的serverID都是1,需將其中一臺修改爲2即可
2、將192.168.1.201設爲192.168.1.202的主服務器
在192.168.1.201上新建授權用戶
- MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
- Query OK, 0 rows affected (0.00 sec)
- MySQL> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | MySQL-bin.000003 | 374 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
在192.168.1.202上將192.168.1.201設爲自己的主服務器
- MySQL> change master to master_host='192.168.1.201',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
- Query OK, 0 rows affected (0.05 sec)
- MySQL> start slave;
- Query OK, 0 rows affected (0.00 sec)
- MySQL> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.1.201
- Master_User: replication
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: MySQL-bin.000003
- Read_Master_Log_Pos: 374
- Relay_Log_File: MySQL-master2-relay-bin.000002
- Relay_Log_Pos: 235
- 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: 374
- Relay_Log_Space: 235
- 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
- 1 row in set (0.00 sec)
3、將192.168.1.202設爲192.168.1.201的主服務器
在192.168.1.202上新建授權用戶
- MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
- Query OK, 0 rows affected (0.00 sec)
- MySQL> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | MySQL-bin.000003 | 374 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
在192.168.1.201上,將192.168.1.202設爲自己的主服務器
- MySQL> change master to master_host='192.168.1.202',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
- Query OK, 0 rows affected (0.05 sec)
- MySQL> start slave;
- Query OK, 0 rows affected (0.00 sec)
- MySQL> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.1.202
- Master_User: replication
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: MySQL-bin.000003
- Read_Master_Log_Pos: 374
- Relay_Log_File: MySQL-master1-relay-bin.000002
- Relay_Log_Pos: 235
- 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: 374
- Relay_Log_Space: 235
- 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
- 1 row in set (0.00 sec)
4、MySQL同步測試
如上述均正確配置,現在任何一臺MySQL上更新數據都會同步到另一臺MySQL,MySQL同步在此不再演示
二、keepalived安裝及配置
1、192.168.1.201服務器上keepalived安裝及配置
安裝keepalived
- #tar zxvf keepalived-1.1.20.tar.gz
- #cd keepalived-1.1.20
- #./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
- #make && make install
配置keepalived
我們自己在新建一個配置文件,默認情況下keepalived啓動時會去/etc/keepalived目錄下找配置文件
- #mkdir /etc/keepalived
- #vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id MySQL-ha
- }
- vrrp_instance VI_1 {
- state BACKUP #兩臺配置此處均是BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 #優先級,另一臺改爲90
- advert_int 1
- nopreempt #不搶佔,只在優先級高的機器上設置即可,優先級低的機器不設置
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.200
- }
- }
- virtual_server 192.168.1.200 3306 {
- delay_loop 2 #每個2秒檢查一次real_server狀態
- lb_algo wrr #LVS算法
- lb_kind DR #LVS模式
- persistence_timeout 60 #會話保持時間
- protocol TCP
- real_server 192.168.1.201 3306 {
- weight 3
- notify_down /usr/local/MySQL/bin/MySQL.sh #檢測到服務down後執行的腳本
- TCP_CHECK {
- connect_timeout 10 #連接超時時間
- nb_get_retry 3 #重連次數
- delay_before_retry 3 #重連間隔時間
- connect_port 3306 #健康檢查端口
- }
- }
編寫檢測服務down後所要執行的腳本
- #vi /usr/local/MySQL/bin/MySQL.sh
- #!/bin/sh
- pkill keepalived
- #chmod +x /usr/local/MySQL/bin/MySQL.sh
注:此腳本是上面配置文件notify_down選項所用到的,keepalived使用notify_down選項來檢查real_server的服務狀態,當發現real_server服務故障時,便觸發此腳本;我們可以看到,腳本就一個命令,通過pkill keepalived強制殺死keepalived進程,從而實現了MySQL故障自動轉移。另外,我們不用擔心兩個MySQL會同時提供數據更新操作,因爲每臺MySQL上的keepalived的配置裏面只有本機MySQL的IP+VIP,而不是兩臺MySQL的IP+VIP
啓動keepalived
- #/usr/local/keepalived/sbin/keepalived –D
- #ps -aux | grep keepalived
測試
找一臺局域網PC,然後去ping MySQL的VIP,這時候MySQL的VIP是可以ping的通的
停止MySQL服務,看keepalived健康檢查程序是否會觸發我們編寫的腳本
2、192.168.1.202上keepalived安裝及配置
安裝keepalived
- #tar zxvf keepalived-1.1.20.tar.gz
- #cd keepalived-1.1.20
- #./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
- #make && make install
配置keepalived
這臺配置和上面基本一樣,但有三個地方不同:優先級爲90、無搶佔設置、real_server爲本機IP
- #mkdir /etc/keepalived
- #vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id MySQL-ha
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.200
- }
- }
- virtual_server 192.168.1.200 3306 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.168.1.202 3306 {
- weight 3
- notify_down /usr/local/MySQL/bin/MySQL.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
編寫檢測服務down後所要執行的腳本
- #vi /usr/local/MySQL/bin/MySQL.sh
- #!/bin/sh
- pkill keepalived
- #chmod +x /usr/local/MySQL/bin/MySQL.sh
- 啓動keepalived
- #/usr/local/keepalived/sbin/keepalived –D
- #ps -aux | grep keepalived
測試
停止MySQL服務,看keepalived健康檢查程序是否會觸發我們編寫的腳本
三、測試
MySQL遠程登錄測試
我們找一臺安裝有MySQL客戶端的windows,然後登錄VIP,看是否能登錄,在登錄之兩臺MySQL服務器都要授權允許從遠程登錄
- MySQL> grant all privileges on *.* to 'root'@'%' identified by '123456';
- Query OK, 0 rows affected (0.00 sec)
- MySQL> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
使用客戶端登錄VIP測試
- C:\MySQL\bin>MySQL.exe -uroot -p123456 -h192.168.1.200 -P3306
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 224
- Server version: 5.0.89-log Source distribution
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MySQL>
● keepalived故障轉移測試
※在windows客戶端一直去ping VIP,然後關閉192.168.1.201上的keepalived,正常情況下VIP就會切換到192.168.1.202上面去
※開啓192.168.1.201上的keepalived,關閉192.168.1.202上的keepalived,看是否能自動切換,正常情況下VIP又會屬於192.168.1.201
注:keepalived切換速度還是非常塊的,整個切換過程只需1-3秒
● MySQL故障轉移測試
※在192.168.1.201上關閉MySQL服務,看VIP是否會切換到192.168.1.202上
※開啓192.168.1.201上的MySQL和keepalived,然後關閉192.168.1.202上的MySQL,看VIP是否會切換到192.168.1.201上
下面是用windows客戶端連接的MySQL的VIP,在切換時我執行了一個MySQL查詢命令,從執行show databases到顯示出結果時間爲3-5秒(大家可以看到上面有個錯誤提示,不過不用擔心,因爲我們的keepalived切換大概爲3秒左右,這3秒左右VIP是誰都不屬於的)
- MySQL> show databases;
- ERROR 2006 (HY000): MySQL server has gone away
- No connection. Trying to reconnect...
- Connection id: 592
- Current database: *** NONE ***
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | MySQL |
- | test |
- +--------------------+
- 3 rows in set (9.01 sec)
後話:世間萬事萬物,都不具備絕對的完美,就像上面的MySQL-HA一樣,keepalived只能做到對3306的健康檢查,但是做不到比如像MySQL複製中的slave-SQL、slave-IO進程的檢查。所以要想做到一些細緻的健康檢查,還得需要藉助額外的監控工具,比如nagios,然後用nagios實現短信、郵件報警,從而能夠有效地解決問題。