mysql最简单的高可用
2台pc机
mysql主主复制实现数据的同步
keepalive实现双机热备,保证服务的正常运行
1、环境
Master1 10.0.0.201
Master2 10.0.0.202
2、Master1操作授权
mysql>grant replication slave on *.* to'admin'@'10.0.0.202' identified by '123456';///授权
mysql>show master status\G;
3、Master2操作授权
mysql->grant replication slave on *.* to'admin'@'10.0.0.201' identified by '123456';///授权
mysql->show master status\G;
4、主1-201上操作
change master to
master_host='10.0.0.202',
master_user='admin',
master_password='123456',
master_log_file='mysql-bin.000006',
master_log_pos=-242;
mysql->start slave;
mysql->show slave status\G;
5、主2-202上操作
change master to
master_host='10.0.0.201',
master_user='admin',
master_password='123456',
master_log_file='mysql-bin.000002',
master_log_pos=242;
mysql->start slave;
mysql->show slave status\G;
6、主201 和202同时安装keepalived (也可以用yum安装,我实验用的yum)
# tar -xvf keepalived-1.1.20.tar.gz
# cd keepalived-1.1.20
#./configure --prefix=/usr/local/keepalived
# make && make install
# cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/sbin/keepalived/usr/sbin/
7.keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from Alexandre.Cassen@firewall.loc
}
vrrp_instance VI_1 {
state BACKUP ///2个都写为BACKUP不然noprempt没用,主的起了会抢占为主
interface eth0 ///vip对外接口位置
virtual_router_id 51 ///标识号,主主要统一
priority 100 ///优先级,高的在初始时为主
advert_int 2
nopreempt ///不抢占模式,在优先级高的上设置
authentication{ ///认证
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { ///虚拟ip
192.168.1.137/24 dev eth0
}
}
开始我想在keepalive配置文件里写脚本监控mysqld服务,然后一直有问题,我就自己在外面写脚本检测了,脚本如下:
#!/bin/bash
#Totle:check mysql
#Description:check mysql status
#Author:chenmin
#date:2015-09-22
check_mysql_health(){
mysql -h***** -u***** -p**** -e "showslave status\G" >/dev/null 2>&1 ///按mysql授权写
}///检测mysql的可用性
check_keepalived_health() { ///检测keepalived服务是否开启
health=`/etc/init.d/keepalived status|grep-c pid` ///0时为没开启,1为正常
[ $health = 0]&&/etc/init.d/keepalived start ///如果没开启时就开启keepalived
}
main() {
check_mysql_health&&check_keepalived_health ///保证mysql可用时keepalived一定可用
check_mysql_health||check_mysql_health||check_mysql_health|| /etc/init.d/keepalived stop
} ///若3次mysql可用检测都失败则关闭keepalived服务,让backup占用vip
while true ///死循环,2秒执行一次检测
do
main
sleep 2
done
[root@localhost mysqlcheck]# nohup bash/etc/keepalived/mysqlcheck/check_mysql.sh &
///放后台执行检测
可能还有很多不足,长连接会断开,vip起来时间可能得2秒多等,所以我准备下面用mysql-mmm来实现主主互切。
本文出自 “创新分享驰骋里外” 博客,请务必保留此出处http://10554846.blog.51cto.com/10544846/1697185