基于本地测试,部署MySQL的主从复制环境
数据库的配置文件和数据文件存在本地,文件夹和目录结构如下:
mysql├── master│ ├── conf│ │ └── my.cnf│ └── data└── slaver ├── conf │ └── my.cnf └── data复制代码Master配置: my.cnf
[mysqld]# 用于标识不同的数据库服务器,而且唯一server_id = 1# 需要启用二进制日志log-bin= mysql-binread-only=0# 指定同步的数据库binlog-do-db=test_db# 忽略记录二进制日志的数据库replicate-ignore-db=mysqlreplicate-ignore-db=sysreplicate-ignore-db=information_schemareplicate-ignore-db=performance_schema!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/复制代码Slave配置: my.cnf
[mysqld]server_id = 2log-bin= mysql-binread-only=1# 指定同步的数据库binlog-do-db=test_dbreplicate-ignore-db=mysqlreplicate-ignore-db=sysreplicate-ignore-db=information_schemareplicate-ignore-db=performance_schema!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/复制代码
启动创建主从容器
docker pull mysql:5.6// 创建master docker run --name mastermysql -d -p 3316:3306 -e MYSQL_ROOT_PASSWORD=root -v ~/docker/mysql/master/data:/var/lib/mysql -v ~/docker/mysql/master/conf/my.cnf:/etc/mysql/my.cnf mysql:5.6// 创建slavedocker run --name slavermysql -d -p 3326:3306 -e MYSQL_ROOT_PASSWORD=root -v ~/docker/mysql/slaver/data:/var/lib/mysql -v ~/docker/mysql/slaver/conf/my.cnf:/etc/mysql/my.cnf mysql:5.6复制代码
主数据库配置
docker exec -it mastermysql bashmysql -u root -p// 创建一个用户来同步数据GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';// 查看状态,记住File、Position的值,在Slaver中将用到show master status;复制代码
从数据库配置
// 进入slaver容器docker exec -it slavermysql bashmysql -u root -p//设置主库链接mysql> change master to master_host='192.168.10.103',master_user='backup',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=675,master_port=3316;// 启动主从同步mysql> start slave;// 查看状态mysql> show slave status;说明:master_host:主库地址master_user:主库创建的同步账号master_password:主库创建的同步密码master_log_file:主库产生的日志master_log_pos:主库日志记录偏移量master_port:主库使用的端口,默认为3306复制代码