1.安装docker
详细步骤链接: https://www.cnblogs.com/yufeng218/p/8370670.html.
2.安装mysql,33061的mysql作为主机,33062的mysql作为从机
2.1 主机,如果没有pull过mysql镜像,执行如下命令也会去拉取mysql的镜像
docker run --name some-mysql1 -p 33061:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
2.2 从机
docker run --name some-mysql2 -p 33062:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
2.3 查看正在运行的容器
docker ps 
2.4 用本地navicat连接dokcer上的mysql

2.5 如果用navicat连不上mysql可以参考以下地址
链接1: https://blog.csdn.net/qq_38684778/article/details/107230725?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-8.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-8.no_search_link.
3. 在主机some-mysql1建一个用户
grant replication slave on *.* to ‘usr1’@’%’ identified by ‘123’; 
4. 主机some-mysql1的配置
4.1 进入mysql容器
docker exec -it some-mysql1 bash 
4.2 查看mysql配置文件
cat /etc/mysql//mysql.conf.d/mysqld.cnf 
4.3 把原来的mysql配置文件的内容拷贝出来
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
4.3 退出,然后新建一个mysqld.cnf文件,把刚才的内容拷贝进来,开启二进制功能,Esc+:wq保存退出
 
4.4 把新建的mysqld.cnf文件替换掉原来的mysql配置文件,并且重启mysql
docker cp ./mysqld.cnf some-mysql1:etc/mysql/mysql.conf.d/ docker restart some-mysql1 
4.5 使用show master status命令查看主机是否开启了二进制功能,记住File和Position这两参数

4. 从机some-mysql2的配置
4.1 进入到从机
docker exec -it some-mysql2 bash 
4.2 查看从机的配置文件,拷贝内容出来
cat /etc/mysql//mysql.conf.d/mysqld.cnf
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
4.3 删除掉刚才创建的mysqld.cnf文件

4.4 新建mysqld.cnf文件,把刚才的内容拷贝进来,加上server-id,保存退出
 
4.5 将新建的mysqld.cnf替换掉从机的配置文件,并且重启从机
docker cp ./mysqld.cnf some-mysql2:etc/mysql/mysql.conf.d/ docker restart some-mysql2 
4.6 登录进入到从机

4.7 指定主机,开启同步
change master to master_host='192.168.244.129',master_port=33061,master_user='usr1',master_password='123',master_log_file='binlog.000001',master_log_pos=154;

4.8 使用show slave status\G;命令查看同步配置是否成功

5. 测试
5.1 在主机some-mysql1创建一个数据库和一个user表

5.2 可以看到从机some-mysql2也跟着创建了demo1数据库和user表

|