Docker搭建Mysql集群、主从同步复制

1、创建数据挂载点:

mkdir /opt/mysql-master/mysql、/opt/mysql-master/conf.d、/opt/mysql-slave/mysql、/opt/mysql-slave/conf.d

2、分别在master、slave节点文件目录conf.d下创建touch my.cnf、编辑vim my.cnf;注意service-i要唯一

log-bin=master-bin
server-id=1

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

3、容器的方式启动master、slave

docker run -p 3306:3306 -d --name mysql-master -v /opt/mysql-master/mysql:/var/lib/mysql -v /opt/mysql-master/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 

docker run -p 3306:3306 -d --name mysql-slave -v /opt/mysql-slave/mysql:/var/lib/mysql -v /opt/mysql-slave/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 

4、在master节点上执行允许所有的远程访问

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

Docker搭建Mysql集群、主从同步复制_第1张图片

记住file和position的值,在slave下数据库执行

1、change master to master_host='master服务器ip', master_user='root',master_password='master数据库密码', master_log_file='上图file',master_log_pos=上图position;

2、start slave --启用slave

主从同步搭建完成、在master添加操作会自动同步到slave中

你可能感兴趣的:(Docker,Mysql)