0120Keeplived实现自动切换Mysql服务
转自http://biancheng.dnbcw.info/mysql/381020.html
Keepalived+mysql 自动切换
网络结构:
VIP 192.168.88.200
mysq11 192.168.88.130 主
mysql2 192.168.88.131 备
一 mysql 主主同步
二 安装keepalived
三 keepalived 主备配置文件
四 mysql状态检测脚本/root/keepalived_check_mysql.sh
五 防火墙设置
六 启动keepalived
七 查看vrrp通讯记录
八 查看虚拟IP
九 测试
一 mysql 主主同步(略)
说明:数据库testA和testB分别在mysql1和mysql2上,testDB是通过主主复制获得,便于后面的测试
二 安装keepalived
1.软件包下载地址http://www.keepalived.org
安装keepalived,从官方网站下载keepalived-1.2.2.tar.gz后
# tar xvf keepalived-1.2.2.tar.gz
# cd keepalived-1.2.2
# ./configure
# make && make install
2.查看keepalived位置
[root@lvs-dr1 keepalived-1.2.2]# find / -name "keepalived"
/root/keepalived-1.2.2/keepalived
/root/keepalived-1.2.2/keepalived/etc/keepalived
/root/keepalived-1.2.2/bin/keepalived
/usr/local/sbin/keepalived
/usr/local/etc/rc.d/init.d/keepalived
/usr/local/etc/keepalived
/usr/local/etc/sysconfig/keepalived
3.复制文件
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/sbin/keepalived /usr/sbin/
三 keepalived 主备配置文件
主备置文件不同处有 state nopreempt priority 参考资料里有详细说明作用
192.168.88.130 主配置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
15301727316@189.cn
}
notification_email_from zalifei@126.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id MySQL-ha
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 88
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass skyai1.cublog.cn
}
track_script {
check_run
}
virtual_ipaddress {
192.168.88.200
}
}
复制代码192.168.88.131 备用配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
15301727316@189.cn
}
notification_email_from zalifei@126.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id MySQL-ha
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass skyai1.cublog.cn
}
track_script {
check_run
}
virtual_ipaddress {
192.168.88.200
}
}
四 mysql状态检测脚本/root/keepalived_check_mysql.sh
# vim /root/keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/local/webserver/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=mysql
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} -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
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done
赋予执行权限
# chmod +x /root/keepalived_check_mysql.sh
五 防火墙设置
vrrp协议 使用224.0.0.18地址组播
iptables -I RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT
六 启动keepalived
# service keepalived start
七 查看vrrp通讯记录
# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
11:51:45.632044 IP 192.168.88.130 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 88, prio 100, authtype simple, intvl 1s, length 20
八 查看虚拟IP
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:bf:c5:77 brd ff:ff:ff:ff:ff:ff
inet 192.168.88.130/24 brd 192.168.88.255 scope global eth0
inet 192.168.88.200/32 scope global eth0
inet6 fe80::20c:29ff:febf:c577/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
九 测试
# mysql -uroot -h192.168.88.200 -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| testA |
| testDB |
+--------------------+
5 rows in set (0.00 sec)
停掉主数据库服务,测试
# service mysqld stop
# mysql -uuser -h "192.168.88.200" -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| testB |
| testDB |
+--------------------+
5 rows in set (0.02 sec)
到这里,测试完成
0120Keeplived实现自动切换Mysql服务的更多相关文章
- 启动和启动和停止MySQL服务停止MySQL服务
1. 启动MySQL服务 启动MySQL服务的命令为: /etc/init.d/mysqld start 命令执行后如图7-5所示,表示启动MySQL服务成功. (点击查看大图)图7-5 启动 ...
- 启动和停止MySQL服务
1. 启动MySQL服务 启动MySQL服务的命令为: /etc/init.d/mysqld start 命令执行后如图7-5所示,表示启动MySQL服务成功. (点击查看大图)图7-5 启动 ...
- 使用keepalived实现mysql主从复制的自动切换
最近测试了一下mysql+keepalived实现主从自动切换,主从都需要安装keepalived,使用vip漂移实现主从自动切换,这里主要记录的是keepalived的文件配置. 这里mysql搭建 ...
- 设置MySQL服务自动运行
一般情况下,MySQL安装以后是自动运行的,不知道我这台机器是什么原因,MySQL不能自动运行,每次开机后都要手动运行mysqld.exe,比较麻烦,于是用以下方法将MySQL自动启动: 1. 运行c ...
- 【转】双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计
架构简介 前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构.此架构主要是由kee ...
- 双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计
前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构. 架构简介 此架构主要是由ke ...
- Mycat在MySQL主从模式(1主1从)下读写分离和及自动切换模式的验证
实验环境 两台Centos7 MySQL5.7.12 IP地址为:192.168.10.36 192.168.10.37 一台Centos7 Mycat IP地址为:192.168.10.31 一 ...
- keepalived 结合mysql 自动切换
启动keepalived:/usr/local/sbin/keepalived -D -d -S 0 master ip:192.168.32.6 master:/root/sbin# cat /et ...
- mysql主从备份+keepalived自动切换
数据库这一层需要做到避免单点故障可以是主从备份和主主备份,主主备份可能有性能损耗和数据同步的问题.这里记录下主从备份, mysql进行备份之前确保mysql的版本是一样的,我这里用的都是mysql5. ...
随机推荐
- 讲一讲WiFi快连、SmartConfig、SmartConnect
最近要给公司同事们培训WiFi快连技术,整理了相关资料,也分享在博客这,献给有缘人. 前言 现在的智能硬件产品,以WiFi品类居多,这些WiFi硬件没有人机交互界面,但设备要上网肯定要配置SSID等相 ...
- rpmbuild
rpm2cpio xxx.rpm | cpio -div
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- golang——(strings包)常用字符串操作函数
(1)func HasPrefix(s, prefix string) bool 判断字符串s是否有前缀字符串prefix: (2)func HasSuffix(s, suffix string) b ...
- [JZOJ3809]设备塔
其实我并没有JZOJ的号...但既然dalao说了是JZOJ上的题,那就是了吧...... 为了封印辉之环,古代塞姆利亚大陆的人民在异空间中建造了一座设备塔. 简单的说,这座设备塔是一个漂浮在异空间中 ...
- 【题解】【CodeForces712C】Memory and De-Evolution
[题目描述] 给定一个边长为xx的正三角形,现在每秒钟你可以改变其中一条边的长度(修改量整数),在改变过程中,每秒钟都需要保证改变后的三角形是合法的,且变成均为正整数. 现在需要最终把三角形改变成边长 ...
- BZOJ 4057 状压DP
思路: 状压一下 就完了... f[i]表示选了的集合为i 转移的时候判一判就好了.. //By SiriusRen #include <cstdio> #include <cstr ...
- 高通处理器手机 解锁Bootloader 教程
目前很多手机都需要解锁Bootloader之后才能进行刷机操作 本篇教程教你如何傻瓜式解锁Bootloader 首先需要在设置-关于手机 找到版本号(个别手机可能是内核版本号,甚至其他) 然后 快 ...
- 关于Core里的 StartUp里的方法的理解。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; ...
- dubbo之静态服务
有时候希望人工管理服务提供者的上线和下线,此时需将注册中心标识为非动态管理模式 <dubbo:registry address="10.20.141.150:9090" dy ...