我这里一主两从mysql数据库集群,mysql镜像版本是8.0.39
如下
如下:
[root@VM-20-8-centos mysqlData]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mysql-master mysql:8.0 "docker-entrypoint.s…" mysql-master 50 minutes ago Up 50 minutes (healthy) 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp
mysql-slave1 mysql:8.0 "docker-entrypoint.s…" mysql-slave1 50 minutes ago Up 50 minutes (healthy) 3306/tcp, 33060/tcp
mysql-slave2 mysql:8.0 "docker-entrypoint.s…" mysql-slave2 50 minutes ago Up 50 minutes (healthy) 3306/tcp, 33060/tcp
[root@VM-20-8-centos mysqlData]#
创建docker-compose.yml文件
services:
mysql-master:
image: mysql:8.0
container_name: mysql-master
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
ports:
- "3307:3306"
volumes:
- /usr/local/mysqlData/master:/var/lib/mysql
- /usr/local/mysqlConf/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-p rootpassword"]
interval: 10s
retries: 3
start_period: 30s
timeout: 5s
networks:
- mysql-network
mysql-slave1:
image: mysql:8.0
container_name: mysql-slave1
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
volumes:
- /usr/local/mysqlData/slave1:/var/lib/mysql
- /usr/local/mysqlConf/slave1.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-p rootpassword"]
interval: 10s
retries: 3
start_period: 30s
timeout: 5s
networks:
- mysql-network
mysql-slave2:
image: mysql:8.0
container_name: mysql-slave2
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
volumes:
- /usr/local/mysqlData/slave2:/var/lib/mysql
- /usr/local/mysqlConf/slave2.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-p rootpassword"]
interval: 10s
retries: 3
start_period: 30s
timeout: 5s
networks:
- mysql-network
networks:
mysql-network:
注意:/usr/local/mysqlData/slave2 /usr/local/mysqlData/slave1 挂载目录事先创建好
启动命令
docker-compose up -d
创建完成后 docker容器会正常启动
[root@VM-20-8-centos mysqlData]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mysql-master mysql:8.0 "docker-entrypoint.s…" mysql-master 41 minutes ago Up 41 minutes (healthy) 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp
mysql-slave1 mysql:8.0 "docker-entrypoint.s…" mysql-slave1 41 minutes ago Up 41 minutes (healthy) 3306/tcp, 33060/tcp
mysql-slave2 mysql:8.0 "docker-entrypoint.s…" mysql-slave2 41 minutes ago Up 41 minutes (healthy) 3306/tcp, 33060/tcp
[root@VM-20-8-centos mysqlData]#
验证配置变更
这里只是对两个从节点的server_id 手动配置,主节点应该是server_id ,需要事先查看下,是否为1
docker-compose exec mysql-slave1 mysql -uroot -p -e "SET GLOBAL server_id = 2;"
docker-compose exec mysql-slave1 mysql -uroot -p -e "SHOW VARIABLES LIKE 'server_id';"
docker-compose exec mysql-slave2 mysql -uroot -p -e "SET GLOBAL server_id = 3;"
docker-compose exec mysql-slave2 mysql -uroot -p -e "SHOW VARIABLES LIKE 'server_id';"
进入主节点创建复制用户账号
CREATE USER 'replica'@'%' IDENTIFIED BY 'replica_password';
GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%';
FLUSH PRIVILEGES;
# 在主节点上,为复制用户指定 mysql_native_password 插件:
ALTER USER 'replica'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'replica_password';
FLUSH PRIVILEGES;
在主节点查看信息:
获取主节点的二进制日志信息:
首先,您需要在主节点上获取当前的二进制日志文件名和位置。这可以通过在主节点的 MySQL 命令行中执行以下命令来完成:
SHOW MASTER STATUS;
如下
mysql> SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 | 1316 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
记住 binlog.000003 和1316
在从节点去执行同步
CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
MASTER_PORT=3307,
MASTER_USER='replica',
MASTER_PASSWORD='replica_password',
MASTER_LOG_FILE='binlog.000003',
MASTER_LOG_POS=1316;
执行开始命令
START SLAVE;
在从节点查看同步状态
SHOW SLAVE STATUS\G
其他命令
重置从节点的复制:
在从节点上执行以下命令来重置复制状态:
sql
STOP SLAVE;
RESET SLAVE ALL;
删除dokcer-compose
docker-compose down
docker-compose down -v
启动容器
docker-compose up -d