使用KeepAlived搭建MySQL高可用環境

來源:互聯網
上載者:User

使用KeepAlived搭建MySQL高可用環境

使用KeepAlived搭建MySQL的高可用環境。
 首先搭建MySQL的主從複製
 在Master開啟binlog,建立複製帳號,
 然後在Slave輸入命令
 

change master to
master_host='192.168.1.70',
master_port=3306,
master_user='xx',
master_password='xx';
 然後使用start slave開啟複製。
 
然後編譯安裝KeepAlived
 進入keepalived-1.2.12目錄
 然後使用
 ./configure
 make && make install
 
然後在Master伺服器編輯KeepAlived的設定檔
 vim /etc/keepalived/keepalived.conf
 

! Configuration File for keepalived


global_defs {
 
    router_id HA_MySQL
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.199
    }
}

 
virtual_server 192.168.1.199 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 192.168.1.70 3306 {
        weight 3
        notify_down /root/shutdown.sh
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        } 
    }
}
 然後編輯Slave的設定檔
 vim /etc/keepalived/keepalived.conf
 
! Configuration File for keepalived
global_defs {
    router_id HA_MySQL
 }

 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.199
    }
 }

 
virtual_server 192.168.1.199 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 192.168.1.80 3306 {
        weight 3
        notify_down /root/shutdown.sh
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
 }
 其中
 priority                      表示優先順序
 virtual_ipaddress      虛擬IP地址(VIP)
 delay_loop                每個2秒檢查一次real_server狀態
 notify_down              檢測到服務down後執行的指令碼
connect_timeout      連線逾時時間
 nb_get_retry            重連次數
 delay_before_retry  重連間隔時間
 connect_port            健全狀態檢查連接埠
 
shutdown.sh 可以考慮加入郵件警示的功能。
 

#!/bin/bash
pkill keepalived
 
在兩個伺服器上啟動MySQL和KeepAlived服務
 service mysql start
 service keepalived start
 
Master的server_id為1
 Slave的server_id為2
 
然後 串連VIP的MySQL,可以看到已經串連到了Master伺服器(server_id為1)

如果kill掉Master的MySQL,KeepAlived會自動轉移到Slave
 
在Master伺服器上執行
 killall mysqld
 
然後再次查看server_id,
 短暫的失去串連之後,再次串連上VIP,server_id已經變為2,說明VIP已經指向了Slave
 

nopreempt參數表示Master恢複正常之後,是否將VIP繼續指向Master
 這樣的話,會再次引發切換。
 
兩台伺服器的KeepAlived會有心跳檢測,
 如果Master的MySQL服務掛了(3306連接埠掛了),那麼他會選擇自殺.
 Slave的KeepAlived通過心跳檢測發現這個情況,就會將VIP的請求接管。
 

KeepAlived還有很多參數沒有明白是什麼意思
 生產環境的切換指令碼,在Slave提升為Master之後,應該等待所有的中繼日誌應用完畢,否則可能遺失資料

CentOS 6.3下Haproxy+Keepalived+Apache配置筆記

Haproxy + KeepAlived 實現WEB群集 on CentOS 6

Keepalived+Haproxy配置高可用負載平衡

Haproxy+Keepalived構建高可用負載平衡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.