您好,登錄后才能下訂單哦!
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模塊有沒有加載,如果看到下面的內容,就表示加載成功:
/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的主從高可用就做好了,可以在36上測試挺掉mysql服務,看看vip會不會漂移到38上,一般來說都是沒問題的,如果有問題,請檢查你的配置,步驟是不是有錯誤,還有selinux,防火墻是否關閉等
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。