Mysql+keepalived主主切換
一,環境介紹
網路結構:
VIP :192.168.1.30
MYSQL A:192.168.1.21
MYSQL B:192.168.1.22
二、mysql主主同步
要實現mysql+keepalived主主切換,首先要實現的就是兩臺mysql伺服器的主主同步,查看http://smalldeng.blog.51cto.com/1038075/1151900
三,配置keepalived
1,安裝yum -y install keepalived
master A配置檔案
[root@master ~]# cat /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-cluster #修改為自己的主機名
}
##################第一部分###################
vrrp_instance VI_1 {
state BACKUP #都修改成BACKUP
interface eth0
virtual_router_id 60 #預設51 主從都修改為60
priority 100 #優先順序(1-254之間),另一臺改為90,備用節點必須比主節點優先順序低。
advert_int 1
nopreempt #不搶佔資源,意思就是它活了之後也不會再把主搶回來
authentication {
#設定驗證資訊,兩個節點必須一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.30
}
}
##################第二部分###################
virtual_server 192.168.1.30 3306 {
delay_loop 6
lb_algo wrr #LVS演算法
lb_kind DR#LVS模式
nat_mask 255.255.255.0
persistence_timeout 50 #會話保持時間
protocol TCP
real_server 192.168.1.21 3306 {
weight 1
notify_down /root/mysql.sh #檢測到服務down後執行的指令碼
TCP_CHECK {
connect_timeout 10 #連線超時時間
nb_get_retry 3#重連次數
connect_port 3306#健康檢查埠
}
}
}
master B配置檔案,去掉nopreempt,設定優先順序,真實IP
[root@slave1 ~]# cat /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-cluster #修改為自己的主機名
}
##################第一部分###################
vrrp_instance VI_1 {
state BACKUP #都修改成BACKUP
interface eth0
virtual_router_id 60 #預設51 主從都修改為60
priority 80 #在mysql-ha1上LVS上修改成100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.30
}
}
##################第二部分###################
virtual_server 192.168.1.30 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.1.22 3306 {
weight 1
notify_down /root/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
connect_port 3306
}
}
}
3,建立mysql.sh指令碼
vim /root/mysql.sh
#!/bin/bash
pkill keepalived
#chmod +x /root/mysql.sh
4,需要安裝ipvsadm與設定arp協議的響應,在兩臺機器都操作
# vim /etc/sysctl.conf 在最後新增,也可以直接修改cat /proc/sys/net/ipv4/conf/eth0/
############arp##############
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
#sysctl -p
四,檢視虛擬ip在哪,哪臺優先順序高,虛擬IP就會在哪
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:af:00:a6 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.21/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.30/32 scope global eth0
inet6 fe80::20c:29ff:feaf:a6/64 scope link
valid_lft forever preferred_lft forever
[root@slave1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:3d:8b:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.22/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe3d:8bc7/64 scope link
valid_lft forever preferred_lft forever
五,驗證:通過window遠端連線虛擬IP,在兩臺mysql上都需要儲存相同的一個使用者和密碼用於遠端連線,如果能連線表示成功,將master A上的mysql停止,測試是否還能連線。中間如果有錯誤可以檢視/var/log/message日誌。