docker安装mysql主从复制
一、安装
1.拉取mysql5.7镜像:docker pull mysql:5.7
2.启动
[root@kht132 kht]# docker run -p 3306:3306 --name mysql-master -v /mydata/mysql-master/log:/var/log/mysql \
> -v /mydata/mysql-master/data:/var/lib/mysql \
> -v /mydata/mysql-master/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
b9724a632dd961df79a1f1db9516bd7be92993251fbe353e00d619c2ddc0afc3
[root@kht132 kht]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9724a632dd9 mysql:5.7 "docker-entrypoint.s…" 40 seconds ago Up 39 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-master
docker run -p 3305:3306 --name mysql-master --restart=always --privileged=true \
-v /mnt/docker/mysql/log:/var/log/mysql \
-v /mnt/docker/mysql/data:/var/lib/mysql \
-v /mnt/docker/mysql/conf/my.cnf:/etc/my.cnf \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD='mysql_password' -d mysql:5.7
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
3.进入mysql-master容器实例内创建数据同步用户
mysql> CREATE USER 'slave'@'%'IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.*TO'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
4.启动从机
[root@kht132 conf]# docker run -p 3308:3306 --name mysql-slave \
> -v /mydata/mysql-slave/log:/var/log/mysql \
> -v /mydata/mysql-slave/data:/var/lib/mysql \
> -v /mydata/mysql-slave/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
0cc5ed44e22e05ef01e84b793121639b39e6c4c5127bda99134cf6569abd19cf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
## relay_log配置中继日志
relay_log=mall-mysql-relay-bin
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
## slave设置为只读(具有super权限的用户除外)
read_only=1
5.mysql主机查看
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mall-mysql-bin.000001 | 617 | | mysql | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
6.mysql从机执行
mysql> change master to master_host='192.168.2.132', master_user='slave',master_password='123456',master_port=3306,master_log_file='mall-mysql-bin.000001',master_log_pos=617,master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
master_host:主数据库的IP地址;
master_port:主数据库的运行端口;
master_user:在主数据库创建的用于同步数据的用户账号;
master_password:在主数据库创建的用于同步数据的用户密码;
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
master_connect_retry:连接失败重试的时间间隔,单位为秒。
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
二、高可用
haproxy.cfg配置文件
global
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
maxconn 4000
ulimit-n 16384
log 127.0.0.1 local0 err
stats timeout 30s
defaults
log global
mode http
option httplog
timeout connect 5000
timeout client 50000
timeout server 50000
timeout http-request 15s
timeout http-keep-alive 15s
frontend master1
mode tcp
bind *:3302
option tcplog
tcp-request inspect-delay 5s
default_backend master1
backend master1
mode tcp
option tcplog
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 900 maxqueue 256 weight 100
server master1 192.168.2.100:3305 check
server master2 192.168.2.100:3304 check
keepalived.cnf配置文件
! Configuration File for keepalived
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_dev
}
vrrp_script check_alive {
script "/etc/keepalived/check_alive.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
# weight -10 # 如果脚本运行失败,则升级权重-10
}
vrrp_script check_alive1 {
script "/etc/keepalived/check_alive1.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
# weight -10 # 如果脚本运行失败,则升级权重-10
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
mcast_src_ip 192.168.2.100
virtual_router_id 51
nopreempt
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
track_script {
check_alive
}
track_script {
check_alive1
}
}
check_alive.sh
#!/bin/bash
A=`docker ps -a|grep Exited|grep mysql-slave|wc -l`
if [ $A -eq 1 ];then
docker start mysql-slave
sleep 2
if [ `docker ps -a|grep Exited|grep mysql-slave|wc -l` -eq 1 ];then
docker start mysql-slave
fi
fi
check_alive1.sh
#!/bin/bash
A=`docker ps -a|grep Exited|grep mysql-master|wc -l`
if [ $A -eq 1 ];then
docker start mysql-master
sleep 2
if [ `docker ps -a|grep Exited|grep mysql-master|wc -l` -eq 1 ];then
docker start mysql-master
fi