8.8. Mysql双主+keepalived集群架构¶
8.8.1. keepalived主机配置:¶
[root@nginx_keepalived_master sh]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost #健康检查报告通知邮箱
}
notification_email_from keepalived@localhost #发送邮件的地址
smtp_server 127.0.0.1 #邮件服务器
smtp_connect_timeout 30
route_id LVS_DEVEL
}
vrrp_script chk_mysql
{
script "/data/sh/check_mysql.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 151
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.1.200/24
}
}
8.8.2. keepalived备机配置:¶
root@nginx_keepalived_backup sh]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost #健康检查报告通知邮箱
}
notification_email_from keepalived@localhost #发送邮件的地址
smtp_server 127.0.0.1 #邮件服务器
smtp_connect_timeout 30
route_id LVS_DEVEL
}
vrrp_script chk_mysql
{
script "/data/sh/check_mysql.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 151
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.1.200/24
}
}
8.8.3. 主备机监控脚本¶
cat /data/sh/check_mysql.sh
#!/bin/bash
# auto check mysql process
NUM=$(ps aux | grep mysql| grep -v grep | grep -v check|wc -l)
if [[ $NUM -eq 0 ]];then
/etc/init.d/keepalived stop
fi
或者
[root@master1 ~]# vim /opt/check_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
8.8.4. 检测VIP连接mysql是否成功¶
[root@nginx_keepalived_backup sh]# mysql -h 192.168.1.200 -utest -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Mysql如果出现宕机,VIP会自动进行切换。
参考文献: