Keepalived + MySQL雙主配置方案

Keepalived + MySQL雙主配置方案mysql

 

  1. 系統環境以及軟件版本

   

主機名sql

操做系統版本數據庫

Keepalived版本bash

Mysql版本服務器

IP地址socket

Master1函數

REDHAT6.5oop

2.0.0spa

5.6.19操作系統

172.16.1.1(內網ip)

10.10.10.1(數據傳輸ip)

Master2

REDHAT6.5

2.0.0

5.6.19

172.16.1.2(內網ip)

10.10.10.2(數據傳輸ip)

 

  1. MySQL配置

MySQL主主複製結構區別於主從複製結構。在主主複製結構中,兩臺服務器的任何一臺上面的數據庫存發生了改變都會同步到另外一臺服務器上,這樣兩臺服務器互爲主從,而且都能向外提供服務。

2.1 master1 上的配置

basedir = /usr/local/mysql5.6.19

datadir = /usr/local/mysql5.6.19/data

socket = /tmp/mysql.sock

pid-file = /usr/local/mysql5.6.19/data/mysql.pid

bind-address = 0.0.0.0

log_bin = /usr/local/mysql5.6.19/mysql-bin.log #開啓二進制日誌

server-id = 1 #任意天然數n,只要保證兩臺MySQL主機不重複便可

#replicate-do-db = test #要同步的數據庫,默認全部庫 (這裏註釋掉,同步全部庫)

binlog-ignore-db = mysql #忽略mysql

binlog-ignore-db = information_schema #忽略information_schema

auto-increment-increment = 2 #步進值,有n臺主MySQL就填n

auto-increment-offset = 1 #起始值。第n臺主MySQL,此時爲第1MySQL

 

2.2  master2 上的配置

    

basedir = /usr/local/mysql5.6.19

datadir = /usr/local/mysql5.6.19/data

socket = /tmp/mysql.sock

pid-file = /usr/local/mysql5.6.19/data/mysql.pid

bind-address = 0.0.0.0

log_bin = /usr/local/mysql5.6.19/mysql-bin.log #開啓二進制日誌

server-id = 2 #任意天然數n,只要保證兩臺MySQL主機不重複便可

#replicate-do-db = test #要同步的數據庫,默認全部庫

binlog-ignore-db = mysql #忽略mysql

binlog-ignore-db = information_schema #忽略information_schema

auto-increment-increment = 2 #步進值,有n臺主MySQL就填n

auto-increment-offset = 2 #起始值。第n臺主MySQL,此時爲第1MySQL

 

注:二都只有server-id不一樣和 auto-increment- offset不一樣
auto-increment-offset是用來設定數據庫中自動增加的起點的,回爲這兩能服務器都設定了一次自動增加值2,因此它們的起點必須得不一樣,這樣才能避免兩臺服務器數據同步時出現主鍵衝突
replicate-do-db 指定同步的數據庫,咱們只在兩臺服務器間同步test數據庫
另:auto-increment-increment的值應設爲整個結構中服務器的總數,本案例用到兩臺服務器,因此值設爲2

 

 

 

2.3 相互受權用戶(A服務器受權一個容許B訪問的用戶,反之亦然)

在服務器master1172.16.1.1)上


 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'10.10.10.2' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;


在服務器master2172.16.1.2)上

 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'10.10.10.1' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;

 

 

2.4 互告bin-log信息

   

 

在服務器master1
 
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000006 |      106 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

 

 

 

在服務器master2

 
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000008 |      192 |      | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

 


在服務器master1上執行


 
mysql> change master to master_host='10.10.10.2',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;


在服務器master2上執行


 
mysql> change master to master_host='10.10.10.1',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;

 

2.5 在兩服務器都執行如下命令

mysql> start slave;

 

2.6查看狀態

   show slave status\G;

Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

標紅這兩個必須是Yes

 

 

 

 

  1. Keepalived 配置

Master1配置信息

! Configuration File for keepalived

 

global_defs {

   notification_email {

test@163.com    

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id MYSQL_HA

   vrrp_skip_check_adv_addr

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

Vrrp_script check_mysql {

Script 「/usr/local/keepalived-2.0.0/etc/keepalived/check_mysql.sh」

Interval 10

Weight 5

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0 #虛擬ip工做在哪一個接口

    virtual_router_id 51 

    priority 100  #優先級

advert_int 1

Nopreempt #不搶佔資源

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.1.100/24 dev eth0 label eth0:0

}

Track_script {

Check_mysql #調用上面的函數

}

Notify_master 「/usr/local/keepalived-2.0.0/etc/keepalived/notify.sh master 192.168.67.153」

Notify_backup 「/usr/local/keepalived-2.0.0/etc/keepalived/notify.sh backup 192.168.67.153」

Notify_fault 「/usr/local/keepalived-2.0.0/etc/keepalived/notify.sh fault 192.168.67.153」

}

 

virtual_server 172.16.1.100 3306 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP

 

    real_server 10.10.10.1 3306 {

        weight 1

        TCP_CHECK {

            connect_timeout 10    #鏈接超時時間    

            nb_get_retry 3      #重連次數    

            delay_before_retry 3   #重連間隔時間    

            connect_port 3306

            retry 3

        }

    }

}

 

 

 

Master2 配置

! Configuration File for keepalived

 

global_defs {

   notification_email {

     zhanglei_py@163.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id MYSQL_HA

   vrrp_skip_check_adv_addr

   #vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.1.100/24 dev eth0 label eth0:0

    }

}

 

virtual_server 172.16.1.100 3306 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP

 

    real_server 10.10.10.2 3306 {

        weight 1

        TCP_CHECK {

            connect_timeout 10    #鏈接超時時間    

            nb_get_retry 3      #重連次數    

            delay_before_retry 3   #重連間隔時間    

            connect_port 3306

            retry 3 

        }

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

備註:監測腳本

Notify.sh

#!/bin/bash

#

contact="zhanglei_py@163.com"

 

 

Usage(){

echo "Usage: `basename $0`{master|backup|fault} VIP"

}

 

Notify(){

subject="`hostname` state changed to $1"

mailbody="`date "+%F %T"`: `hostname` state change to $1,$VIP floating."

echo $mailbody |mail -s "$subject" $contact

        echo $mailbody >>/var/log/keepalived.log

 

}

 

[ $# -lt 2 ] && Usage && exit

 

VIP=$2

 

case $1 in

master)

Notify master

;;

 

backup)

        Notify backup

;;

 

fault)

Notify fault

;;

*)

Usage

exit 1

;;

Esac

 

 

 

 

Check_mysql.sh

#!/bin/bash

check_mysql_status(){

status=`netstat -tnlp|grep 3306`

if [ $? != 0 ]

then

echo "down" >>/var/log/keepalived.log

        Service keepalived stop

fi

}

 

main(){

check_mysql_status

}

main

相關文章
相關標籤/搜索