准备:一台装有docker的虚拟机或者服务器
- 拉取mysql镜像:
docker pull mysql:5.6
- 启动两个mysql容器
master
docker run -p 1006:3306 --name mysql_master -v F:/mysql/mysql_master/conf:/etc/mysql -v F:/mysql/mysql_master/logs:/logs -v F:/mysql/mysql_master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -itd mysql:5.6
slave
docker run -p 1007:3306 --name mysql_salve -v F:/mysql/mysql_salve/conf:/etc/mysql -v F:/mysql/mysql_salve/logs:/logs -v F:/mysql/mysql_salve/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -itd mysql:5.6
- 查看容器启动情况
docker ps
-
使用Navicat工具创建两个连接,分别为master和slave
-
开启binlog日志
主库 修改 F:/mysql/mysql_master/conf/my.cnf
对应容器内路径是/etc/mysql/my.cnf
[mysqld]
server_id=1
log_bin=mysql-bin
binlog_format=ROW
从库
[mysqld]
server-id = 2
relay_log = /var/lib/mysql/mysql-relay-bin
relay_log-index = /var/lib/mysql/mysql-relay-bin.index
log_slave_updates = 1
read_only = 1
- 配置好后重启docker容器
docker resatrt 容器名或者容器id
- 查看binlog状态
show global variables like '%log_bin%';
log_bin为ON是开启状态
- 在master数据库(主库)中创建同步用户授予用户slave REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据。
- 创建用户
CREATE USER 'slave'@'192.168.11.%' IDENTIFIED BY 'slave'
- 授予用户slave同步权限
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
- 刷新权限
flush privileges;
-
查询主库File和Position
show master status;
- slave库链接master
change master to master_host='192.168.11.1', master_user='slave', master_password='slave', master_port=1006, master_log_file='mysql-bin.000004', master_log_pos= 1271;
-
启动从库
start slave;
12.查看从库状态
show slave status;
当Slave_IO_Running和Slave_SQL_Running为Yes时,表示主从配置成功。