一、master 创建
1、 在 /home/docker/mysql8-master 目录下新建配置文件 my.cnf:
[mysqld]
server-id=100
log-bin=mysql-bin
binlog-ignore-db = mysql
binlog-ignore-db = sys
binlog-ignore-db = performance_scheme
binlog-ignore-db = information_scheme
binlog_format=row
2、Docker 运行 master
docker run -d \
--name mysql8-master \
-p 3306:3306 \
-v /home/docker/mysql8-master/conf.d:/etc/mysql/conf.d \
-v /home/docker/mysql8-master/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--restart always mysql:8.0.26
二、slave 创建
1、 在 /home/docker/mysql8-slave 目录下新建配置文件 my.cnf:
[mysqld]
server-id=101
log-bin=mysql-slave-bin
relay_log=edu-mysql-relay-bin
binlog-ignore-db = mysql
binlog-ignore-db = sys
binlog-ignore-db = performance_scheme
binlog-ignore-db = information_scheme
binlog_format=row
2、Docker 运行 slave
docker run -d \
--name mysql8-slave \
-p 3307:3306 \
-v /home/docker/mysql8-slave/conf.d:/etc/mysql/conf.d \
-v /home/docker/mysql8-slave/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--restart always mysql:8.0.26
三、master 操作
1、创建用户并授权
创建 slave 用户:
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
授权:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
2、锁表
全局锁表操作,防止position状态码改变:
Flush tables with read lock
3、查看主服务器状态
SHOW MASTER STATUS 注:position需要用在从服务器的配置中需要使用
四、slave 操作
1、配置主从
change master to master_host=‘192.168.0.101’,master_user=‘slave’,master_password=‘123456’,master_log_file=‘mysql-bin.000001’,master_log_pos=1412;
注:此处master_log_pos配置的是主服务器的postion状态值
2、开启服务
start slave;
3、查看从库状态
SHOW SLAVE STATUS
Slave_IO_Running 为 yes 则已经配置成功。
五、master 操作
1、主库解锁表
unlock tables;
问题解决:
1、Master command COM_REGISTER_SLAVE failed: Access denied for user ‘slave’@’%’ (using password: YES) (Errno: 1045)
- 错误原因:从服务器权限不足,在主服务器配置权限
- 解决办法:在主服务器的客户端执行:
GRANT REPLICATION SLAVE ON . TO ‘slave’@’%’;
FLUSH PRIVILEGES;
2、error connecting to master ‘repl@192.168.0.101:3306’ - retry-time: 60 retries: 2 message: Authentication plugin ‘caching_sha2_password’ reported error: Authentication requires secure connection.
- 错误原因:密码加密方式不支持
- 解决办法:在主服务器的客户端执行:
ALTER USER ‘slave’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
FLUSH PRIVILEGES;
3、Error ‘Operation ALTER USER failed for ‘slave’@’%’’ on query. Default database: ‘’. Query: ‘ALTER USER ‘slave’@’%’ IDENTIFIED WITH ‘mysql_native_password’ AS ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9’’
- 错误原因:开启主从服务后,master 修改过用户权限,记录到 log。slave因为没有这个用户,所以执行这个语句报错。
- 解决办法:在主服务器的客户端执行:
方法一:
ALTER USER ‘slave’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
stop slave;
start slave;
方法二:
停止 slave 服务,然后查看 master 的 position 位置,修改位置后,再启动 slave 服务。(将报错的部分信息不要,这样会导致其他数据丢失)
|