docker实战教程(十六):mysql主从复制docker版
新建主服务器容器实例3307
docker run -p 3307:3306 --name mysql-master -v /mydata/mysql-master/log:/var/log/mysql -v /mydata/mysql-master/data:/var/lib/mysql -v /mydata/mysql-master/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
进入/mydata/mysql-master/conf目录下新建my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
修改完配置后重启master实例
docker restart mysql-master
进入mysql-master容器
docker exec -it mysql-master /bin/bash
master容器实例内创建数据同步用户
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
新建从服务器3308
docker run -p 3308:3306 --name mysql-slave -v /mydata/mysql-slave/log:/var/log/mysql -v /mydata/mysql-slave/data:/var/lib/mysql -v /mydata/mysql-slave/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
···
# 进入/mydata/mysql-slave/conf目录下新建my.cnf
```linux
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
## relay_log配置中继日志
relay_log=mall-mysql-relay-bin
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
## slave设置为只读(具有super权限的用户除外)
read_only=1
修改完配置后重启slave实例
docker restart mysql-slave
在主数据库中查看主从同步状态
show master status
进入mysql-slave容器
docker exec -it mysql-slave /bin/bash
mysql -u root -p
在从数据库中配置主从复制
change master to master_host='192.168.1.116', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin .000002', master_log_pos=617, master_connect_retry=30;
# master_host:主数据库的IP地址;
# master_port:主数据库的运行端口;
# master_user:在主数据库创建的用于同步数据的用户账号;
# master_password:在主数据库创建的用于同步数据的用户密码;
# master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
# master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
# master_connect_retry:连接失败重试的时间间隔,单位为秒
在从数据库中查看主从同步状态
show slave status \G;
在从数据库中开启主从同步
start slave;
查看从数据库状态发现已经同步
show slave status \G;
如果中间碰到Slave_IO_Running: No 的情况,我是这样处理的,先通过docker logs mysql-slave的容器ID
查看报错日志
2022-11-12T02:27:44.765861Z 0 [Note] Server socket created on IP: '::'.
2022-11-12T02:27:45.081481Z 0 [Warning] Recovery from master pos 617 and file mall-mysql-bin.000002 for channel ''. Previous relay log pos and relay log file had been set to 4, ./mall-mysql-relay-bin.000001 respectively.
2022-11-12T02:27:45.097967Z 0 [Note] Event Scheduler: Loaded 0 events
2022-11-12T02:27:45.098459Z 2 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2022-11-12T02:27:45.100861Z 0 [Note] mysqld: ready for connections.
Version: '5.7.36-log' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
2022-11-12T02:27:45.102285Z 2 [Note] Slave I/O thread for channel '': connected to master 'slave@192.168.1.116:3307',replication started in log 'mall-mysql-bin.000002' at position 617
2022-11-12T02:27:45.102412Z 1 [Note] Slave SQL thread for channel '' initialized, starting replication in log 'mall-mysql-bin.000002' at position 617, relay log './mall-mysql-relay-bin.000002' position: 4
2022-11-12T02:27:45.107964Z 2 [ERROR] Error reading packet from server for channel '': Could not find first log file name in binary log index file (server_errno=1236)
2022-11-12T02:27:45.108000Z 2 [ERROR] Slave I/O for channel '': Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file', Error_code: 1236
很可能是master_log_file名字写错了,这时候再重启master重新生成,重启slave也会自动同步
docker restart mysql-master
mysql -u root -p
show master status
docker restart mysql-slave
mysql -u root -p
show slave status \G
主从复制测试
master
salve