mysql主从配置
master 配置
往 my.cnf 添加以下配置
server-id = 1 # 节点ID,确保唯一
log-bin = mysql-bin #开启mysql的binlog日志功能
sync_binlog = 1 #控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
binlog_format = mixed #binlog日志格式,mysql默认采用statement,建议使用mixed
expire_logs_days = 7 #binlog过期清理时间
max_binlog_size = 100m #binlog每个日志文件大小
binlog_cache_size = 4m #binlog缓存大小
max_binlog_cache_size= 512m #最大binlog缓存大
binlog-ignore-db=mysql #不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
auto-increment-offset = 1 # 自增值的偏移量
auto-increment-increment = 1 # 自增值的自增量
slave-skip-errors = all #跳过从库错误
slave 配置
往 my.cnf 添加以下配置
server-id = 2
log-bin=mysql-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
master 创建用户
创建用户
CREATE USER repl_user IDENTIFIED BY '123456';
赋予权限
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%';
FLUSH PRIVILEGES;
slave设置
change master to
master_host='172.18.0.4',
master_user='repl_user',
master_password='123456',
master_log_file='mysql-bin.000002',
master_log_pos=5214;
master_log_file 和 master_log_pos 需与 master 配置一样,在master通过命令:show master status 获取
启动slave
start slave;
查看状态
show slave status \G;
这两个参数都是 Yes 说明链接上了,这时候在master 创建数据库,slave就会复制过来,说明配置成功。
可能会报错:MySQL主从复制报错Authentication plugin ‘caching_sha2_password‘ reported error: Authentication
解决方案:传送门
参考:
1.传送门
2.传送门