利用keepalived構建高可用MySQL-HA

關於MySQL-HA,目前有多種解決方案,好比heartbeat、drbd、mmm、共享存儲,可是它們各有優缺點。heartbeat、drbd配置較爲複雜,須要本身寫腳本才能實現MySQL自動切換,對於不會腳本語言的人來講,這無疑是一種腦裂問題;對於mmm,生產環境中不多有人用,且mmm 管理端須要單獨運行一臺服務器上,要是想實現高可用,就得對mmm管理端作HA,這樣無疑又增長了硬件開支;對於共享存儲,我的以爲MySQL數據仍是放在本地較爲安全,存儲設備畢竟存在單點隱患。

  使用MySQL雙master+keepalived是一種很是好的解決方案,在MySQL-HA環境中,MySQL互爲主從關係,這樣就保證了兩臺MySQL數據的一致性,而後用keepalived實現虛擬IP,經過keepalived自帶的服務監控功能來實現MySQL故障時自動切換。ios

  下面,我把即將上線的一個生產環境中的架構與你們分享一下,看一下這個架構中,MySQL-HA是如何實現的,環境拓撲以下算法

  
  
  
  
  
1 . MySQL - VIP: 192.168 . 1.200 2 . MySQL - master1: 192.168 . 1.201 3 . MySQL - master2: 192.168 . 1.202 4 . 5 . OS版本:CentOS 5.4 6 . MySQL版本: 5.0 . 89 7 . Keepalived版本: 1.1 . 20

  1、MySQL master-master配置windows

  一、修改MySQL配置文件安全

  兩臺MySQL均如要開啓binlog日誌功能,開啓方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin選項服務器

  兩臺MySQL的server-ID不能同樣,默認狀況下兩臺MySQL的serverID都是1,需將其中一臺修改成2便可架構

  二、將192.168.1.201設爲192.168.1.202的主服務器ide

  在192.168.1.201上新建受權用戶工具

  
  
  
  
  
1 . MySQL > grant replication slave on * . * to ' replication ' @ ' % ' identified by ' replication ' ; 2 . Query OK, 0 rows affected ( 0.00 sec) 3 . 4 . MySQL > show master status; 5 . + -- ----------------+----------+--------------+------------------+ 6 . | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 7 . + -- ----------------+----------+--------------+------------------+ 8 . | MySQL - bin. 000003 | 374 | | | 9 . + -- ----------------+----------+--------------+------------------+ 10 . 1 row in set ( 0.00 sec)

  在192.168.1.202上將192.168.1.201設爲本身的主服務器oop

  
  
  
  
  
1 . 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 ; 2 . Query OK, 0 rows affected ( 0.05 sec) 3 . 4 . MySQL > start slave; 5 . Query OK, 0 rows affected ( 0.00 sec) 6 . 7 . MySQL > show slave status\G 8 . *************************** 1 . row *************************** 9 . Slave_IO_State: Waiting for master to send event 10 . Master_Host: 192.168 . 1.201 11 . Master_User: replication 12 . Master_Port: 3306 13 . Connect_Retry: 60 14 . Master_Log_File: MySQL - bin. 000003 15 . Read_Master_Log_Pos: 374 16 . Relay_Log_File: MySQL - master2 - relay - bin. 000002 17 . Relay_Log_Pos: 235 18 . Relay_Master_Log_File: MySQL - bin. 000003 19 . Slave_IO_Running: Yes 20 . Slave_SQL_Running: Yes 21 . Replicate_Do_DB: 22 . Replicate_Ignore_DB: 23 . Replicate_Do_Table: 24 . Replicate_Ignore_Table: 25 . Replicate_Wild_Do_Table: 26 . Replicate_Wild_Ignore_Table: 27 . Last_Errno: 0 28 . Last_Error: 29 . Skip_Counter: 0 30 . Exec_Master_Log_Pos: 374 31 . Relay_Log_Space: 235 32 . Until_Condition: None 33 . Until_Log_File: 34 . Until_Log_Pos: 0 35 . Master_SSL_Allowed: No 36 . Master_SSL_CA_File: 37 . Master_SSL_CA_Path: 38 . Master_SSL_Cert: 39 . Master_SSL_Cipher: 40 . Master_SSL_Key: 41 . Seconds_Behind_Master: 0 42 . 1 row in set ( 0.00 sec)

  三、將192.168.1.202設爲192.168.1.201的主服務器測試

  在192.168.1.202上新建受權用戶

  
  
  
  
  
1 . MySQL > grant replication slave on * . * to ' replication ' @ ' % ' identified by ' replication ' ; 2 . Query OK, 0 rows affected ( 0.00 sec) 3 . 4 . MySQL > show master status; 5 . + -- ----------------+----------+--------------+------------------+ 6 . | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 7 . + -- ----------------+----------+--------------+------------------+ 8 . | MySQL - bin. 000003 | 374 | | | 9 . + -- ----------------+----------+--------------+------------------+ 10 . 1 row in set ( 0.00 sec)

  在192.168.1.201上,將192.168.1.202設爲本身的主服務器

  
  
  
  
  
1 . 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 ; 2 . Query OK, 0 rows affected ( 0.05 sec) 3 . 4 . MySQL > start slave; 5 . Query OK, 0 rows affected ( 0.00 sec) 6 . 7 . MySQL > show slave status\G 8 . *************************** 1 . row *************************** 9 . Slave_IO_State: Waiting for master to send event 10 . Master_Host: 192.168 . 1.202 11 . Master_User: replication 12 . Master_Port: 3306 13 . Connect_Retry: 60 14 . Master_Log_File: MySQL - bin. 000003 15 . Read_Master_Log_Pos: 374 16 . Relay_Log_File: MySQL - master1 - relay - bin. 000002 17 . Relay_Log_Pos: 235 18 . Relay_Master_Log_File: MySQL - bin. 000003 19 . Slave_IO_Running: Yes 20 . Slave_SQL_Running: Yes 21 . Replicate_Do_DB: 22 . Replicate_Ignore_DB: 23 . Replicate_Do_Table: 24 . Replicate_Ignore_Table: 25 . Replicate_Wild_Do_Table: 26 . Replicate_Wild_Ignore_Table: 27 . Last_Errno: 0 28 . Last_Error: 29 . Skip_Counter: 0 30 . Exec_Master_Log_Pos: 374 31 . Relay_Log_Space: 235 32 . Until_Condition: None 33 . Until_Log_File: 34 . Until_Log_Pos: 0 35 . Master_SSL_Allowed: No 36 . Master_SSL_CA_File: 37 . Master_SSL_CA_Path: 38 . Master_SSL_Cert: 39 . Master_SSL_Cipher: 40 . Master_SSL_Key: 41 . Seconds_Behind_Master: 0 42 . 1 row in set ( 0.00 sec)

  四、MySQL同步測試

  如上述均正確配置,如今任何一臺MySQL上更新數據都會同步到另外一臺MySQL,MySQL同步在此再也不演示

  2、keepalived安裝及配置

  一、192.168.1.201服務器上keepalived安裝及配置

  安裝keepalived

  
  
  
  
  
1 . #tar zxvf keepalived - 1.1 . 20 .tar.gz 2 . #cd keepalived - 1.1 . 20 3 . #. / configure -- prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 4 . #make && make install

  配置keepalived

  咱們本身在新建一個配置文件,默認狀況下keepalived啓動時會去/etc/keepalived目錄下找配置文件

  
  
  
  
  
1 . #mkdir / etc / keepalived 2 . #vi / etc / keepalived / keepalived.conf 3 . ! Configuration File for keepalived 4 . global_defs { 5 . notification_email { 6 . luwenju @live .cn 7 . } 8 . notification_email_from luwenju @live .cn 9 . smtp_server 127.0 . 0.1 10 . smtp_connect_timeout 30 11 . router_id MySQL - ha 12 . } 13 . 14 . vrrp_instance VI_1 { 15 . state BACKUP #兩臺配置此處均是BACKUP 16 . interface eth0 17 . virtual_router_id 51 18 . priority 100 #優先級,另外一臺改成90 19 . advert_int 1 20 . nopreempt #不搶佔,只在優先級高的機器上設置便可,優先級低的機器不設置 21 . authentication { 22 . auth_type PASS 23 . auth_pass 1111 24 . } 25 . virtual_ipaddress { 26 . 192.168 . 1.200 27 . } 28 . } 29 . 30 . virtual_server 192.168 . 1.200 3306 { 31 . delay_loop 2 #每一個2秒檢查一次real_server狀態 32 . lb_algo wrr #LVS算法 33 . lb_kind DR #LVS模式 34 . persistence_timeout 60 #會話保持時間 35 . protocol TCP 36 . real_server 192.168 . 1.201 3306 { 37 . weight 3 38 . notify_down / usr / local / MySQL / bin / MySQL.sh #檢測到服務down後執行的腳本 39 . TCP_CHECK { 40 . connect_timeout 10 #鏈接超時時間 41 . nb_get_retry 3 #重連次數 42 . delay_before_retry 3 #重連間隔時間 43 . connect_port 3306 #健康檢查端口 44 . } 45 . }

  編寫檢測服務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

  
  
  
  
  
1 . # / usr / local / keepalived / sbin / keepalived –D 2 . #ps - aux | grep keepalived

  測試

  找一臺局域網PC,而後去ping  MySQL的VIP,這時候MySQL的VIP是能夠ping的通的

  中止MySQL服務,看keepalived健康檢查程序是否會觸發咱們編寫的腳本

  二、192.168.1.202上keepalived安裝及配置

  安裝keepalived

  
  
  
  
  
1 . #tar zxvf keepalived - 1.1 . 20 .tar.gz 2 . #cd keepalived - 1.1 . 20 3 . #. / configure -- prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 4 . #make && make install

  配置keepalived

  這臺配置和上面基本同樣,但有三個地方不一樣:優先級爲90、無搶佔設置、real_server爲本機IP

  
  
  
  
  
1 . #mkdir / etc / keepalived 2 . #vi / etc / keepalived / keepalived.conf 3 . ! Configuration File for keepalived 4 . global_defs { 5 . notification_email { 6 . luwenju @live .cn 7 . } 8 . notification_email_from luwenju @live .cn 9 . smtp_server 127.0 . 0.1 10 . smtp_connect_timeout 30 11 . router_id MySQL - ha 12 . } 13 . 14 . vrrp_instance VI_1 { 15 . state BACKUP 16 . interface eth0 17 . virtual_router_id 51 18 . priority 90 19 . advert_int 1 20 . authentication { 21 . auth_type PASS 22 . auth_pass 1111 23 . } 24 . virtual_ipaddress { 25 . 192.168 . 1.200 26 . } 27 . } 28 . 29 . virtual_server 192.168 . 1.200 3306 { 30 . delay_loop 2 31 . lb_algo wrr 32 . lb_kind DR 33 . persistence_timeout 60 34 . protocol TCP 35 . real_server 192.168 . 1.202 3306 { 36 . weight 3 37 . notify_down / usr / local / MySQL / bin / MySQL.sh 38 . TCP_CHECK { 39 . connect_timeout 10 40 . nb_get_retry 3 41 . delay_before_retry 3 42 . connect_port 3306 43 . } 44 . }

  編寫檢測服務down後所要執行的腳本

  
  
  
  
  
1 . #vi / usr / local / MySQL / bin / MySQL.sh 2 . #! / bin / sh 3 . pkill keepalived 4 . #chmod + x / usr / local / MySQL / bin / MySQL.sh 5 . 6 . 啓動keepalived 7 . # / usr / local / keepalived / sbin / keepalived –D 8 . #ps - aux | grep keepalived

  測試

  中止MySQL服務,看keepalived健康檢查程序是否會觸發咱們編寫的腳本

  3、測試

  MySQL遠程登陸測試

  咱們找一臺安裝有MySQL客戶端的windows,而後登陸VIP,看是否能登陸,在登陸之兩臺MySQL服務器都要受權容許從遠程登陸

  
  
  
  
  
1 . MySQL > grant all privileges on * . * to ' root ' @ ' % ' identified by ' 123456 ' ; 2 . Query OK, 0 rows affected ( 0.00 sec) 3 . 4 . MySQL > flush privileges ; 5 . Query OK, 0 rows affected ( 0.00 sec)

  使用客戶端登陸VIP測試

  
  
  
  
  
1 . C:\MySQL\bin > MySQL.exe - uroot - p123456 - h192. 168.1 . 200 - P3306 2 . Welcome to the MySQL monitor. Commands end with ; or \g. 3 . Your MySQL connection id is 224 4 . Server version: 5.0 . 89 - log Source distribution 5 . 6 . Type ' help; ' or ' \h ' for help. Type ' \c ' to clear the current input statement. 7 . 8 . 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是誰都不屬於的)

  
  
  
  
  
1 . MySQL > show databases; 2 . ERROR 2006 (HY000): MySQL server has gone away 3 . No connection. Trying to reconnect... 4 . Connection id: 592 5 . Current database : *** NONE *** 6 . 7 . + -- ------------------+ 8 . | Database | 9 . + -- ------------------+ 10 . | information_schema | 11 . | MySQL | 12 . | test | 13 . + -- ------------------+ 14 . 3 rows in set ( 9.01 sec)

  後話:世間萬事萬物,都不具有絕對的完美,就像上面的MySQL-HA同樣,keepalived只能作到對3306的健康檢查,可是作不到好比像MySQL複製中的slave-SQL、slave-IO進程的檢查。因此要想作到一些細緻的健康檢查,還得須要藉助額外的監控工具,好比nagios,而後用nagios實現短信、郵件報警,從而可以有效地解決問題。

相關文章
相關標籤/搜索