docker部署mysql 8.0主从集群
·
1、拉取镜像
注:国内拉取镜像需要先配置镜像加速器地址
cat /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": [ "https://docker.m.daocloud.io" ]
}
EOF
systemctl daemon-reload
systemctl restart docker
docker pull mysql:8.0.46
2、准备my.cnf配置文件和docker compose配置文件
注:先临时启动mysql服务,拷贝默认的my.cnf文件
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.46
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/
# 修改mysql主配置文件
vim /opt/mysql/master/my.cnf
[mysqld]
server-id = 1 # 主库的唯一ID,必须比从库小
log-bin = mysql-bin # 开启二进制日志
binlog_format = ROW # 推荐使用行级复制,更安全
# 修改mysql从配置文件
vim /opt/mysql/slave/my.cnf
[mysqld]
server-id = 2 # 主库的唯一ID,必须比从库小
log-bin = mysql-bin # 开启二进制日志
binlog_format = ROW # 推荐使用行级复制,更安全
# 准备docker compose配置文件
cat > mysql-8.0.yml <<'EOF'
services:
mysql-master:
container_name: mysql-master
image: mysql:8.0.46
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:8.0.46
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- "/var/lib/mysql"
- "/opt/mysql/slave/my.cnf:/etc/my.cnf"
ports:
- "3307:3306"
EOF
3、启动mysql容器服务
[root@develop ~]# docker compose -f mysql-8.0.yml up -d
[+] Running 3/3
✔ Network root_default Created 0.0s
✔ Container mysql-slave Started 0.4s
✔ Container mysql-master Started
4、配置mysql主服务
# 进入mysql主容器并登录mysql服务
[root@develop ~]# docker exec -it mysql-master bash
bash-5.1# mysql -uroot -p123456
# 创建复制用户
CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;
# 查看主服务状态
mysql> SHOW MASTER STATUS\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 827
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
5、配置mysql从服务
# 进入mysql容器并登录mysql服务
[root@develop ~]# docker exec -it mysql-slave bash
bash-5.1# mysql -uroot -p123456
# 配置主从连接并启动
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='192.168.80.100',
SOURCE_USER='repl',
SOURCE_PASSWORD='123456',
SOURCE_LOG_FILE='mysql-bin.000003',
SOURCE_LOG_POS=827;
START REPLICA;
6、检查主从复制状态
mysql> SHOW REPLICA STATUS\G;
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: 192.168.80.100
Source_User: repl
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: mysql-bin.000003
Read_Source_Log_Pos: 827
Relay_Log_File: a499205f6cf4-relay-bin.000002
Relay_Log_Pos: 326
Relay_Source_Log_File: mysql-bin.000003
Replica_IO_Running: Yes # 表示主从复制正常
Replica_SQL_Running: Yes # 表示主从复制正常
7、检查主从延迟
mysql> SHOW REPLICA STATUS\G;
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: 192.168.80.100
Source_User: repl
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: mysql-bin.000003
Read_Source_Log_Pos: 827
Relay_Log_File: a499205f6cf4-relay-bin.000002
Relay_Log_Pos: 326
Relay_Source_Log_File: mysql-bin.000003
Replica_IO_Running: Yes
Replica_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_Source_Log_Pos: 827
Relay_Log_Space: 543
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Source_SSL_Allowed: No
Source_SSL_CA_File:
Source_SSL_CA_Path:
Source_SSL_Cert:
Source_SSL_Cipher:
Source_SSL_Key:
Seconds_Behind_Source: 0 # 表示主从延迟时间,时间越短越好。
更多推荐


所有评论(0)