利用keepalived構建MySQL
環境拓撲如下: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 { luwenju@live.cn } notification_email_from luwenju@live.cn 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 { luwenju@live.cn } notification_email_from luwenju@live.cn 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上
關鍵字:keepalived、構建、MySQL
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統有什么不同呢
- Centos 6.6默認iptable規則詳解