2019独角兽企业重金招聘Python工程师标准>>>
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
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;
记住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中