1、原理
2、实现
1、安装镜像
docker pull mysql:8.0.26
2、新建目录
mkdir -p /home/apps/mysql-master/{config,log,data}
3、创建并启动
docker run -d --name mysql-master \ --restart=always \ --privileged=true \ -p 3310:3306 \ -v /home/apps/mysql-master/config:/etc/mysql/conf.d \ -v /home/apps/mysql-master/log:/var/log/mysql \ -v /home/apps/mysql-master/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:8.0.26
4、新增/修改master基本配置
vim /home/apps/mysql-master/config/my.cnf
添加以下内容
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve
1、同上面操作一样
# 创建目录 mkdir -p /home/apps/mysql-slave-01/{config,log,data} # 启动容器 docker run -d --name mysql-slave-01 \ --restart=always \ --privileged=true \ -p 3310:3306 \ -v /home/apps/mysql-slave-01/config:/etc/mysql/conf.d \ -v /home/apps/mysql-slave-01/log:/var/log/mysql \ -v /home/apps/mysql-slave-01/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:8.0.26 # 修改Slave基本配置 vim /home/apps/mysql-slave-01/config/my.cnf # 添加以下内容 [client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve
1、添加master配置
vim /home/apps/mysql-master/config/my.cnf server_id=1 # 开启二进制日志 log-bin=mysql-bin read-only=0 # 需要同步的数据库 binlog-do-db=rapid-cloud binlog-do-db=rapid-cloud-test # 需要忽略的数据库 replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema
2、重启容器
docker restart mysql-master
3、添加Slave配置
vim /home/apps/mysql-slave-01/config/my.cnf server_id=2 log-bin=mysql-bin read-only=1 binlog-do-db=rapid-cloud binlog-do-db=rapid-cloud-test replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema
4、重启容器
docker restart mysql-slave-01
5、master添加帐号,用来同步的用户
# 进入容器 docker exec -it mysql-master /bin/bash # 进入主库mysql数据库 mysql -u root -p # 授权root可以远程访问( 主从无关,为了方便我们远程连接mysql) # 授权远程 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; # 刷新 flush privileges; # 创建backup用户 # 应先创建新用户 create user 'backup'@'%' identified by '123456'; # 执行授权 grant all privileges on *.* to 'backup'@'%'; # 刷新 flush privileges; # 授权远程 ALTER USER 'backup'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; # 刷新 flush privileges; # 查看主库状态 show master status;
6、在从库里设置主库连接
# 进入容器 docker exec -it mysql-slave-01 /bin/bash # 进入主库mysql数据库 mysql -u root -p change master to master_host='192.168.3.13',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3310;
11、启动从库同步
purge master logs to'mysql-bin.000001';
# 开始同步 start slave; # 停止同步 # stop slave; # 查看同步状态 show slave status\G;
12、错误排查
13、总结: