docker 主从mysql配置

1、主机容器
docker run -d --name mysql-master --network my_network -p 3307:3306 -e MYSQL_ROOT_PASSWORD=密码 mysql

2、从机容器
docker run -d --name mysql-slave --network my_network -p 3308:3306 -e MYSQL_ROOT_PASSWORD=密码 mysql

3、在主机修改mysql配置
[mysqld]
log-bin = mysql-bin
server-id = 1 #主机是1

重启容器

4、在主机数据库中运行

CREATE USER'replication_user'@'%' IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE ON *.* TO'replication_user'@'%';
ALTER USER'replication_user'@'%' IDENTIFIED WITH mysql_native_password BY'密码';


5、在从机修改mysql配置
[mysqld]
log-bin = mysql-bin
server-id = 2 #主机是2

重启容器

6、在从机数据库中运行

CHANGE MASTER TO MASTER_HOST='从机获得的IP地址',MASTER_PORT=3306, MASTER_USER='replication_user', MASTER_PASSWORD='密码', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0;
START SLAVE;

 

还有一个问题,当前docker的mysql官方容器已经更新到9.0.1

1、主服务器配置更新

CREATE USER'replica_user'@'%' IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE ON *.* TO'replica_user'@'%';
ALTER USER'replica_user'@'%' IDENTIFIED WITH caching_sha2_password BY '密码';
FLUSH PRIVILEGES;
 
2、从服务器设置
STOP REPLICA;
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='mysql-master',
  SOURCE_USER='replica_user',
  SOURCE_PASSWORD='密码',
  SOURCE_SSL=1,
  SOURCE_LOG_FILE='mysql-bin.000001',
  SOURCE_LOG_POS=0;

START REPLICA;
posted @ 2024-08-03 16:58  王伟晔  阅读(16)  评论(0编辑  收藏  举报