**
mysql基于keepalived双主高可用*
环境:两台Linux服务器
mysql基于keepalived双主高可用和
在双主的基础上
主A配置
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER-A
}
vrrp_script mysql {
script “/opt/ mysql.sh”
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql
}
virtual_ipaddress {
/ 192.168.1.254
}
}
脚本
[root@localhost ~]# cat /opt/mysql.sh
#!/bin/bash
counter=
(
n
e
t
s
t
a
t
−
n
a
∣
g
r
e
p
"
L
I
S
T
E
N
"
∣
g
r
e
p
"
3306
"
∣
w
c
−
l
)
i
f
[
"
(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "
(netstat−na∣grep"LISTEN"∣grep"3306"∣wc−l)if["{counter}" -eq 0 ]; then
systemctl stop keepalived
fi
[root@localhost ~]# chmod +x /opt/mysql.sh
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ip a | grep ens32
[root@localhost ~]# mysql
MariaDB [(none)]> grant all on . to ‘root’@‘192.168.1.%’ identified by ‘123456’;
MariaDB [(none)]> flush privileges;
主B配置
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER-B
}
vrrp_script mysql {
script “/opt/mysql.sh”
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql
}
virtual_ipaddress {
192.168.1.254
}
}
脚本
[root@localhost ~]# cat /opt/mysql.sh
#!/bin/bash
counter=
(
n
e
t
s
t
a
t
−
n
a
∣
g
r
e
p
"
L
I
S
T
E
N
"
∣
g
r
e
p
"
3306
"
∣
w
c
−
l
)
i
f
[
"
(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "
(netstat−na∣grep"LISTEN"∣grep"3306"∣wc−l)if["{counter}" -eq 0 ]; then
systemctl stop keepalived
fi
[root@localhost ~]# chmod +x /opt/mysql.sh
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ip a | grep ens32
[root@localhost ~]# mysql
MariaDB [(none)]> grant all on . to ‘root’@‘192.168.1.%’ identified by ‘123456’;
MariaDB [(none)]> flush privileges;