主从复制mysql文章源自懂站帝-http://www.sfdkj.com/13716.html
实战版20220520验证OK文章源自懂站帝-http://www.sfdkj.com/13716.html
主搭建步骤文章源自懂站帝-http://www.sfdkj.com/13716.html
1、新建主服务器容器实例3307文章源自懂站帝-http://www.sfdkj.com/13716.html
#docker run -p 3307:3306 --name mysql-master \文章源自懂站帝-http://www.sfdkj.com/13716.html
-v /mydata/mysql-master/log:/var/log/mysql \文章源自懂站帝-http://www.sfdkj.com/13716.html
-v /mydata/mysql-master/data:/var/lib/mysql \文章源自懂站帝-http://www.sfdkj.com/13716.html
-v /mydata/mysql-master/conf:/etc/mysql \文章源自懂站帝-http://www.sfdkj.com/13716.html
-e MYSQL_ROOT_PASSWORD=123.com \文章源自懂站帝-http://www.sfdkj.com/13716.html
-d mysql:5.7文章源自懂站帝-http://www.sfdkj.com/13716.html
2、进入/mydata/mysql-master/conf目录下新建my.cnf文章源自懂站帝-http://www.sfdkj.com/13716.html
#cd mydata/mysql-master/conf文章源自懂站帝-http://www.sfdkj.com/13716.html
#vim my.cnf文章源自懂站帝-http://www.sfdkj.com/13716.html
[mysqld]文章源自懂站帝-http://www.sfdkj.com/13716.html
##设置server_id,同一局域网中需要唯一文章源自懂站帝-http://www.sfdkj.com/13716.html
server_id=101文章源自懂站帝-http://www.sfdkj.com/13716.html
##指定不需要同步的数据库名称文章源自懂站帝-http://www.sfdkj.com/13716.html
binlog-ignore-db=mysql文章源自懂站帝-http://www.sfdkj.com/13716.html
##开启二进制日志功能文章源自懂站帝-http://www.sfdkj.com/13716.html
log-bin=mall-mysql-bin文章源自懂站帝-http://www.sfdkj.com/13716.html
##设置二进制日志使用内存大小(事务)文章源自懂站帝-http://www.sfdkj.com/13716.html
binlog_cache_size=1M文章源自懂站帝-http://www.sfdkj.com/13716.html
##设置使用二进制日志格式(mixed,statement,row)文章源自懂站帝-http://www.sfdkj.com/13716.html
binlog_format=mixed文章源自懂站帝-http://www.sfdkj.com/13716.html
##二进制日志过期清理时间,默认值为0:表示不自动清理文章源自懂站帝-http://www.sfdkj.com/13716.html
expire_logs_days=7文章源自懂站帝-http://www.sfdkj.com/13716.html
#跳过主从复制中遇到的所有错误或者指定类型的错误,避免slave端复制中断文章源自懂站帝-http://www.sfdkj.com/13716.html
#如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致文章源自懂站帝-http://www.sfdkj.com/13716.html
slave_skip_errors=1062文章源自懂站帝-http://www.sfdkj.com/13716.html
3、修改完配置后重启master实例文章源自懂站帝-http://www.sfdkj.com/13716.html
#docker restart mysql-master文章源自懂站帝-http://www.sfdkj.com/13716.html
4、进入Mysql-master容器文章源自懂站帝-http://www.sfdkj.com/13716.html
#docker exec -it mysql-master /bibn/bash文章源自懂站帝-http://www.sfdkj.com/13716.html
/mysql -u root -p文章源自懂站帝-http://www.sfdkj.com/13716.html
mysql>文章源自懂站帝-http://www.sfdkj.com/13716.html
5、master容器实例内创建数据同步用户文章源自懂站帝-http://www.sfdkj.com/13716.html
建立用户文章源自懂站帝-http://www.sfdkj.com/13716.html
msyql>CREATE USER 'slave'@'%' IDENTIFIED BY '123.com';文章源自懂站帝-http://www.sfdkj.com/13716.html
给用户授权文章源自懂站帝-http://www.sfdkj.com/13716.html
mysql>GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';文章源自懂站帝-http://www.sfdkj.com/13716.html
从搭建步骤文章源自懂站帝-http://www.sfdkj.com/13716.html
6、新建从服务器容器实例3308文章源自懂站帝-http://www.sfdkj.com/13716.html
#docker run -p 3308:3306 --name mysql-slave \文章源自懂站帝-http://www.sfdkj.com/13716.html
-v /mydata/mysql-slave/log:/var/log/mysql \文章源自懂站帝-http://www.sfdkj.com/13716.html
-v /mydata/mysql-slave/data:/var/lib/mysql \文章源自懂站帝-http://www.sfdkj.com/13716.html
-v /mydata/mysql-slave/conf:/etc/mysql \文章源自懂站帝-http://www.sfdkj.com/13716.html
-e MYSQL_ROOT_PASSWORD=123.com \文章源自懂站帝-http://www.sfdkj.com/13716.html
-d mysql:5.7文章源自懂站帝-http://www.sfdkj.com/13716.html
7、进入/mydata/mysql-slave/conf目录下新建my.cnf文章源自懂站帝-http://www.sfdkj.com/13716.html
#cd /mydata/mysql-slave/conf文章源自懂站帝-http://www.sfdkj.com/13716.html
#vim my.cnf文章源自懂站帝-http://www.sfdkj.com/13716.html
[mysqld]文章源自懂站帝-http://www.sfdkj.com/13716.html
##设置server_id,同一局域网中需要唯一文章源自懂站帝-http://www.sfdkj.com/13716.html
server_id=102文章源自懂站帝-http://www.sfdkj.com/13716.html
##指定不需要同步的数据库名称文章源自懂站帝-http://www.sfdkj.com/13716.html
binlog-ignore-db=mysql文章源自懂站帝-http://www.sfdkj.com/13716.html
##开启二进制日志功能文章源自懂站帝-http://www.sfdkj.com/13716.html
log-bin=mall-mysql-bin文章源自懂站帝-http://www.sfdkj.com/13716.html
##设置二进制日志使用内存大小(事务)文章源自懂站帝-http://www.sfdkj.com/13716.html
binlog_cache_size=1M文章源自懂站帝-http://www.sfdkj.com/13716.html
##设置使用二进制日志格式(mixed,statement,row)文章源自懂站帝-http://www.sfdkj.com/13716.html
binlog_format=mixed文章源自懂站帝-http://www.sfdkj.com/13716.html
##二进制日志过期清理时间,默认值为0:表示不自动清理文章源自懂站帝-http://www.sfdkj.com/13716.html
expire_logs_days=7文章源自懂站帝-http://www.sfdkj.com/13716.html
##跳过主从复制中遇到的所有错误或者指定类型的错误,避免slave端复制中断文章源自懂站帝-http://www.sfdkj.com/13716.html
#如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致文章源自懂站帝-http://www.sfdkj.com/13716.html
slave_skip_errors=1062文章源自懂站帝-http://www.sfdkj.com/13716.html
##relay_log配置中继日志文章源自懂站帝-http://www.sfdkj.com/13716.html
relay_log=mall-mysql-relay-bin文章源自懂站帝-http://www.sfdkj.com/13716.html
##log_slave_uodates表示slave将复制事件写进自己的二进制日志文章源自懂站帝-http://www.sfdkj.com/13716.html
log_slave_updates=1文章源自懂站帝-http://www.sfdkj.com/13716.html
##slave设置为只读(具有super权限的用户除外)文章源自懂站帝-http://www.sfdkj.com/13716.html
read_only=1文章源自懂站帝-http://www.sfdkj.com/13716.html
8、修改完配置后重启slave实例文章源自懂站帝-http://www.sfdkj.com/13716.html
#docker restart mysql-slave文章源自懂站帝-http://www.sfdkj.com/13716.html
9、在主数据库中查看主从同步状态文章源自懂站帝-http://www.sfdkj.com/13716.html
#show master status;文章源自懂站帝-http://www.sfdkj.com/13716.html
注意:文章源自懂站帝-http://www.sfdkj.com/13716.html
MASTER_LOG_FILE='mysql-bin.000002',文章源自懂站帝-http://www.sfdkj.com/13716.html
MASTER_LOG_POS=154,文章源自懂站帝-http://www.sfdkj.com/13716.html
10、进入mysql-slave容器文章源自懂站帝-http://www.sfdkj.com/13716.html
#docker exec -it mysql-slave /bin/bash文章源自懂站帝-http://www.sfdkj.com/13716.html
/mysql -u root -p文章源自懂站帝-http://www.sfdkj.com/13716.html
11、在从数据库中配置主从复制(注意)文章源自懂站帝-http://www.sfdkj.com/13716.html
change master to master_host='192.168.52.162',master_user='slave',master_password='123.com',master_port=3307,MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=154,MASTER_CONNECT_RETRY=30;文章源自懂站帝-http://www.sfdkj.com/13716.html
注意:文章源自懂站帝-http://www.sfdkj.com/13716.html
MASTER_LOG_FILE='mysql-bin.000002',文章源自懂站帝-http://www.sfdkj.com/13716.html
MASTER_LOG_POS=154,文章源自懂站帝-http://www.sfdkj.com/13716.html
说明:文章源自懂站帝-http://www.sfdkj.com/13716.html
master_host: 主数据库ip地址文章源自懂站帝-http://www.sfdkj.com/13716.html
master_port: 主数据库运行端口文章源自懂站帝-http://www.sfdkj.com/13716.html
master_user:在主数据库创建用于同步数据的用户账号文章源自懂站帝-http://www.sfdkj.com/13716.html
master_password: 在主数据库创建的用户同步数据的用户密码文章源自懂站帝-http://www.sfdkj.com/13716.html
master_log_file: 指定从数据库要复制数据的日志文件,通过查看主数据库的状态,获取file参数文章源自懂站帝-http://www.sfdkj.com/13716.html
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据库的状态,获position参数文章源自懂站帝-http://www.sfdkj.com/13716.html
master_connect_retry:连接失败重试时间间隔,单位秒文章源自懂站帝-http://www.sfdkj.com/13716.html
12、在从数据库中查看主从同步状态文章源自懂站帝-http://www.sfdkj.com/13716.html
mysql>show slave status \G;文章源自懂站帝-http://www.sfdkj.com/13716.html
13、在从数据库中查看主从同步文章源自懂站帝-http://www.sfdkj.com/13716.html
mysql>start slave;文章源自懂站帝-http://www.sfdkj.com/13716.html
14、查看从数据库状态发现已经同步文章源自懂站帝-http://www.sfdkj.com/13716.html
注意:这里状态出错啦,应该是2个YES文章源自懂站帝-http://www.sfdkj.com/13716.html
解决办法:文章源自懂站帝-http://www.sfdkj.com/13716.html
15、主从复制测试文章源自懂站帝-http://www.sfdkj.com/13716.html
验证:主库里插入数据,从库同步了文章源自懂站帝-http://www.sfdkj.com/13716.html