192.168.209.128 slave
在128这台slave执行以下语句:
change master to master_host='192.168.209.132',master_port=33061,master_user='rep',master_password='123456',master_log_file='bin-log-mysqld5.000002',master_log_pos=1030
start slave
show slave status\G
主从同步开始后,在128,把mysql shutdown掉了
然后再在从启动Mysql,start slave
这时候,同步会继续,不会出错。
使用keepalived做mysql主从切换的高可用
keepalived切换的优缺点
1.可以切换虚拟IP
2.可能发生裂脑,就是主从服务器都同时出现一样的VIP,导致写入数据的时候,往主从都写入了数据
3.可能导致主从mysql数据不一致。主在down机的时候,有部分数据还没同步到从mysql
此实验在mysql使用gtid同步实现的前提下的
192.168.209.132 master
192.168.209.131 slave
1.安装keepalived
直接yum安装或者编译安装都可以,生产环境也是ok的
2.配置keepalived的配置文件
keepalived配置文件默认放在/etc/keepalived/文件夹下
如果不把配置文件放这里,那么启动keepalived的时候,需要用参数指定配置文件的位置
这里我用默认安装和默认配置文件位置
192.168.209.132:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id SLAVE
}
vrrp_script chk_mysql {
script "/data/script/mysql_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 131
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.209.16
}
track_script {
chk_mysql
}
}
监控脚本:
vim /data/script/mysql_check.sh
#!/bin/sh
mysqlstr=/usr/local/mysql/bin/mysql
host=localhost
user=root
password=123456
port=33061
mysql_status=1
$mysqlstr -h $host -u $user -p$password -P $port -e "show status" >/dev/null 2>&1
if [ $? = 0 ] then
echo "mysql_status=1"
exit 0
else
pkill keepalived
fi
192.168.209.131:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id SLAVE
}
vrrp_script chk_mysql {
script "/data/script/mysql_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 131
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.209.16
}
track_script {
chk_mysql
}
}
配置文件:
vim /data/script/mysql_check.sh
#!/bin/sh
mysqlstr=/usr/local/mysql/bin/mysql
host=localhost
user=root
password=123456
port=33061
mysql_status=1
$mysqlstr -h $host -u $user -p$password -P $port -e "show status" >/dev/null 2>&1
if [ $? = 0 ] then
echo "mysql_status=1"
exit 0
else
pkill keepalived
fi
对实验结果开始进行验证
192.168.209.132上获取到vip
把192.168.209.132上的mysqld给干掉
查看192.168.209.132上的mysqld和keepalived进程是否都被干掉了;虚拟IP是否切换到192.168.209.131上了
查看192.168.209.131上是否有VIP
把192.168.209.132上的keepalived和mysqld都启动起来。先启mysqld再起keepalived
此时keepalived启动起来了,虽然权重比192.168.209.131的高,但是设置了不抢夺,所以192.168.209.132上的keepalived不会切换vip过来
此时,把192.168.209.131上的mysql停掉它
查看131上的mysql和keepalived是否已经都停止了
查看192.168.209.132上是否有VIP了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)