如果你有多个博客 或者网站 定期清理数据库就瘦瘦身就变得非常有必要. 像我的VPS到现在一共运行了6个网站 其中有4个是 wordpress 博客 那么经常维护网站的任务之一就是清理一些垃圾数据.
先写一个PHP函数 用于执行清理SQL的.
1 2 3 4 5 6 7 8 | set_time_limit(600); mysql_connect("localhost", "user", "password") or die(mysql_error()); function run_query($query) { echo $query; mysql_query($query) or die(mysql_error()); echo " ***OK!*** \n"; } |
set_time_limit(600); mysql_connect("localhost", "user", "password") or die(mysql_error()); function run_query($query) { echo $query; mysql_query($query) or die(mysql_error()); echo " ***OK!*** \n"; }
set_time_limit(600) 用于防止脚本执行时间太长, 特别是你放在后台定期执行时 (crontab) 很必要加个限制 一但脚本卡住了 就得强行杀进程.
需要把数据库的访问信息先更改了, 这个函数会把 SQL打印出来并执行 如果碰到任何错误 脚本就会退出.
接下来就是执行一些清理命令了.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | function clean_wp($db_name, $table_prefix, $skipwp = false) { mysql_select_db($db_name); echo "清理 $db_name... \n"; if (!$skipwp) { $query = " DELETE `a`, `b`, `c` FROM `{$table_prefix}terms` AS `a` LEFT JOIN `{$table_prefix}term_taxonomy` AS `c` ON `a`.`term_id` = `c`.`term_id` LEFT JOIN `{$table_prefix}term_relationships` AS `b` ON `b`.`term_taxonomy_id` = `c`.`term_taxonomy_id` WHERE `c`.`taxonomy` = 'post_tag' AND `c`.`count` = 0"; run_query($query); $query = " DELETE `a`, `b`, `c` FROM `{$table_prefix}posts` as `a` LEFT JOIN `{$table_prefix}term_relationships` as `b` ON ( `a` . `ID` = `b` . `object_id` ) LEFT JOIN `{$table_prefix}postmeta` as `c` ON ( `a` . `ID` = `c` . `post_id` ) WHERE `a` . `post_type` = 'revision' "; run_query($query); $query = " DELETE FROM `{$table_prefix}commentmeta` WHERE `comment_id` NOT IN ( SELECT `comment_id` FROM `{$table_prefix}comments` ) "; run_query($query); $query = " DELETE FROM `{$table_prefix}commentmeta` WHERE `meta_key` LIKE \"%akismet%\" "; run_query($query); $query = " DELETE FROM `{$table_prefix}comments` WHERE `comment_approved` <> 1 "; run_query($query); $query = " DELETE FROM `{$table_prefix}posts` WHERE `post_type` = \"revision\" "; run_query($query); } $alletabellen = mysql_query("SHOW TABLES"); while($tabel = mysql_fetch_assoc($alletabellen)) { foreach ($tabel as $db => $tabelnaam) { $query = "REPAIR TABLE `$tabelnaam`"; run_query($query); $query = "OPTIMIZE TABLE `$tabelnaam`"; run_query($query); } } echo " --- 完成!!! ---"; } |
function clean_wp($db_name, $table_prefix, $skipwp = false) { mysql_select_db($db_name); echo "清理 $db_name... \n"; if (!$skipwp) { $query = " DELETE `a`, `b`, `c` FROM `{$table_prefix}terms` AS `a` LEFT JOIN `{$table_prefix}term_taxonomy` AS `c` ON `a`.`term_id` = `c`.`term_id` LEFT JOIN `{$table_prefix}term_relationships` AS `b` ON `b`.`term_taxonomy_id` = `c`.`term_taxonomy_id` WHERE `c`.`taxonomy` = 'post_tag' AND `c`.`count` = 0"; run_query($query); $query = " DELETE `a`, `b`, `c` FROM `{$table_prefix}posts` as `a` LEFT JOIN `{$table_prefix}term_relationships` as `b` ON ( `a` . `ID` = `b` . `object_id` ) LEFT JOIN `{$table_prefix}postmeta` as `c` ON ( `a` . `ID` = `c` . `post_id` ) WHERE `a` . `post_type` = 'revision' "; run_query($query); $query = " DELETE FROM `{$table_prefix}commentmeta` WHERE `comment_id` NOT IN ( SELECT `comment_id` FROM `{$table_prefix}comments` ) "; run_query($query); $query = " DELETE FROM `{$table_prefix}commentmeta` WHERE `meta_key` LIKE \"%akismet%\" "; run_query($query); $query = " DELETE FROM `{$table_prefix}comments` WHERE `comment_approved` <> 1 "; run_query($query); $query = " DELETE FROM `{$table_prefix}posts` WHERE `post_type` = \"revision\" "; run_query($query); } $alletabellen = mysql_query("SHOW TABLES"); while($tabel = mysql_fetch_assoc($alletabellen)) { foreach ($tabel as $db => $tabelnaam) { $query = "REPAIR TABLE `$tabelnaam`"; run_query($query); $query = "OPTIMIZE TABLE `$tabelnaam`"; run_query($query); } } echo " --- 完成!!! ---"; }
函数 clean_wp 有三个参数, 第一个是表名 所以方便清理各个博客网站, 第二个WORDPRESS表的前缀 比如默认安装的是 wp_ 第三个是可选的 默认认为在这个表里有 wordpress 数据库 如果没有 就跳过相关的清理工作.
清理的内容包括垃圾评论, 历史版本, 未被审核的评论(小心使用). 通用的清理 为 修复 REPAIR TABLE 和优化 OPTIMIZE TABLE.
英文: 快速清理多个网站的数据库
GD Star Rating
loading...
本文一共 331 个汉字, 你数一下对不对.loading...
上一篇: Linux 下的彩蛋 - apt-get moo
下一篇: 怎么选择 crontab 的编辑器?
扫描二维码,分享本文到微信朋友圈
