精彩推荐

Magento清空数据库log和var缓存文件脚本

1280人阅读  0人回复   查看全部 | 阅读模式 | 复制链接   

1

主题

1

帖子

5

积分

新手上路

Rank: 1

积分
5
发表于 2015-4-23 20:08:26
分享到:
Magento的数据库,用久了,很多log表会变的非常巨大,数据库体积也越来越大,这会严重影响到数据库的运行。

此脚本可以自动清空数据库log表。如果你的log表有用,可以按照实际情况修改此脚本。

将文件上传至你的magento文件根目录,然后用如下地址执行清空命令:

清空数据库log表:http://你网站的网址/clear-magento.php?clean=log

清空magento缓存文件:http://你网站的网址/clear-magento.php?clean=var

如果你的数据库log表比较大,那么使用如上命令清log可能会失败,或者用去很长时间,你可以手动清空,在脚本代码中的log表有:

‘dataflow_batch_export’,
‘dataflow_batch_import’,
‘log_customer’,
‘log_quote’,
‘log_summary’,
‘log_summary_type’,
‘log_url’,
‘log_url_info’,
‘log_visitor’,
‘log_visitor_info’,
‘log_visitor_online’,
‘index_event’,
‘report_event’,
‘report_compared_product_index’,
‘report_viewed_product_index’,
‘catalog_compare_item’,
‘catalogindex_aggregation’,
‘catalogindex_aggregation_tag’,
‘catalogindex_aggregation_to_tag’,
‘catalogsearch_query’,
‘catalogsearch_fulltext’,
‘catalogsearch_result’

清空var缓存文件一般不会花费太多时间,缓存文件在脚本中路径分别为:

‘downloader/.cache/*’,
‘downloader/pearlib/cache/*’,
‘downloader/pearlib/download/*’,
‘var/cache/’,
‘var/locks/’,
‘var/log/’,
‘var/report/’,
‘var/session/’,
‘var/tmp/’

你也可以手动去清除。

脚本内容如下,请自己复制黏贴到 clear-magento.php 文件中:

  1. <?php
  2. $xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);

  3. $db['host'] = $xml->global->resources->default_setup->connection->host;
  4. $db['name'] = $xml->global->resources->default_setup->connection->dbname;
  5. $db['user'] = $xml->global->resources->default_setup->connection->username;
  6. $db['pass'] = $xml->global->resources->default_setup->connection->password;
  7. $db['pref'] = $xml->global->resources->db->table_prefix;

  8. if($_GET['clean'] == 'log') clean_log_tables();
  9. if($_GET['clean'] == 'var') clean_var_directory();

  10. function clean_log_tables() {
  11.     global $db;

  12.     $tables = array(
  13.         'dataflow_batch_export',
  14.         'dataflow_batch_import',
  15.         'log_customer',
  16.         'log_quote',
  17.         'log_summary',
  18.         'log_summary_type',
  19.         'log_url',
  20.         'log_url_info',
  21.         'log_visitor',
  22.         'log_visitor_info',
  23.         'log_visitor_online',
  24.         'index_event',
  25.         'report_event',
  26.         'report_compared_product_index',
  27.         'report_viewed_product_index',
  28.         'catalog_compare_item',
  29.         'catalogindex_aggregation',
  30.         'catalogindex_aggregation_tag',
  31.         'catalogindex_aggregation_to_tag',
  32.         'catalogsearch_query',
  33.         'catalogsearch_fulltext',
  34.         'catalogsearch_result'
  35.     );

  36.     mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
  37.     mysql_select_db($db['name']) or die(mysql_error());

  38.     foreach($tables as $v => $k) {
  39.         @mysql_query('TRUNCATE `'.$db['pref'].$k.'`');
  40.         echo $db['pref'].$k.' cleared<br />';
  41.     }
  42. }

  43. function clean_var_directory() {
  44.     $dirs = array(
  45.         'downloader/.cache/*',
  46.         'downloader/pearlib/cache/*',
  47.         'downloader/pearlib/download/*',
  48.         'var/cache/',
  49.         'var/locks/',
  50.         'var/log/',
  51.         'var/report/',
  52.         'var/session/',
  53.         'var/tmp/'
  54.     );

  55.     foreach($dirs as $v => $k) {
  56.         exec('rm -rf '.$k);
  57.         echo $k.' cleared<br />';
  58.     }
  59. }

复制代码


回复

使用道具 举报

快速回复 返回顶部 返回列表