数据库这一层需要做到避免单点故障可以是主从备份和主主备份,主主备份可能有性能损耗和数据同步的问题。这里记录下主从备份,
mysql进行备份之前确保mysql的版本是一样的,我这里用的都是mysql5.7.18
1、原文博客地址:http://qizhanming.com/blog/2017/06/20/how-to-config-mysql-57-master-slave-replication-on-centos-7
这篇博客已经写的很好了,不再记录了。
mysql备份好了以后,操作主mysql上面的数据可以看到从数据库的数据实时同步的。
2、那问题来了,如果在项目中主mysql挂了,我们是要手动切换msyql吗,这样会比较麻烦的,而且这段时间项目是不能用的。这里用keepalived实现IP的vip漂移,
keepalived就是一个软件,在主mysql和从mysql上面都安装后,会有一个虚拟IP在其中的一台服务器上面,如果这台服务器宕机了,这个IP就会出现在另一台服务器上面,这是keepalived的功能,能够判断是否可用和权重大小,然后自动漂移IP,这个IP是虚拟IP,我们可以自己随意设置,不过要跟主,从mysql的IP在同一个网段,云服务器好像需要再设置下默认是关闭的,如果是主机是可以直接用的。
1、安装keepalived
# yum install keepalived –y
2、备份keepalived.conf
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.back
3、编辑keepalived.conf
global_defs {
notification_email{
xxx@qq.com
}
notification_email_from waterlufei123@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KeepAlive_Mysql
}
vrrp_script vs_mysql_82 {
script "/etc/keepalived/checkMySQL.sh"
interval 60
}
vrrp_instance VI_82 {
state BACKUP
nopreempt
interface eth1
virtual_router_id 82
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
vs_mysql_82
}
virtual_ipaddress {
192.168.108.156/24 brd 192.168.108.255 dev eth1 label eth1:1
}
}
virtual_server 192.168.108.156 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.108.146 3306 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
其中/etc/keepalived/checkMySQL.sh需要新建,内容为:
echo "enter mysql_check.sh" >> /etc/keepalived/check_mysql.log
MYSQL=/usr/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_PORT=3306
# 日志文件
LOG_FILE=/etc/keepalived/check_mysql.log
# 检查次数
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -P${MYSQL_PORT} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
#echo `date --date=today +"%Y-%M-%d %H:%m:%S"` - [INFO] - mysql available: success[$MYSQL_OK] >> $LOG_FILE
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
echo `date --date=today +"%Y-%M-%d %H:%m:%S"` - [INFO] - mysql invaild. keepalived stop. >> $LOG_FILE
exit 1
fi
sleep 1
done
添加执行权限:
shell > chmod +x /etc/keepalived/checkMySQL.sh
手动执行下这个脚本,看看返回是否是0,如果是1,请检查mysql连不上的原因:
shell > /etc/keepalived/checkMySQL.sh
shell > echo $?
从库的keepalived.conf为:
global_defs {
notification_email{
1163804452@qq.com
}
notification_email_from waterlufei123@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KeepAlive_Mysql
}
vrrp_script vs_mysql_82 {
script "/etc/keepalived/checkMySQL.sh"
interval 60
}
vrrp_instance VI_82 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 82
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
vs_mysql_82
}
virtual_ipaddress {
192.168.108.156/24 brd 192.168.108.255 dev eth0 label eth0:1
}
}
virtual_server 192.168.108.156 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.108.145 3306 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
/etc/keepalived/checkMySQL.sh文件一样。
然后主从服务器都启动keepalived:
shell > service keepalived start
shell > chkconfig keepalived on
查看虚拟VIP
# ip addr
连接数据库IP是192.168.108.156,当从库坏了以后不应该主库,当主库坏了以后,VIP会漂移到145上,当我们继续访问192.168.108.156时,实际访问的是145上的数据库
总的来说就是keepalived可以实现生成虚拟IP的功能,但是已什么为条件生成转移VIP呢?所以这里就加了checkMySQL.sh配置文件,根据mysql的状态漂移vip,然后呢,然后这里keepalived还可以用到其他地方,例如nginx,然后弄一个checkNginx.conf之类的,实现nginx的自动切换,这里也就是 说说还没做过