在我的服务器中,数据库占用了太多空间:
Filesystem Size Used Avail Use% Mounted on
/dev/root 58G 58G 461M 100% /
tmpfs 966M 0 966M 0% /dev/shm
tmpfs 387M 856K 386M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/nvme0n1p15 105M 6.1M 99M 6% /boot/efi
tmpfs 194M 4.0K 194M 1% /run/user/1000
回答,当我调查它时,我看到那个/vae/lib/mysql
占据了空间:
ncdu 1.15.1 ~ Use the arrow keys to navigate, press ? for help
--- /var/lib
50.6 GiB [##########] /mysql
801.2 MiB [ ] /snapd
138.4 MiB [ ] /apt
90.1 MiB [ ] /mecab
39.4 MiB [ ] /dpkg
3.8 MiB [ ] /ubuntu-advantage
我试图调查什么东西占用了太多空间。所有数据库都为 WordPress 站点提供服务,因此我无法过多修改架构。
但我检查了数据库大小:
SELECT table_schema AS "Database", SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema;
+--------------------+--------------+
| Database | Size (MB) |
+--------------------+--------------+
| blog1 | 166.00000000 |
| information_schema | 0.00000000 |
| mysql | 2.76562500 |
| performance_schema | 0.00000000 |
| blog2 | 31.79687500 |
| sys | 0.01562500 |
+--------------------+--------------+
6 rows in set (0.35 sec)
它们都是相对较小的数据库。因此,天哪,/var/lib/mysql 为何占用太多空间?
该设置只是一个带有 mysql 的 LEMP 堆栈,没有复制。
编辑1
我查看了一下/var/lib/mysql
,看到很多binlog的记录:
sudo du -sk /var/lib/mysql
53151572 /var/lib/mysql
$ sudo ls -l /var/lib/mysql | wc -l
623
# I susspect 653 lines would be too much if pasted here
$ sudo ls -l /var/lib/mysql | grep binlog | head
-rw-r----- 1 mysql mysql 43170892 Jun 4 00:00 binlog.000233
-rw-r----- 1 mysql mysql 35975939 Jun 5 00:00 binlog.000234
-rw-r----- 1 mysql mysql 36153241 Jun 6 00:00 binlog.000235
-rw-r----- 1 mysql mysql 37148526 Jun 7 00:00 binlog.000236
-rw-r----- 1 mysql mysql 34947871 Jun 8 00:00 binlog.000237
-rw-r----- 1 mysql mysql 34058129 Jun 9 00:00 binlog.000238
-rw-r----- 1 mysql mysql 36786212 Jun 10 00:00 binlog.000239
-rw-r----- 1 mysql mysql 34790230 Jun 11 00:00 binlog.000240
-rw-r----- 1 mysql mysql 37634381 Jun 12 00:00 binlog.000241
-rw-r----- 1 mysql mysql 35801131 Jun 13 00:00 binlog.000242
$ sudo ls -l /var/lib/mysql | grep binlog | wc -l
545
# 545 lines are binlog files therefore I have binlogs
我已经找到了一种缓解这种情况的方法:
$ cat /etc/mysql/mysql.conf.d/rj.cnf
# Added by Rick James
[mysqld]
log_bin = # turn off
binlog_expire_logs_seconds = 86400 # 1 day
max_binlog_size = 1 # 100M
我做了:
mysql> SHOW BINARY LOGS;
PURGE BINARY LOGS BEFORE '2023-07-30 00:00:00';
Query OK, 0 rows affected, 1 warning (0.01 sec)
但文件仍然不断填满,如果我或者以某种方式可以告诉 binlog 存储它 /dev/null/var/lib/mysql
会安全吗?rm -rf