背景
“Relay_Log_File” 是 MySQL 中用于复制的参数之一。在 MySQL 复制中,当一个服务器作为主服务器(master)时,它会将其更改写入二进制日志文件(binary log file)。而另一个服务器作为从服务器(slave)时,则会从主服务器复制这些更改,并将它们写入中继日志文件(relay log file)。
需求:
- 搭建的主从复制数据库出现了故障,导致slave异常,同步已经失效。
- 再次重建主从关系时,发现硬盘被大量的Relay_Log_File文件占满,需要清理并释放空间。
- Relay_Log_File文件名称如下图:
数据目录/data硬盘空间已经被占用73%
[ptmauser@mysql-0002 data]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 31G 0 31G 0% /dev
tmpfs 31G 12K 31G 1% /dev/shm
tmpfs 31G 2.3G 29G 8% /run
tmpfs 31G 0 31G 0% /sys/fs/cgroup
/dev/mapper/klas-root 93G 54G 40G 58% /
tmpfs 31G 56K 31G 1% /tmp
/dev/vda1 2.0G 219M 1.8G 11% /boot
/dev/vda2 1022M 20K 1022M 1% /boot/efi
tmpfs 6.2G 0 6.2G 0% /run/user/993
/dev/mapper/datavg-data_lv01 200G 145G 56G 73% /data
tmpfs 6.2G 0 6.2G 0% /run/user/1000
tmpfs 60M 0 60M 0% /var/log/rtlog
tmpfs 6.2G 0 6.2G 0% /run/user/1001
解决:
- 登录mysql-cli,关闭slave实例,通过reset slave实例清除Relay_Log_File文件。
实际操作
登录MySQL-cli
[ptmauser@mysql-0002 data]$ mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.42-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
检查slave状态
MySQL [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.22.88.15
Master_User: repl
Master_Port: 5575
Connect_Retry: 60
Master_Log_File: binlog.000097
Read_Master_Log_Pos: 911621900
Relay_Log_File: mysql-0002-relay-bin.000002
Relay_Log_Pos: 148188
Relay_Master_Log_File: binlog.000005
Slave_IO_Running: No
Slave_SQL_Running: No
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: 148680
Relay_Log_Space: 152830684793
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: NULL
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: 0
Master_UUID: a90b3d6a-419c-11ee-ab08-fa163e5b8157
Master_Info_File: /data/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: a90b3d6a-419c-11ee-ab08-fa163e5b8157:8-2042286
Executed_Gtid_Set: 9553cced-419c-11ee-8f4c-fa163e7c44c3:1-4,
a90b3d6a-419c-11ee-ab08-fa163e5b8157:8-154
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.000 sec)
ERROR: No query specified
尝试重启slave实例,并检查状态
MySQL [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
MySQL [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 172.22.88.15
Master_User: repl
Master_Port: 5575
Connect_Retry: 60
Master_Log_File: binlog.000101
Read_Master_Log_Pos: 422443924
Relay_Log_File: mysql-0002-relay-bin.000002
Relay_Log_Pos: 148188
Relay_Master_Log_File: binlog.000005
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1008
Last_Error: Error 'Can't drop database 'db_api_data_manager_yw'; database doesn't exist' on query. Default database: 'db_api_data_manager_yw'. Query: 'DROP DATABASE `db_api_data_manager_yw`'
Skip_Counter: 0
Exec_Master_Log_Pos: 148680
Relay_Log_Space: 153306313854
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1008
Last_SQL_Error: Error 'Can't drop database 'db_api_data_manager_yw'; database doesn't exist' on query. Default database: 'db_api_data_manager_yw'. Query: 'DROP DATABASE `db_api_data_manager_yw`'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 8815
Master_UUID: a90b3d6a-419c-11ee-ab08-fa163e5b8157
Master_Info_File: /data/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 231115 13:59:38
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: a90b3d6a-419c-11ee-ab08-fa163e5b8157:8-2063342
Executed_Gtid_Set: 9553cced-419c-11ee-8f4c-fa163e7c44c3:1-4,
a90b3d6a-419c-11ee-ab08-fa163e5b8157:8-154
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.000 sec)
ERROR: No query specified
确认主从关系丢失,已出现了具体的同步报错:
Last_SQL_Error: Error 'Can't drop database 'db_api_data_manager_yw'; database doesn't exist' on query. Default database: 'db_api_data_manager_yw'. Query: 'DROP DATABASE `db_api_data_manager_yw`'
之后再想办法重建主从关系,本文先清理Relay_Log_File文件。
重置slave实例
在MySQL-cli中关闭slave实例,重置slave实例,MySQL将自动清理硬盘上产生的Relay_Log_File文件。
MySQL [(none)]> stop slave;
Query OK, 0 rows affected (0.001 sec)
MySQL [(none)]> reset slave;
Query OK, 0 rows affected (2.699 sec)
MySQL [(none)]> exit
退出MySQL-cli,验证数据目录及relay_log_file文件。
检查硬盘空间及Relay_Log_File
大量堆积的relay-bin-log被成功自动清除。
数据目录/data/硬盘空间已经自动清理,使用率重新回到正常数值2%。
[ptmauser@mysql-0002 data]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 31G 0 31G 0% /dev
tmpfs 31G 12K 31G 1% /dev/shm
tmpfs 31G 2.3G 29G 8% /run
tmpfs 31G 0 31G 0% /sys/fs/cgroup
/dev/mapper/klas-root 93G 54G 40G 58% /
tmpfs 31G 56K 31G 1% /tmp
/dev/vda1 2.0G 219M 1.8G 11% /boot
/dev/vda2 1022M 20K 1022M 1% /boot/efi
tmpfs 6.2G 0 6.2G 0% /run/user/993
/dev/mapper/datavg-data_lv01 200G 2.3G 198G 2% /data
tmpfs 6.2G 0 6.2G 0% /run/user/1000
tmpfs 60M 0 60M 0% /var/log/rtlog
tmpfs 6.2G 0 6.2G 0% /run/user/1001
[ptmauser@mysql-0002 data]$
注意:
- 避免手动直接删除relay-bin-log文件。
- 重置slave实例前,先关闭slave实例。