(1)docker 安装mysql 5.7
sudo docker run -p 3307:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.20
sudo docker run -p 3308:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.20
(2) 进入docker 安装vim 修改 /etc/mysql/my.cnf
sudo docker exec -it mysql_master /bin/bash
...
apt-get update
apt-get install vim
vim /etc/mysql/my.cnf
my.cnf cms只是随便起的一个数据库名称
[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=1
binlog-do-db=cms
replicate-do-db=cms
(3) master 和 slave 执行
master
-- CREATE USER --
create user 'mark'@'%' identified by '12345678';
SET PASSWORD FOR 'mark'@'%' = PASSWORD('12345678');
GRANT REPLICATION SLAVE ON *.* TO 'mark'@'%';
grant replication client,replication slave on *.* to 'mark'@'192.168.3.179' identified by '12345678';
-- SET MASTER --
STOP SLAVE IO_THREAD;
STOP SLAVE SQL_THREAD ;
CHANGE MASTER TO
MASTER_HOST='192.168.3.179',MASTER_PORT=3308,
MASTER_USER='mark',
MASTER_PASSWORD='12345678',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=1373;
START SLAVE ;
slave
-- CREATE USER --
create user 'mark'@'%' identified by '12345678';
SET PASSWORD FOR 'mark'@'%' = PASSWORD('12345678');
GRANT REPLICATION SLAVE ON *.* TO 'mark'@'%';
grant replication client,replication slave on *.* to 'mark'@'192.168.3.179' identified by '12345678';
-- SET MASTER --
STOP SLAVE IO_THREAD ;
STOP SLAVE SQL_THREAD ;
CHANGE MASTER TO
MASTER_HOST='192.168.3.179',MASTER_PORT=3307,
MASTER_USER='mark',
MASTER_PASSWORD='12345678',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=1373;
START SLAVE ;
其中 MASTER_LOG_FILE 和 MASTER_LOG_POS要由另外的mysql
SHOW MASTER STATUS ;
给出
然后建表插入都在cms下操作就会相互同步了。