Mysql + keepalive高可用搭建
系統(tǒng)環(huán)境:centos6.8
Ip:192.168.137.36 主庫(kù)
192.168.137.38 從庫(kù)
VIP(虛擬ip/浮動(dòng)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主庫(kù)修改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從庫(kù)修改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
}
}
}
保存退出
上面的配置簡(jiǎn)單說(shuō)明:
state BACKUP??表示為備模式,nopreempt?為不搶占,雙方都配為備模式,并且不搶占,可以避免“腦裂”問(wèn)題,priority 100?為權(quán)重,數(shù)字越高,權(quán)重越高,當(dāng)雙方都配為BACKUP模式,并且配置nopreempt時(shí),keepalived靠這個(gè)數(shù)字來(lái)判斷誰(shuí)是主,誰(shuí)是備
主從都要?jiǎng)?chuàng)建/data/sh/mysql.sh腳本
mkdir /data/sh
vim?/data/sh/mysql.sh?加入以下內(nèi)容:
#!/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模塊有沒(méi)有加載,如果看到下面的內(nèi)容,就表示加載成功:
/etc/init.d/mysqld? start??#啟動(dòng)mysql
/etc/init.d/keepalive? start???#啟動(dòng)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的主從高可用就做好了,可以在36上測(cè)試挺掉mysql服務(wù),看看vip會(huì)不會(huì)漂移到38上,一般來(lái)說(shuō)都是沒(méi)問(wèn)題的,如果有問(wèn)題,請(qǐng)檢查你的配置,步驟是不是有錯(cuò)誤,還有selinux,防火墻是否關(guān)閉等
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。