因为一个收集集团机器的历史性能的数据库的一个表涨到2.2 T,而磁盘空间总共2.6 T,和开发讨论之后决定废弃该表,对数据进行清理。问题是如何在生产环境下删除2.2 T 的文件并不能影响数据库服务器的IO性能?
这里给大家介绍一个使用硬链接和truncate 工具处理删除大数据文件的方法:
1 创建tmp 表并 rename 表
mysql> rename table ep to ep_bak;
Query OK, 0 rows affected (0.07 sec)
mysql> rename table ep_tmp to ep;
Query OK, 0 rows affected (0.01 sec)
2 对bak表创建硬链接
ln ep_bak.ibd ep_bak.ibd.hdlk
root@rac1 # ll ep_bak.*
-rw-rw---- 1 mysql mysql 9023 Jul 28 2011 ep_bak.frm
-rw-rw---- 2 mysql mysql 2356792000512 Nov 30 18:05 ep_bak.ibd
-rw-rw---- 2 mysql mysql 2356792000512 Nov 30 18:05 ep_bak.ibd.hdlk
3 删除表 ep_bak
drop table ep_bak;
注意 表的大小为2194G,直接rm 必然会对数据库服务器的IO性能造成压力,因此我们使用coreutils 工具集中的truncate 对大文件进行shrink,脚本内容如下:
#!/bin/bash
TRUNCATE=/usr/local/bin/truncate
for i in `seq 2194 -10 10 `; #从2194 开始每次递减10 ,输出结果见下面
do
sleep 2
echo "$TRUNCATE -s ${i}G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk "
$TRUNCATE -s ${i}G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
done
使用truncate 工具删除数据时服务器IO负载,并未对服务器造成影响
root@rac1 # more truncate_ep.1130.log
/usr/local/bin/truncate -s 2194G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2184G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2174G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2164G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2154G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2144G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2134G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2124G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2114G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2104G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
/usr/local/bin/truncate -s 2094G /home/mysql/data3306/mysql/monitor/ep_bak.ibd.hdlk
.......
如何使用 coreutils ?
#tar -zxvf coreutils-8.9.tar.gz
#cd coreutils-8.9
#./configure
#make