环境
MySQL 8.0.27
docker 20.10.11
流程
1. 准备配置文件
目录
--mysql_master
----data
----conf
------my.cnf
--mysql_slave
----data
----conf
------my.cnf
主库的my.cnf
[mysqld]
# 设置server-id,唯一值,标识主机,必须与从库不一致
server_id=1
# 开启二进制日志,主库必须开启
log-bin=mysql-bin
#设置只记录binlog的库
binlog-do-db=demo
从库的my.cnf
# 设置server-id,唯一值,标识主机,必须与主库不一致
[mysqld]
server_id=2
#设置只同步binlog的库
replicate-do-db=demo
2. 启动
# 启动主库
docker run --name mysqlmaster -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -v D:\docker_volum\mysql\mysql-master\data:/var/lib/mysql -v D:\docker_volum\mysql\mysql-master\conf:/etc/mysql/conf.d mysql:8.0
# 启动从库
docker run --name mysqlslave -d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 -v D:\docker_volum\mysql\mysql-slave\data:/var/lib/mysql -v D:\docker_volum\mysql\mysql-slave\conf:/etc/mysql/conf.d mysql:8.0
3. 主库创建账号
# 进入主库
docker exec -it mysqlmaster bash
# 登录mysql
mysql -uroot -p
# 创建用户
mysql> create user 'replicationuser'@'%' identified by '123456';
# 授权
mysql> grant replication slave,replication client on *.* to 'replicationuser'@'%';
# 查看主库状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 719 | demo | | |
+------------------+----------+--------------+------------------+-------------------+
4. 从库配置主从
# 进入从库
docker exec -it mysqlslave bash
# 登录从库
mysql -uroot -p
# 配置master信息,注意master_log_file和master_log_pos为刚刚show master获取到的值,
mysql> change master to master_host='172.17.0.5',master_port=3306,master_user='replicationuser',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=719;
# 开启主从
mysql> start slave;
# 查看连接状态
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 172.17.0.5
Master_User: zhenghong
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 719
Relay_Log_File: 85e372088d86-relay-bin.000003
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 719
Relay_Log_Space: 719
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: b20adecb-6167-11ec-a545-0242ac110005
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
Slave_IO_Running 和Slave_SQL_Running 都为YES,则说明配置成功了。
这里有几种异常情况,show slave status \G 的last_IO_error 字段展示了错误的原因。
情况1:Authentication requires secure connection
Last_IO_Errno: 2061
Last_IO_Error: error connecting to master 'replicationuser@172.17.0.2:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection
解决方法:在从库的机器上复制用户请求服务器公钥
mysql -u replicationuser -p -h 172.17.0.2 -P3306 --get-server-public-key
情况2:master and slave have equal MySQL server ids
Last_IO_Errno: 13117
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
主库和从库的server-id一样了,刚刚配置文件中的server-id 配置的的确是不同的。
mysql>show variables like 'server_id';
mysql>set global server_id=2; #此处的数值和my.cnf里设置的一样就行
情况3:Slave failed to initialize relay log info structure from the repository
Last_SQL_Errno: 13124
Last_SQL_Error: Slave failed to initialize relay log info structure from the repository
之前的日志仍然保留着slave的信息
mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.32 sec)
mysql> change master to master_host='172.17.0.2',master_port=3306,master_user='replicationuser',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=156;
Query OK, 0 rows affected, 9 warnings (0.31 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.04 sec)
5. 测试
主库
mysql> create database demo;
Query OK, 1 row affected (0.01 sec)
mysql> use demo;
Database changed
mysql> create table t_user(id bigint primary key ,name varchar(200));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t_user values(1,'mindong');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_user;
+----+---------+
| id | name |
+----+---------+
| 1 | mindong |
+----+---------+
1 row in set (0.00 sec)
从库
可以看到添加的表和数据已经同步过来了。
mysql> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_user;
+----+---------+
| id | name |
+----+---------+
| 1 | mindong |
+----+---------+
1 row in set (0.00 sec)
网友评论