使用Keepalived实现MySQL双主高可用
MySQL双主配置
环境准备:
OS: CentOS7
master:192.168.1.10
backup:192.168.1.20
VIP:192.168.1.30
一、安装MySQL数据库.
在master 和 backup 上安装mysql,安装完后自动启动,mysql root密码为123456
二、修改MySQL配置文件:
1.master端配置文件如下:
.master端配置文件如下: # vim /etc/my.cnf #添加
server_id = #backup上设置为2
log-bin = /data/mysql/mysql-bin
log-bin-index=/data/mysql/my-bin.index
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
auto-increment-increment = #字段变化增量值
auto-increment-offset = #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误 # systemctl restart mysqld
2. backup端配置文件如下:
master端和backup端配置只有server_id不一样,别的都一致.
三、创建数据同步用户并查看log bin日志和pos位置:
1.> master上创建 mysql 同步账号并查看log bin日志和pos位置:
# mysql -uroot -p123456 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%' IDENTIFIED BY 'repl'; mysql> flush privileges; mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin. | | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
master配置如下:
# mysql -uroot -p123456 mysql> change master to
-> master_host='192.168.1.20', #这里填backup的IP
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=; mysql> start slave;
2.> backup上创建mysql同步账号配置如下:
# mysql -uroot -p123456 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%' IDENTIFIED BY 'repl'; mysql> flush privileges; mysql> change master to
-> master_host='192.168.1.10', #这里填master的IP
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=; mysql> start slave;
---------------------
分别查看同步状态:
master查看:
mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.20
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: test2-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
---------------------
backup查看:
mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.10
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: test3-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
---------------------
Slave_IO和Slave_SQL是YES说明主主同步成功。
四、MySQL主主同步测试
master上插入数据测试:
mysql> create database testdb; mysql> use testdb; mysql> create table user (number INT(),name VARCHAR()); mysql> insert into user values(,'testid'); mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
---------------------
backup上查看:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+ mysql> use testdb; mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+ mysql> select number,name from user;
+--------+------+
| number | name |
+--------+------+
| | testid |
+--------+------+
---------------------
可以看到已经成功同步过去,同样在backup插入到user表数据,一样同步过去,双主配置没有问题。
五、配置keepalived实现双机热备
1.master安装keepalived并配置:
# yum install -y keepalived # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
admin@test.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id MYSQL_HA
} vrrp_instance VI_1 {
state BACKUP
interface eth0 #根据实际网络接口进行更改
virtual_router_id
priority #优先级,master设置为100
advert_int
nopreempt #不主动抢占资源,只在master上设置
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.30
}
} virtual_server 192.168.1.30 {
delay_loop
#lb_algo rr
#lb_kind NAT
persistence_timeout
protocol TCP real_server 192.168.1.10 { #检测本地mysql
weight
notify_down /tmp/mysql.sh #当mysql服务down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout
nb_get_retry
delay_before_retry
}
}
}
backup安装keepalived并配置:
# yum install -y keepalived # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
admin@test.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id MYSQL_HA
} vrrp_instance VI_1 {
state BACKUP
interface eth0 #根据实际网络接口进行更改
virtual_router_id
priority #优先级,backup设置为90
advert_int
#nopreempt #主动抢占资源
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.30
}
} virtual_server 192.168.1.30 {
delay_loop
#lb_algo rr
#lb_kind NAT
persistence_timeout
protocol TCP real_server 192.168.1.20 { #检测本地mysql
weight
notify_down /tmp/mysql.sh #当mysql服务down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout
nb_get_retry
delay_before_retry
}
}
}
master 和 backup上编辑mysql.sh
# vim /tmp/mysql.sh #!/bin/bash
pkill keepalived # chmod +x !$
# systemctl start keepalived
两台mysql服务器授权允许root远程登录:
# mysql -uroot -p123456789 mysql> grant all on *.* to 'root'@'192.168.1.%' identified by ''; mysql> flush privileges;
测试高可用
通过mysql客户端通过VIP连接,看是否连接成功。
这里我用同网段的另一台机器,连接测试:
# mysql -h192.168.1. -uroot -p123456 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select * from test.user;
+--------+------+
| number | name |
+--------+------+
| | testid |
+--------+------+
row in set (0.01 sec)
---------------------
可以看到,连接成功,且查询数据没有问题,停止master上mysql服务,是否能正常切换到backup上,可以使用 ip addr命令来查看VIP在哪台服务器上。
master上查看是否有VIP,可以看到VIP在master上
# ip addr
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP group default qlen
link/ether :0c::cf:ab:c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/ brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.1.30/ scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::fe8e:3c2f:4d32:e9fd/ scope link noprefixroute
valid_lft forever preferred_lft forever
---------------------
停掉master上mysql服务:
# systemctl stop mysqld # ps axu |grep keepalived
root 0.0 0.0 pts/ S+ : : grep --color=autokeepalived # ip addr
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP group default qlen
link/ether :0c::cf:ab:c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/ brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::fe8e:3c2f:4d32:e9fd/ scope link noprefixroute
valid_lft forever preferred_lft forever
---------------------
可以看到,keepalived在mysql服务停掉之后也被停掉,VIP不在master上。
backup上查看是否有VIP,可以看到VIP在backup上。
# ip addr
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP group default qlen
link/ether :0c::::d5 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/ brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.1.30/ scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::4b20:2e16:a957:f9a1/ scope link noprefixroute
valid_lft forever preferred_lft forever
---------------------
查看/var/log/messages日志,可以看到主备切换过程:
Apr :: hosts systemd: Stopping MySQL Server...
Apr :: hosts Keepalived_healthcheckers[]: TCP connection to [192.168.1.10]: failed.
Apr :: hosts Keepalived_healthcheckers[]: TCP connection to [192.168.1.10]: failed.
Apr :: hosts Keepalived_healthcheckers[]: Check on service [192.168.1.10]: failed after retry.
Apr :: hosts Keepalived_healthcheckers[]: Removing service [192.168.1.10]: from VS [192.168.1.30]:
Apr :: hosts Keepalived_healthcheckers[]: IPVS (cmd , errno ): No such destination
Apr :: hosts Keepalived_healthcheckers[]: Executing [/tmp/mysql.sh] for service [192.168.1.10]: in VS [192.168.1.30]:
Apr :: hosts Keepalived_healthcheckers[]: Lost quorum -= > for VS [192.168.1.30]:
Apr :: hosts Keepalived_healthcheckers[]: Remote SMTP server [127.0.0.1]: connected.
Apr :: hosts Keepalived_vrrp[]: VRRP_Instance(VI_1) sent priority
Apr :: hosts Keepalived_vrrp[]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr :: hosts Keepalived[]: Stopping
Apr :: hosts Keepalived_healthcheckers[]: IPVS (cmd , errno ): No such file or directory
Apr :: hosts Keepalived_healthcheckers[]: Stopped
Apr :: hosts Keepalived_vrrp[]: Stopped
Apr :: hosts Keepalived[]: Stopped Keepalived v1.3.5 (/,), git commit v1.3.5--g6fa32f2
Apr :: hosts systemd: Stopped MySQL Server.
---------------------
恢复master服务器故障,看是否主动抢占资源,成为活动服务器。
master上启动mysql服务和keepalived服务:
# systemctl start mysqld # systemctl start keepalived # ip addr
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP group default qlen
link/ether :0c::cf:ab:c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/ brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::fe8e:3c2f:4d32:e9fd/ scope link noprefixroute
valid_lft forever preferred_lft forever
---------------------
可以看到,即使master故障恢复,也没有抢占资源,VIP仍然在backup上,这是因为之前已经配置了master为非抢占模式(nopreempt)。
不过需要注意的是:
nopreempt这个参数只能用于state为BACKUP的情况,所以在配置的时候要把master和backup的state都设置成BACKUP,这样才会实现keepalived的非抢占模式!
也就是说:
* 当state状态一个为MASTER,一个为BACKUP的时候,加不加nopreempt这个参数都是一样的效果。即都是根据priority优先级来决定谁抢占vip资源的,是抢占模式!
* 当state状态都设置成BACKUP,如果不配置nopreempt参数,那么也是看priority优先级决定谁抢占vip资源,即也是抢占模式。
* 当state状态都设置成BACKUP,如果配置nopreempt参数,那么就不会去考虑priority优先级了,是非抢占模式!即只有vip当前所在机器发生故障,另一台机器才能接管vip。
即使优先级高的那一台机器恢复正常后也不会主动抢回vip,只能等到对方发生故障,才会将vip切回来。
关闭主从复制:
登录到从库服务器进行配置
关闭复制
mysql> STOP SLAVE;
重置,清除复制信息,这样再启动时就不会进行复制了。
mysql> RESET SLAVE ALL;
参考文档:https://blog.csdn.net/miss1181248983/article/details/89139951
使用Keepalived实现MySQL双主高可用的更多相关文章
- MySQL集群(四)之keepalived实现mysql双主高可用
前面大家介绍了主从.主主复制以及他们的中间件mysql-proxy的使用,这一篇给大家介绍的是keepalived的搭建与使用! 一.keepalived简介 1.1.keepalived介绍 Kee ...
- 基于keepalived搭建mysql双主高可用
目录 概述 环境准备 keepalived搭建 mysql搭建 mysql双主搭建 mysql双主高可用搭建 概述 传统(不借助中间件)的数据库主从搭建,如果主节点挂掉了,从节点只能读取无法写入,只能 ...
- 基于Keepalived实现LVS双主高可用集群
Reference: https://mp.weixin.qq.com/s?src=3×tamp=1512896424&ver=1&signature=L1C7us ...
- nginx+keepalived实现nginx双主高可用的负载均衡
http://kling.blog.51cto.com/3320545/1253474 一.前言: 在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作.提供稳定可靠的服务,可以给用户带 ...
- MySQL5.7 利用keepalived来实现mysql双主高可用方案的详细过程
Reference: http://blog.csdn.net/mchdba/article/details/51377989 服务器准备 Keepalived:192.168.13.15 Keep ...
- keepalived+mysql实现双主高可用
环境: DB1:centos6.8.mysql5.5.192.168.2.204 hostname:bogon DB2:centos6.8.mysql5.5.192.168.2.205 hostn ...
- 通过keepalived搭建MySQL双主模式的高可用集群系统
1. 配置MySQL双主模式 1.修改my.cnf配置文件 默认情况下,MySQL的配置文件是/etc/my.cnf,在配置文件的[mysqld]段添加如下内容: server-id=1 log-bi ...
- MariaDB+Keepalived双主高可用配置MySQL-HA
利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟VIP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. ...
- Nginx(haproxy)+keepalived+Tomcat双主高可用负载均衡
周末的时候一个正在学Linux的朋友问我,高可用怎么玩?我和他微信了将近三个小时,把Nginx和haproxy双主高可用教给他了,今天突然想把这个给写进博客里,供给那些正在学习Linux系统的朋友们, ...
随机推荐
- 通过SimpleHTTPServer实现树莓派与主机传输文件
默认情况我们的Raspberry Pi已经安装了python 2.7版本.python2 中默认包含了SimpleHTTPServer这个库.因此我们可以直接拿来使用. 从名字中我们就可以看出来,他是 ...
- idea中使用python环境
idea中使用python环境,需要下载相关的python sdk 可以添加选择自己的python环境
- 【day05】css
一.盒模型(BoxModel) 1.width 宽度 2.height 高度 说明: 块元素和有宽高属性的标记(img,input) 能设置宽度和高度,而行元素不能设置宽高 3 ...
- 测开面试 | Python常问算法
1.排序 从小到大排序:sorted(list) 从大到小排序:sorted(list, reverse=True) sort() 方法,改变原有数组的顺序 sort(reverse=True) #! ...
- [LeetCode] 834. Sum of Distances in Tree 树中距离之和
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge co ...
- Manjaro Linux安装后第一件事
环境:Manjaro KDE 使用中科大源 USTC Mirror 当然也可以勾选所有中国镜像源 谷歌加上云pinyin拼音输入法,不亚于搜狗 fcitx-qt5 kcm-fcitx (confi ...
- QLayout及其子类 清除添加的widget
起初,我的思路是,先取得Layout的items数量, 然后通过索引来移除每一个items,代码如下: QHBoxLayout * hly = new QHBoxLayout; ; i < ; ...
- JSP还有必要学吗?这篇文章告诉你
阅读本文大概需要 12.4 分钟. 来源:http://suo.im/4wqRi7 作者:杨明翰 前戏 前后端分离已成为互联网项目开发的业界标准使用方式,通过nginx+tomcat的方式(也可以 ...
- DingTalk钉钉消息推送(.net core 3 WebApi尝鲜记)
我发了个朋友圈,Swagger真他妈的牛B,解放了开发API的码农,麻麻再也不用担心我们写API文档耽误回家吃饭了. /// <summary> /// 发送钉钉消息 /// </s ...
- LeetCode 220. Contains Duplicate III (分桶法)
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...