MySQL 主主複製

pursuer.chen發表於2016-03-16

介紹

環境

OS:CentOS 6.7,MySQL 5.6

Master:192.168.137.10  hosts:master

Backup:192.168.137.20 hosts:slave

VIP:192.168.137.50

 

Master配置

修改cnf配置檔案:

server_id =10

log_bin=/usr/local/mysql/data/mysql_bin

max_binlog_size = 1G

binlog_format=MIXED

#binlog-do-db=

binlog-ignore-db=test #這裡記住不要加入系統資料庫,多個資料庫以逗號隔開

#replicate-do-db=

replicate-ignore-db=test

log-slave-updates

slave-skip-errors=all

auto_increment_increment=2

auto_increment_offset=1

重啟mysql

Service mysql restart

檢視binglog位置

SHOW MASTER STATUS

由於是剛建立預設是:mysql_bin.000001:120

建立複製使用者:

mysql –uroot -proot

GRANT  REPLICATION SLAVE ON *.* TO 'repl'@'192.168.80.%' IDENTIFIED  BY 'repl';
flush  privileges;

change master to 

master_host = '192.168.137.20', 

master_user = 'repl', 

master_password = 'repl', 

master_log_file = 'mysql_bin.000001',  #注意binlog的命名

master_log_pos = 120;

啟動複製,先在132上建立完複製使用者再執行

start slave;

Backup配置

server_id =20

log_bin=/usr/local/mysql/data/mysql_bin

max_binlog_size = 1G

binlog_format=MIXED

#binlog-do-db=

binlog-ignore-db=test

#replicate-do-db=

replicate-ignore-db=test #這裡記住不要加入系統資料庫,多個資料庫以逗號隔開

log-slave-updates

slave-skip-errors=all

auto_increment_increment=2

auto_increment_offset=2

重啟mysql

Service mysql restart

檢視binglog位置

SHOW MASTER STATUS

由於是剛建立預設是:mysql_bin.000001:120

 

建立複製使用者:mysql –uroot -proot

GRANT  REPLICATION SLAVE ON *.* TO 'repl'@'192.168.80.%' IDENTIFIED  BY 'repl';
flush  privileges;

change master to 

master_host = '192.168.137.10', 

master_user = 'repl', 

master_password = 'repl', 

master_log_file = 'mysql-bin.000001',  #注意binlog的命名

master_log_pos = 120;

 

啟動複製

start slave;

master keeplived安裝配置

通過keeplived管理虛擬IP,並執行故障切換。

yum install openssl openssl-devel -y

wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz

tar -xvf keepalived-1.2.19.tar.gz

cd keepalived-1.2.19

./configure --prefix=/usr/local/keepalived

make && make install

配置啟動項

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/bin/

keepalived配置

master

vim /etc/keepalived/keepalived.conf

#! Configuration File forkeepalived
global_defs {
notification_email {
test@163.com
 }
notification_email_from  ha@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL      #組標識,雙主相同
 }
vrrp_instance VI_1 {
 state BACKUP           #兩臺都設定BACKUP
 interface eth0
 virtual_router_id 51       #主備相同
 priority 100           #優先順序,backup設定120 master設定150
 advert_int 1
 nopreempt             #不主動搶佔資源,兩臺都設定
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
 192.168.137.50  #虛擬IP
 }
}
virtual_server 192.168.137.50 3306 {
 delay_loop 2
 #lb_algo rr              #LVS演算法,用不到,我們就關閉了
 #lb_kind DR              #LVS模式如果不關閉備用伺服器不能通過VIP連線主MySQL
 persistence_timeout 50  #同一IP的連線60秒內被分配到同一臺真實伺服器
 protocol TCP
 real_server 192.168.137.10 3306 { #檢測本地mysql,backup也要寫檢測本地mysql
 weight 3
 notify_down /usr/local/keepalived/mysql.sh    #當mysq服down時,執行此指令碼,殺死keepalived實現切換
 TCP_CHECK {
 connect_timeout 3    #連線超時
 nb_get_retry 3       #重試次數
 delay_before_retry 3 #重試間隔時間

  }
}
}

 

backup

vim /etc/keepalived/keepalived.conf

#! Configuration File forkeepalived
global_defs {
notification_email {
test@163.com
 }
notification_email_from  ha@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL      #組標識,雙主相同
 }
vrrp_instance VI_1 {
 state BACKUP           #兩臺都設定BACKUP
 interface eth0
 virtual_router_id 51       #主備相同
 priority 90           #優先順序,backup設定120 master設定150
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
 192.168.137.50
 }
}

virtual_server 192.168.137.50 3306 {
delay_loop 2
 #lb_algo rr              #LVS演算法,用不到,我們就關閉了
 # #lb_kind DR              #LVS模式如果不關閉備用伺服器不能通過VIP連線主MySQL
persistence_timeout 50  #同一IP的連線60秒內被分配到同一臺真實伺服器
protocol TCP
real_server 192.168.137.20 3306 { #檢測本地mysql,backup也要寫檢測本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh    #當mysq服down時,執行此指令碼,殺死keepalived實現切換
TCP_CHECK {
connect_timeout 3    #連線超時
nb_get_retry 3       #重試次數
delay_before_retry 3 #重試間隔時間

     }
   }
 }

注意:nopreempt(不主動搶佔)針對優先順序高的一方是有效的;如果不使用 nopreempt 那麼優先順序高的伺服器每次重啟都會把VIP搶佔過去,這樣會導致VIP頻繁切換。正常情況下VIP切換到了哪一臺伺服器就把那臺作為主不需要再搶佔回去,目前測試nopreempt針對優先順序低的一方沒有效果,優先順序低的不加上該引數也不會把優先順序高的VIP搶佔走。   

將keepalived加入系統啟動項

chkconfig --add keepalived

chkconfig  keepalived on

vim /usr/local/keepalived/mysql.sh

#!/bin/bash

pkill keepalived

修改檔案可執行許可權

chmod +x /usr/local/keepalived/mysql.sh

啟動Keepalived

service keepalived start

檢視VIP狀態

ip add

注意:在檔案配置中使用備註千萬要注意,要確保你的備註被識別為備註,之前就遇到過這樣的問題在程式碼後使用#備註由於#和實際的程式碼挨在一起被程式當成了程式碼導致整個配置出現了問題,特別是這種問題很難找,所以程式碼中使用備註千萬要小心。

總結

 

 

備註:

    作者:pursuer.chen

    部落格:http://www.cnblogs.com/chenmh

本站點所有隨筆都是原創,歡迎大家轉載;但轉載時必須註明文章來源,且在文章開頭明顯處給明連結。

《歡迎交流討論》

相關文章