資料庫伺服器刪除巨大.ibd表空間文件(數十GB),直接rm會觸發內核同步刪除所有數據塊,導致I/O風暴、系統假死、業務停頓。
ln huge.ibd huge.ibd.hardlink,inode引用計數+1。rm huge.ibd,引用計數-1,但inode與數據塊保留(硬鏈接仍持有),瞬間完成,零I/O壓力。huge.ibd.hardlink反覆truncate -s -1G逐步縮小,每次釋放1GB數據塊,I/O平滑分攤。rm huge.ibd.hardlink,最後一塊釋放完成。
truncate -s -1G huge.ibd.hardlink # 縮小 1GB
truncate -s 0 huge.ibd.hardlink # 直接歸零(等同刪除所有數據塊,慎用)
# 單位後綴:K=1024, M=1024^2, G=1024^3, T, P, E, Z
# 符號:+擴展 -縮小 <至多 >至少 /向下捨入倍數 %向上捨入倍數
touch abc
truncate -s +1K abc
truncate -s +1K abc
ls -l abc
# -rw-r--r-- 1 root root 2048 Nov 28 05:31 abc
fallocate(FALLOC_FL_PUNCH_HOLE)或truncate(ext4/xfs/btrfs均支援)。lsof | grep huge.ibd確認)。ionice -c 3或cgroups blkio.weight限制I/O優先級,進一步降低業務影響。ibdata1不可用此法,僅適用innodb_file_per_table=ON的獨立.ibd文件。truncate屬於GNU coreutils 7.5+,主流發行版(CentOS 6+/Debian 7+/Ubuntu 12.04+)均內建。