mysql+keepalived双主搭建
基本情况
介绍:
搭建mysql双主架构,两个mysql节点互为主从,使用keepalived,绑定vip,应用服务器通过vip连接数据库,发生故障后vip漂移到另一节点,实现节点故障自动切换
节点列表:
ip | role |
---|---|
172.25.230.47 | master1 |
172.25.230.55 | master2 |
172.25.230.48 | vip |
mysql版本:5.7.40
映射主机端口:13010
两个mysql均为容器中部署
启动mysql
分别在两台mysql服务器上操作:
docker run -itd --name mysqltest1 -e 'MYSQL_ROOT_PASSWORD=123456' -p 13310:3306 mysql:5.7.40
创建mysqltest1容器并启动
配置mysql双主
进入mysql容器
docker exec -it mysqltest1 bash
分别修改两个mysql的配置文件:
172.25.230.47 master1
vim /etc/my.cnf
在[mysqld]下配置以下内容:
[mysqld] ......... ##开启GTID功能 gtid-mode = ON ##开启从库的写操作写入binlog日志中 log-slave-updates = 1 ##每台机器的id不能相同,一般主库为1,从库顺延,两台的配置不一样 server_id = 1 ##开启binlog日志,MySQL-bin为日志的前缀名 log-bin = MySQL-bin ##开启binlog日志索引内容文件,MySQL-bin.index为索引文件前缀名 enforce_gtid_consistency = 1 ##开启gtid安全限制,开启后,只允许执行安全语句保持gtid的一致性 log-bin-index = MySQL-bin.index ##指定自增长ID的增量为2 auto_increment_increment=2 ##指定自增长ID的起始值为1 auto_increment_offset=1 ##设置auto_increment_increment及auto_increment_offset的作用在于避免双主写入而导致自增ID冲突的情况 ##如果不用自增ID可以不设置 ## master1插入数据,自增ID从1开始,后续每个自增ID加2,即1,3,5,7
172.25.230.55 master2
vim /etc/my.cnf
在[mysqld]下配置以下内容:
[mysqld] ......... gtid-mode = ON log-slave-updates = 1 server_id = 2 log-bin = MySQL-bin enforce_gtid_consistency = 1 log-bin-index = MySQL-bin.index auto_increment_increment = 2 auto_increment_offset = 2
重启mysql
docker restart mysqltest1
进入mysql容器,执行:
mysql -uroot -p
mysql> grant replication slave on *.* to rep@"172.25.230.%" identified by '123Abc..';
分别在两台主机上建立同步用户rep
master1
在master1的mysql命令行中配置master2信息:
mysql> reset slave;
mysql> change master to
-> master_host='172.25.230.55', master2 ip
-> master_user='rep', 同步账户rep
-> master_password='123Abc..', 同步账户rep密码
-> master_port=13310, master2的mysql端口
-> master_auto_position=1;
mysql> start slave;
master2
在master2的mysql命令行中配置master1信息:
mysql> reset slave;
mysql> change master to
-> master_host='172.25.230.47', master1 ip
-> master_user='rep',
-> master_password='123Abc..',
-> master_port=13310, master1的mysql端口
-> master_auto_position=1;
mysql> start slave;
查看同步状态
master1
mysql> show slave status\G;
master2
mysql> show slave status\G;
双主同步测试
在master1上建立库表并插入数据:
mysql> create database testbase character set utf8 collate utf8_bin;
mysql> create table t1(id int,name varchar(20));
mysql> insert into t1 (id,name) values (1,'user1');
mysql> insert into t1 (id,name) values (2,'user2');
分别在master1和master2上查看
在master2上插入数据:
mysql> insert into t1 (id,name) values (3,'user3');
mysql> insert into t1 (id,name) values (4,'user4');
在master2和master1上查看
双主复制成功
配置keepalived
keepalived配置文件
master1
vim /etc/keepalived/keepalived.conf
global_defs { router_id 1 ##服务器标识号,主备不一致即可 } ##健康检查脚本 vrrp_script chk_mysql { script "/etc/keepalived/script/mysql_status.sh" ##状态检查脚本 interval 2 weight -5 fall 3 } vrrp_instance VI_1 { state MASTER ##指定主备节点,MASTER为主节点,BACKUP为备节点 interface eth0 ##网卡名称 virtual_router_id 51 ##虚拟路由编号,主备一致 priority 100 ##优先级,主节点高于备节点 unicast_src_ip 172.25.230.52 ##心跳源ip,设置单播模式,有些服务器或者网络不支持组播模式 unicast_peer { 172.25.230.55 ##单播的目标地址,即BACKUP节点地址,BACKUP有多台就配置多个地址 } authentication { ##密钥认证,指定 auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.25.230.48/20 ##vip } track_script { chk_mysql ##引用脚本,名称要与vrrp_script中设定的名称对上 } }
master2
vim /etc/keepalived/keepalived.conf
global_defs { router_id 2 } ##健康检查脚本 vrrp_script chk_mysql { script "/etc/keepalived/script/mysql_status.sh" interval 2 weight -5 fall 3 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 mcast_src_ip 172.25.230.55 unicast_src_ip 172.25.230.55 unicast_peer { 172.25.230.55 } authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.25.230.48/20 } track_script { chk_mysql } }
配置mysql状态检查脚本
将mysql脚本上传到目录中:/etc/keepalived/script/,并授予执行权限
chmod +x /etc/keepalived/script/mysql_status.sh
mysql检查脚本:mysql_status.sh
#!/bin/bash mysqlname="mysqltest1" docker ps | awk 'NR>2{print line}{line=$0} END{print line}' | awk '{print $NF}' | grep -w "$mysqlname" if [ $? -ne 0 ];then systemctl stop keepalived fi
启动keepalived
systemctl start keepalived
查看vip情况
master1
master2
切换测试
通过远程的方式连接vip,登录数据库
mysql -h 172.25.230.48 -uroot -P13310 -p
查看主从复制状态:
显示的是master2的相关信息
停止master1的mysql容器
docker stop mysqltest1
vip已切换到master2
再次通过vip登录mysql,查看mysql主从复制状态:
显示的是master2的相关信息
启动master1的mysql
docker start mysqltest1
systemctl start keepalived