Mysql + keepalive高可用搭建

weixin_34127717發表於2017-11-15
  Mysql + keepalive高可用搭建

系統環境:centos6.8
Ip:192.168.137.36 主庫
192.168.137.38 從庫

VIP(虛擬ip/浮動ip):192.168.137.100

軟體版本:mysql 5.7.20
Keepalived 1.2.13

主從搭建略
Mysql1
vim /etc/my.cnf
binlog-do-db=test
log-bin=mysql-bin
server-id=1
gtid-mode=ON
enforce-gtid-consistency

Mysql2
Vim /etc/my.cnf
gtid-mode=ON
enforce-gtid-consistency
log-bin=mysql-bin
log-slave-updates=ON
server-id=3
binlog-do-db=test

安裝keepalived
yum install keepalived

在192.168.137.36主庫修改keepalived的配置檔案
注意:加顏色部分為修改部分

[root@zabbix ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs{ 
notification_email { 
root@localhost 

smtp_server 127.0.0.1 
smtp_connect_timeout 30 
router_id LVS_DEVEL 

#VIP1 
vrrp_instance VI_1 { 
state BACKUP 
interface eth0 
lvs_sync_daemon_inteface eth0 
virtual_router_id 151 
priority 100 
advert_int 5 
nopreempt 
authentication { 
auth_type PASS 
auth_pass 2222 

virtual_ipaddress { 
192.168.137.100 


virtual_server 192.168.137.100 3306 { 
delay_loop 6 
lb_algo wrr 
lb_kind DR 
persistence_timeout 60 
protocol TCP 
real_server 192.168.137.36 3306 { 
weight 100 
notify_down /data/sh/mysql.sh 
TCP_CHECK { 
connect_timeout 10 
nb_get_retry 3 
delay_before_retry 3 
connect_port 3306 


}

儲存退出
在192.168.137.38從庫修改keepalived的配置檔案
[root@localhost sh]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs{ 
notification_email { 
root@localhost 

smtp_server 127.0.0.1 
smtp_connect_timeout 30 
router_id LVS_DEVEL 

#VIP1 
vrrp_instance VI_1 { 
state BACKUP 
interface eth0 
lvs_sync_daemon_inteface eth0 
virtual_router_id 151 
priority 98 
advert_int 5 
nopreempt 
authentication { 
auth_type PASS 
auth_pass 2222 

virtual_ipaddress { 
192.168.137.100 


virtual_server 192.168.137.100 3306 { 
delay_loop 6 
lb_algo wrr 
lb_kind DR 
persistence_timeout 60 
protocol TCP 
real_server 192.168.137.38 3306 { 
weight 98 
notify_down /data/sh/mysql.sh 
TCP_CHECK { 
connect_timeout 10 
nb_get_retry 3 
delay_before_retry 3 
connect_port 3306 


}
儲存退出
上面的配置簡單說明:
state BACKUP  表示為備模式,nopreempt 為不搶佔,雙方都配為備模式,並且不搶佔,可以避免“腦裂”問題,priority 100 為權重,數字越高,權重越高,當雙方都配為BACKUP模式,並且配置nopreempt時,keepalived靠這個數字來判斷誰是主,誰是備
主從都要建立/data/sh/mysql.sh指令碼
mkdir /data/sh
vim /data/sh/mysql.sh 加入以下內容:
#!/bin/bash
/etc/init.d/keepalived  stop
 
chmod  755  /data/sh/mysql.sh
modprobe  ip_vs  #載入ip_vs模組 虛擬IP要用
lsmod |  grep  ip_vs  檢視ip_vs模組有沒有載入,如果看到下面的內容,就表示載入成功:
Mysql + keepalive高可用搭建

/etc/init.d/mysqld  start  #啟動mysql
/etc/init.d/keepalive  start   #啟動keepalived
在36上的操作和38上一樣,只是keepalived.conf配置檔案裡priority 100 改為 priority 98
 
real_server 192.168.137.36 改為 real_server 192.168.137.38其他不變。
 
檢視VIP情況命令: ip a
Mysql + keepalive高可用搭建
 
至此mysql的主從高可用就做好了,可以在36上測試挺掉mysql服務,看看vip會不會漂移到38上,一般來說都是沒問題的,如果有問題,請檢查你的配置,步驟是不是有錯誤,還有selinux,防火牆是否關閉等









本文轉自去輕狂書生51CTO部落格,原文連結: http://blog.51cto.com/8999a/2047477,如需轉載請自行聯絡原作者



相關文章