# 主mysql启动
docker run --privileged=true -d -p 3307:3306 --name='mysql_master' \
-e MYSQL_ROOT_PASSWORD=123456 \
-v /opt/mysql_master/log:/var/log/mysql \
-v /opt/mysql_master/data:/var/lib/mysql \
-v /opt/mysql_master/conf:/etc/mysql/conf.d mysql
# 从mysql启动
docker run --privileged=true -d -p 3308:3306 \
-e MYSQL_ROOT_PASSWORD=123456 --name='mysql_slave' \
-v /opt/mysql_slave/log:/var/log/mysql \
-v /opt/mysql_slave/data:/var/lib/mysql \
-v /opt/mysql_slave/conf:/etc/mysql/conf.d mysql
# 主mysql配置
# 主mysql配置文件
vim /opt/mysql_master/conf/my.cnf
# 重启主mysql
docker restart mysql_master
docker exec -it mysql_master /bin/bash
# 创建数据同步的用户
mysql -uroot -p123456
create user 'slave'@'%' identified by '123456';
grant replication slave,replication client on *.* to 'slave'@'%'; # 权限配置
# 确认主mysql的主从状态
show master status;
# 从mysql配置
# 从mysql配置文件
vim /opt/mysql_slave/conf/my.cnf
# 重启从mysql
docker restart mysql_slave
docker exec -it mysql_slave /bin/bash
mysql -uroot -p123456
# 配置主从复制
change master to master_host='10.122.1.86', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=784, master_connect_retry=30;
# 参数详解: master_log_file和master_log_pos在主mysql中使用show master status; master_connect_retry连接失败重试间隔时间; master_log_pos指定数据库从哪个位置开始复制数据
# 开启主从复制
start slave;
# 确认
show slave status \G;
<details>
<summary>主mysql配置文件</summary>
<pre><code>
mysqld
server-id=101
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
</code></pre>
</details>
<details>
<summary>从mysql配置文件</summary>
<pre><code>
mysqld
server-id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-slave1-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=mall-mysql-relay-bin
log_slave_updates=1
read_only=1
</code></pre>
</details>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。