两个服务器上分别是:192.168.56.102(主)、192.168.56.105(从)
主从服务器mysql版本最好一致(从不能高于主)
在两台服务器上启动都启动好两个mysql,安装教程:Linux之centos7下docker 安装部署mysql8.0环境
修改 Master 的配置文件/etc/my.cnf,在my.cnf文件中加入如下配置内容
[mysqld]
#必须唯一
server-id = 100
#开启及设置二进制日志文件名称
log_bin = mysql-bin
binlog_format = MIXED
sync_binlog = 1
#二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
expire_logs_days =7
# binlog_cache_size = 128m
# max_binlog_cache_size = 512m
# max_binlog_size = 256M
# 要同步的数据库
# binlog-do-db = test
# 不需要同步的数据库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys
参数说明:
参数名称 | 说明 |
---|---|
binlog_format | mysql复制主要有三种方式: STATEMENT:基于SQL语句的复制(statement-based replication, SBR) ROW:基于行的复制(row-based replication, RBR) MIXED:混合模式复制(mixed-based replication, MBR) ① STATEMENT模式(SBR) 每一条会修改数据的sql语句会记录到binlog中。优点是并不需要记录每一条sql语句和每一行的数据变化,减少了binlog日志量,节约IO,提高性能。缺点是在某些情况下会导致master-slave中的数据不一致(如sleep()函数, last_insert_id(),以及user-defined functions(udf)等会出现问题) ② ROW模式(RBR) 不记录每条sql语句的上下文信息,仅需记录哪条数据被修改了,修改成什么样了。而且不会出现某些特定情况下的存储过程、或function、或trigger的调用和触发无法被正确复制的问题。缺点是会产生大量的日志,尤其是alter table的时候会让日志暴涨。 ③ MIXED模式(MBR) 以上两种模式的混合使用,一般的复制使用STATEMENT模式保存binlog,对于STATEMENT模式无法复制的操作使用ROW模式保存binlog,MySQL会根据执行的SQL语句选择日志保存方式。 |
expire_logs_days | 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。 |
binlog-do-db | 要同步的数据库 |
binlog-ignore-db | 不需要同步的数据库 |
修改 slave 的配置文件/etc/my.cnf,在my.cnf文件中加入如下配置内容
#必须唯一
server-id = 101
docker restart mysql容器名称
docker exec -it mysql容器名称/id bash
mysql -uroot -p123456(密码)
slave
,密码:123456
#创建用户
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '123456';
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
#分配权限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
#刷新权限
mysql> flush privileges;
SHOW MASTER STATUS
,记录二进制文件名(mysql-bin.000001)和位置(5778)mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
| mysql-bin.000001 | 5778 | | mysql,information_schema,performation_schema,sys | c7a674f5-3600-11eb-903c-0242ac110002:1-21 |
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
1 row in set (0.00 sec)
docker exec -it mysql容器名称/id bash
mysql -uroot -p123456(密码)
注意:MASTER_LOG_FILE和MASTER_LOG_POS要和主库查看出来状态对上
CHANGE MASTER TO
MASTER_HOST='192.168.56.102',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=5778;
mysql> start slave;
mysql> show slave status\G;
展示信息:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.102
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2638
Relay_Log_File: 13b2d81a26f9-relay-bin.000004
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
成功的标志:Slave_IO_Running: Yes和Slave_SQL_Running: Yes
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1 message: Access denied for user 'slave'@'172.17.0.1' (using password: YES)
解决是在少加了MASTER_PORT=3306
#停住备份
stop slave;
#重新设置
CHANGE MASTER TO
MASTER_HOST='192.168.56.102',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=5778;
#开始备份
start slave;
出现这个问题是,mysql容器直接复制导致的,需要修改MySQL server id
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决:找到容器里:/var/lib/mysql/auto.cnf,把这个server-uuid改了,重启mysql容器
[root@centos-105 /usr/local/docker/mysql/data]cat auto.cnf
[auto]
server-uuid=c7a674f5-3600-11eb-903c-0242ac110002
#错误一
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
#错误二
Error 'Operation ALTER USER failed for 'slave'@'%'' on query. Default database: ''. Query: 'ALTER USER 'slave'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9''
出现这个问题是MASTER_LOG_POS=5778;没有设置正确,重新看一下主库的post码重新设置就好了