docker部署mysql 5.7主从集群
·
1、拉取镜像
注:国内下载需要配置镜像加速器
cat > /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": [ "https://docker.m.daocloud.io" ]
}
EOF
systemctl daemon-reload
systemctl restart docker
docker pull mysql:5.7
2、准备my.cnf配置文件和docker compose配置文件
注:默认的my.cnf配置文件可以从先启动Mysql容器服务,然后从容器里拷贝出来。
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
docker cp mysql:/etc/my.cnf .
# 创建配置目录
mkdir -p /opt/mysql/{master,slave}
# 拷贝配置文件
\cp -f my.cnf /opt/mysql/master/
\cp -f my.cnf /opt/mysql/slave/
# 修改配置文件
vim /opt/mysql/master/my.cnf
[mysqld]
server-id = 1 # 唯一ID,必须与从库不同
log-bin = mysql-bin # 开启并设置二进制日志文件名
binlog_format = ROW # 推荐使用ROW格式
expire_logs_days = 7 # 日志保留天数,避免磁盘占满
vim /opt/mysql/slave/my.cnf
server-id = 2 # 唯一ID,必须与从库不同
log-bin = mysql-bin # 开启并设置二进制日志文件名
binlog_format = ROW # 推荐使用ROW格式
expire_logs_days = 7 # 日志保留天数,避免磁盘占满
# 准备docker compose配置文件
```yaml
services:
mysql-master:
container_name: mysql-master
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- "/var/lib/mysql"
- "/opt/mysql/master/my.cnf:/etc/my.cnf"
ports:
- "3306:3306"
mysql-slave:
container_name: mysql-slave
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- "/var/lib/mysql"
- "/opt/mysql/slave/my.cnf:/etc/my.cnf"
ports:
- "3307:3306"
3、启动mysql服务
[root@develop ~]# docker compose -f mysql-5.7.yml up -d
[+] Running 3/3
✔ Network root_default Created 0.0s
✔ Container mysql-slave Started 0.3s
✔ Container mysql-master Started
4、配置mysql主服务
# 进入mysql-master容器并登录mysql服务
[root@develop ~]# docker exec -it mysql-master bash
bash-4.2# mysql -uroot -p123456
# 创建复制用户
CREATE USER 'repl'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;
# 查看主服务状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 747 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5、配置mysql从服务
# 进入mysql-slave容器并登录mysql服务
[root@develop ~]# docker exec -it mysql-slave bash
bash-4.2# mysql -uroot -p123456
# 配置主从连接并启动
CHANGE MASTER TO
MASTER_HOST='192.168.80.100',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=747;
START SLAVE;
6、检查主从服务状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 747
Relay_Log_File: 5fc9a5d8447d-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes # 表示主从复制正常
Slave_SQL_Running: Yes # 表示主从复制正常
7、检查主从延迟
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 747
Relay_Log_File: 5fc9a5d8447d-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 747
Relay_Log_Space: 534
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0 # 表示主从延迟时间,时间越短越好。
更多推荐


所有评论(0)