1.环境准备:
系统版本 Centos7
IP:192.168.130.5 master
IP:192.168.130.6 backup
虚拟ip:10.10.11.82
Docker环境:
centos7离线安装docker,docker-compose
keepalived安装包:
2.配置两台 MySQL 主主同步
制作Mysql容器
使用dokcer快速启动mysql容器,注意端口号有没有被占用,两台服务器都要启动,两台服务器端口号要一致,防火墙也看一下,对外能不能访问端口。
sudo docker run -p 3370:3306 --name mysql-db \
-v /data/mysql/log:/var/log/mysql \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/conf:/etc/mysql/conf.d \
--restart=always \
-e MYSQL_ROOT_PASSWORD=123456 \
-d 5107333e08a8
配置MySQL
MASTER:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names = 1
log-bin=/var/lib/mysql/mysql-bin
server-id=1 #backup这台设置2
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
BACKUP:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names = 1
log-bin=/var/lib/mysql/mysql-bin
server-id=2 #backup这台设置2
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 2 #初始字段ID为2
slave-skip-errors = all #忽略所有复制产生的错误
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
注:master和backup只有server-id不同和 auto-increment-offset不同。
修改好以后,重启mysql容器
docker restart mysql-db
查看下log bin日志和pos值位置
进入mysql docker容器内部,登录mysql环境:
mysql> show master status;
配置主从
主主模式就是配置两个主从,先配置192.168.130.5(主1)->192.168.130.6(主2)的主从,然后再反过来配置192.168.130.6(主2)->192.168.130.5(主1)的主从,这样主主的模式就配置好了。
配置 master:
CREATE USER 'replication'@'192.168.130.%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.130.%';
FLUSH PRIVILEGES;
CHANGE MASTER TO
MASTER_PORT = 3370,
MASTER_HOST='192.168.130.6',
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=154;
START SLAVE;
配置 backup:
CREATE USER 'replication'@'192.168.130.%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.130.%';
FLUSH PRIVILEGES;
CHANGE MASTER TO
MASTER_PORT = 3370,
MASTER_HOST='192.168.130.5',
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=154;
START SLAVE;
这两个参数根据自己的服务器填对应的值,不要填错了。
3.测试主主同步
主主同步配置完毕,查看同步状态mysql> show slave status\G
Slave_IO和Slave_SQL是YES说明主主同步成功。可以使用工具链接一下试试,这一步略过。
建一张表,两个数据库里面分别插入数据,看看是否同步。
4.安装 keepalived
yum install -y keepalived
安装之后,在 /etc/keepalived目录下有个 keepalived.conf 配置文件:
编辑master服务器上的keepalived.conf:
! Configuration File forkeepalived
global_defs {
notification_email {
test@sina.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #标识,双主相同
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
virtual_router_id 51 #主备相同
priority 100 #优先级,backup设置90
advert_int 1
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 指定VIP地址
10.10.11.82
}
}
virtual_server 10.10.11.82 3308 { #配置virtual_server ip为上面配置的虚拟vip地址 端口为mysql的端口
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50 #同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.10.11.79 3308 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /etc/keepalived/chk_mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
}
编辑backup服务器上的keepalived.conf:
! Configuration File forkeepalived
global_defs {
notification_email {
test@sina.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #标识,双主相同
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
virtual_router_id 51 #主备相同
priority 90 #优先级,backup设置90
advert_int 1
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 指定VIP地址
10.10.11.82
}
}
virtual_server 10.10.11.82 3308 { #配置virtual_server ip为上面配置的虚拟vip地址 端口为mysql的端口
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50 #同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.10.11.80 3308 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /etc/keepalived/chk_mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
}
backup服务器只修改priority为90、nopreempt不设置、real_server设置本地IP。
5.编写检测服务down后所要执行的脚本
vim /etc/keepalived/chk_mysql.sh
跟keepalived.conf配置文件放同一个目录下面
内容如下:
#!/bin/bash
#version 0.0.1
#当mysql进程不存在时,会自动重启mysql容器;
A=`ps -ef|grep mysqld|grep -v grep|grep -v bash|wc -l`
if [ $A -eq 0 ];then
docker restart keepalivedMysql #重启mysql容器
sleep 2
if [ `ps -ef|grep mysqld|grep -v grep|grep -v bash|wc -l` -eq 0 ];then
systemctl stop keepalived #停掉keepalived服务
fi
fi
chk_mysql.sh脚本:目的是检测mysqld进程是否存活,如果mysql进程挂掉了,就杀掉keepalived服务,从而实现了MySQL故障自动转移。
两台服务器这一步授权一定要执行不然脚本会失效:
chmod +x chk_mysql.sh
启动keeplived
systemctl enable keeplived –now
7.验证
1:如果验证主从是否可以自动切换实现热备效果可以把其中一台机器的keepalived服务stop掉,然后观察虚拟ip看是否实现了故障转移。
2:如果验证健康脚本是否生效,可以把mysql容器stop掉,过两秒再次查看一下mysql容器是否会自动启动。
来源参考:Docker搭建MySQL主主模式+Keepalived实现高可用集群_51CTO博客_docker-compose mysql集群
No Comments