![Mysql不断写入binlog,迅速耗尽磁盘空间](https://linux22.com/image/777257/Mysql%E4%B8%8D%E6%96%AD%E5%86%99%E5%85%A5binlog%EF%BC%8C%E8%BF%85%E9%80%9F%E8%80%97%E5%B0%BD%E7%A3%81%E7%9B%98%E7%A9%BA%E9%97%B4.png)
我的 Ubuntu 服务器开始运行大量 IO 操作。服务器上有几个 WordPress 网站,但它们每天最多只有几十次访问。几天之内就用掉了 30GB 的磁盘空间。
检查iotop
运行结果iotop
显示mysql在不断写入磁盘。典型的输出如下:
Total DISK READ: 0.00 B/s | Total DISK WRITE: 390.38 K/s
Current DISK READ: 0.00 B/s | Current DISK WRITE: 664.80 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
298 be/3 root 0.00 B/s 0.00 B/s 0.00 % 4.79 % [jbd2/vda1-8]
981 be/4 mysql 0.00 B/s 0.00 B/s 0.00 % 0.55 % mysqld [ib_log_flush]
960 be/4 mysql 0.00 B/s 0.00 B/s 0.00 % 0.42 % mysqld [ib_io_wr-1]
63310 be/4 mysql 0.00 B/s 30.92 K/s 0.00 % 0.17 % mysqld [connection]
62908 be/4 mysql 0.00 B/s 34.79 K/s 0.00 % 0.09 % mysqld [connection]
64165 be/4 mysql 0.00 B/s 34.79 K/s 0.00 % 0.07 % mysqld [connection]
964 be/4 mysql 0.00 B/s 185.52 K/s 0.00 % 0.05 % mysqld [ib_pg_flush_co]
983 be/4 mysql 0.00 B/s 100.49 K/s 0.00 % 0.00 % mysqld [ib_log_writer]
71067 be/4 www-data 0.00 B/s 3.87 K/s 0.00 % 0.00 % apache2 -k start
确实,检查目录后/var/lib/mysql
发现有数百个 binlog 文件,总计约 30GB。时间戳表明 mysql 以接近每小时 1GB 的速度写入 binlog,并且没有减慢的迹象。
检查 mysql 进程
运行mysql -p -e "show processlist"
查看mysql进程,没有显示任何内容。
+--------+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+------+-----------+------+---------+------+-------+------------------+
| 627525 | root | localhost | NULL | Query | 0 | init | show processlist |
+--------+------+-----------+------+---------+------+-------+------------------+
检查 binlog 文件
使用mysqlbinlog
查看 binlog 文件显示它们充满了某种哈希值。典型的文件如下所示:
# The proper term is pseudo_replica_mode, but we use this compatibility alias
# to make the statement usable on server versions 8.0.24 and older.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220515 4:09:54 server id 1 end_log_pos 126 CRC32 0x070b8f09 Start: binlog v 4, server v 8.0.29-0ubuntu0.20.04.3 created 220515 4:09:54
BINLOG '
En2AYg8BAAAAegAAAH4AAAAAAAQAOC4wLjI5LTB1YnVudHUwLjIwLjA0LjMAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEwANAAgAAAAABAAEAAAAYgAEGggAAAAICAgCAAAACgoKKioAEjQA
CigAAQmPCwc=
'/*!*/;
# at 126
#220515 4:09:54 server id 1 end_log_pos 157 CRC32 0x433aa4c9 Previous-GTIDs
# [empty]
# at 157
#220515 4:09:54 server id 1 end_log_pos 236 CRC32 0x671d08bc Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=yes original_committed_timestamp=1652587794635604 immediate_commit_timestamp=1652587794635604 transaction_length=14092
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1652587794635604 (2022-05-15 04:09:54.635604 UTC)
# immediate_commit_timestamp=1652587794635604 (2022-05-15 04:09:54.635604 UTC)
/*!80001 SET @@session.original_commit_timestamp=1652587794635604*//*!*/;
/*!80014 SET @@session.original_server_version=80029*//*!*/;
/*!80014 SET @@session.immediate_server_version=80029*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 236
#220515 4:09:54 server id 1 end_log_pos 334 CRC32 0x71a6c06f Query thread_id=614826 exec_time=0 error_code=0
SET TIMESTAMP=1652587794/*!*/;
SET @@session.pseudo_thread_id=614826/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1149239296/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=246,@@session.collation_connection=246,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
# at 334
#220515 4:09:54 server id 1 end_log_pos 415 CRC32 0x6976a620 Table_map: `wordpress-jessjohn`.`wp_options` mapped to number 81
# at 415
#220515 4:09:54 server id 1 end_log_pos 14218 CRC32 0x8cd2158b Update_rows: table id 81 flags: STMT_END_F
BINLOG '
En2AYhMBAAAAUQAAAJ8BAAAAAFEAAAAAAAEAEndvcmRwcmVzcy1qZXNzam9obgAKd3Bfb3B0aW9u
cwAECA/8DwX8AgRQAAABAYACAfYgpnZp
En2AYh8BAAAA6zUAAIo3AAAAAFEAAAAAAAEAAgAE//8AHQAAAAAAAAANAHJld3JpdGVfcnVsZXMA
AAAAA3llcwAdAAAAAAAAAA0AcmV3cml0ZV9ydWxlc4c1AABhOjE0MDp7czoxMToiXndwLWpzb24v
PyQiO3M6MjI6ImluZGV4LnBocD9yZXN0X3JvdXRlPS8iO3M6MTQ6Il53cC1qc29uLyguKik/Ijtz
OjMzOiJpbmRleC5waHA/cmVzdF9yb3V0ZT0vJG1hdGNoZXNbMV0iO3M6MjE6Il5pbmRleC5waHAv
d3AtanNvbi8/JCI7czoyMjoiaW5kZXgucGhwP3Jlc3Rfcm91dGU9LyI7czoyNDoiXmluZGV4LnBo
cC93cC1qc29uLyguKik/IjtzOjMzOiJpbmRleC5waHA/cmVzdF9yb3V0ZT0vJG1hdGNoZXNbMV0i
O3M6MTc6Il53cC1zaXRlbWFwXC54bWwkIjtzOjIzOiJpbmRleC5waHA/c2l0ZW1hcD1pbmRleCI7
... 245 lines of this ...
dD0kbWF0Y2hlc1sxXSZjcGFnZT0kbWF0Y2hlc1syXSI7czoyMjoiW14vXSsvKFteL10rKS9lbWJl
ZC8/JCI7czo0MzoiaW5kZXgucGhwP2F0dGFjaG1lbnQ9JG1hdGNoZXNbMV0mZW1iZWQ9dHJ1ZSI7
fQN5ZXOLFdKM
'/*!*/;
# at 14218
#220515 4:09:54 server id 1 end_log_pos 14249 CRC32 0x322d3658 Xid = 18716628
COMMIT/*!*/;
# at 14249
#220515 4:09:54 server id 1 end_log_pos 14329 CRC32 0xc4b6c15a Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=yes original_committed_timestamp=1652587794702570 immediate_commit_timestamp=1652587794702570 transaction_length=100144
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1652587794702570 (2022-05-15 04:09:54.702570 UTC)
# immediate_commit_timestamp=1652587794702570 (2022-05-15 04:09:54.702570 UTC)
/*!80001 SET @@session.original_commit_timestamp=1652587794702570*//*!*/;
/*!80014 SET @@session.original_server_version=80029*//*!*/;
/*!80014 SET @@session.immediate_server_version=80029*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 14329
#220515 4:09:54 server id 1 end_log_pos 14427 CRC32 0x1328bf8c Query thread_id=614825 exec_time=0 error_code=0
SET TIMESTAMP=1652587794/*!*/;
BEGIN
/*!*/;
# at 14427
#220515 4:09:54 server id 1 end_log_pos 14507 CRC32 0x64436ee3 Table_map: `wordpress-jessjohn`.`wp_usermeta` mapped to number 95
# at 14507
#220515 4:09:54 server id 1 end_log_pos 114362 CRC32 0xec16092b Update_rows: table id 95 flags: STMT_END_F
BINLOG '
En2AYhMBAAAAUAAAAKs4AAAAAF8AAAAAAAEAEndvcmRwcmVzcy1qZXNzam9obgALd3BfdXNlcm1l
dGEABAgID/wD/AMEDAEBwAIB9uNuQ2Q=
En2AYh8BAAAAD4YBALq+AQAAAF8AAAAAAAEAAgAE//8ANwAAAAAAAAABAAAAAAAAAA4Ac2Vzc2lv
... and so on for another >200 lines
是什么原因导致所有这些记录?
我不太熟悉 mysql 日志记录,所以不知道接下来该怎么做。我想一个快速的解决方法就是关闭日志记录。我不明白 binlog 文件在说什么,也不明白是什么原因导致记录了这么多内容。
答案1
MySQL 中的 Binlog 用于主服务器和从服务器之间的复制。如果您不使用这种架构,可以通过执行以下命令禁用它:
SET sql_log_bin = 0;
如果您具有这样的结构,则可以使用以下命令删除旧的(已复制的)记录:
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';
或者
PURGE BINARY LOGS TO 'mysql-bin.010';
如需更多信息,您可以使用这个答案。