docker搭建mysql主从同步

1. cnf文件

  • 主 master01.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
symbolic-links=0
server-id=10001
log-bin=mysql-bin
  • 从 slave01.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
symbolic-links=0
server-id=20001
log-bin=mysql-bin

2. docker启动脚本

  • 主 mysql-master01.sh
docker stop mysql-master01
docker rm mysql-master01
docker run \
--privileged=true \
--restart=always \
--name mysql-master01 \
-p 3320:3306 \
-v /Users/macintoshhd/docker/mysql/master01-data:/var/lib/mysql \
-v /Users/macintoshhd/docker/mysql/conf/master01.cnf:/etc/mysql/conf.d/master01.cnf \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7.26
  • 从 mysql-slave01.sh
docker stop mysql-slave01
docker rm mysql-slave01
docker run \
--privileged=true \
--restart=always \
--name mysql-slave01 \
-p 3330:3306 \
-v /Users/macintoshhd/docker/mysql/slave01-data:/var/lib/mysql \
-v /Users/macintoshhd/docker/mysql/conf/slave01.cnf:/etc/mysql/conf.d/slave01.cnf \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7.26

3. 配置命令

docker exec -it mysql-master01 /bin/bash
mysql -uroot -p123456
GRANT REPLICATION SLAVE,FILE,REPLICATION CLIENT ON *.* TO 'repluser'@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
  • 获取主机状态
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     619 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
docker exec -it mysql-slave01 /bin/bash
change master to master_host='192.168.50.47', master_port=3320, master_user='repluser', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=619;
start slave;
show slave status\G;

4. 如果显示Yes说明配置成功

image.png

5. 操作目录结构

/Users/macintoshhd/docker/mysql/
├── conf/
│   ├── master01.cnf
│   └── slave01.cnf
├── master01-data/
├── slave01-data/
├── mysql-master01.sh
└── mysql-slave01.sh

你可能感兴趣的:(docker搭建mysql主从同步)