实验环境:
DB1:172.16.1.100
DB2:172.16.1.101
VRRIP:172.16.1.99
步骤:
yum -y install mysql
1。修改DB1的mysql配置文件
1 2 3 4 5 | |
2. 在DB1创建复制用户并授权
1 | |
3.在DB2的mysql库将DB1设为自己的主服务器
1 2 3 4 5 6 | |
4.然后运行
1 | |
5.查询DB2运行状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
6.然后再DB1和DB2配置上面的配置
7.在DB1和DB2上下载keepalived
yum install keepalived
8.配置keepalived的配置文件
[root@db1 ~]# vim /etc/keepalived/keepalived.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
8./usr/local/keepalived/mysql.sh
#!/bin/bash
#function:监视mysql主从服务器状态
#written by:alec
#
user=mysql
host=172.16.1.150
password=123
#
check_mysql() {
IO=`/usr/local/mysql/bin/mysql -u$user -p$password -h$host -e 'show slave status\G'|grep IO_Running|tr -s ' '|awk '{print $2}'|grep Yes|wc -l`
SQL=`/usr/local/mysql/bin/mysql -u$user -p$password -h$host -e 'show slave status\G'|grep SQL_Running|tr -s ' '|sed -n '1p'|awk '{print $2}'|grep Yes|wc -l`
if [ "$IO" -eq 1 ]&&[ "$SQL" -eq 1 ]
then
service keepalived status|grep pid
if [ $? -eq 0 ]
then
echo "服务已经启动"
else
service keepalived start
if [ $? -eq 0 ]
then
echo "服务启动成功"
else
echo "服务启动失败"
fi
fi
else
service keepalived status|grep pid
if [ $? -eq 0 ]
then
service keepalived stop
if [ $? -eq 0 ]
then
echo "服务关闭成功"
else
echo "服务关闭失败"
fi
else
echo "服务已经关闭"
fi
fi
}
main(){
check_mysql
}
main;
systemctl start keepalived
------------------------------------
错误一:database doesn't exist' on query. Default database
解决:
1.stop slave;
2.set global sql_slave_skip_counter=1;
3.start slave;