我正在优化Apache和mod_php的networking服务器上的MySQL,所以我跑了mysqltuner ,结果如下:
-------- General Statistics -------------------------------------------------- [--] Skipped version check for MySQLTuner script [OK] Currently running supported MySQL version 5.1.63-0+squeeze1 [OK] Operating on 64-bit architecture -------- Storage Engine Statistics ------------------------------------------- [--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster [--] Data in MyISAM tables: 255M (Tables: 120) [--] Data in InnoDB tables: 5M (Tables: 2) [!!] Total fragmented tables: 14 -------- Security Recommendations ------------------------------------------- ERROR 1142 (42000) at line 1: SELECT command denied to user 'lovecpokladu'@'localhost' for table 'user' [OK] All database users have passwords assigned -------- Performance Metrics ------------------------------------------------- [--] Up for: 9h 37m 22s (6M q [185.754 qps], 286K conn, TX: 72B, RX: 767M) [--] Reads / Writes: 85% / 15% [--] Total buffers: 58.0M global + 2.7M per thread (151 max threads) [OK] Maximum possible memory usage: 463.8M (11% of installed RAM) [OK] Slow queries: 0% (105/6M) [OK] Highest usage of available connections: 33% (50/151) [OK] Key buffer size / total MyISAM indexes: 16.0M/48.9M [OK] Key buffer hit rate: 99.9% (378M cached / 497K reads) [OK] Query cache efficiency: 83.2% (4M cached / 5M selects) [!!] Query cache prunes per day: 84442 [OK] Sorts requiring temporary tables: 6% (10K temp sorts / 159K sorts) [!!] Joins performed without indexes: 403 [OK] Temporary tables created on disk: 25% (79K on disk / 312K total) [OK] Thread cache hit rate: 99% (936 created / 286K connections) [!!] Table cache hit rate: 0% (64 open / 102K opened) [OK] Open file limit used: 10% (106/1K) [OK] Table locks acquired immediately: 99% (1M immediate / 1M locks) [!!] Connections aborted: 49% [OK] InnoDB data size / buffer pool: 5.6M/8.0M -------- Recommendations ----------------------------------------------------- General recommendations: Run OPTIMIZE TABLE to defragment tables for better performance MySQL started within last 24 hours - recommendations may be inaccurate Enable the slow query log to troubleshoot bad queries Adjust your join queries to always utilize indexes Increase table_cache gradually to avoid file descriptor limits Your applications are not closing MySQL connections properly Variables to adjust: query_cache_size (> 16M) join_buffer_size (> 128.0K, or always use indexes with joins) table_cache (> 64)
我的PDO初始化脚本:
$pdo = new PDO("mysql:host=localhost;dbname=...;charset=utf8", $user, $pass, array("SET NAMES utf8")); $pdo->exec("SET CHARACTER SET utf8"); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
它说[!!] Connections aborted: 49% ,我应该担心吗? 我使用PDO来非持久地连接到PHP的数据库。 连接应该在脚本结束时closures,所以我没有得到如此高的连接终止的原因。 或者即使它真的很重要。
非常感谢!
如果mysql客户端(在这种情况下是您的PHP脚本)在退出之前无法执行mysql_close,MySQL将logging一个中止的连接并增加终止的客户端状态variables。
从MySQL文档: 通信错误和中止连接
如果客户端成功连接,但稍后断开连接或终止连接,则服务器将递增Aborted_clients状态variables,并将中止的连接消息logging到错误日志中。 原因可以是以下任何一种:
The client program did not call mysql_close() before exiting. The client had been sleeping more than wait_timeout or interactive_timeout seconds without issuing any requests to the server. See Section 5.1.4, “Server System Variables”.
我不是一个PHP程序员,但是从系统pipe理员的angular度来看,如果你没有正确地closures你的MySQL连接,你可以在MySQL日志中看到这些消息。
另外从文档: 连接和连接pipe理
成功连接到数据库后,PDO类的一个实例将返回到您的脚本。 该连接在该PDO对象的生命周期中保持活动状态。 要closures连接,您需要通过确保所有剩余的引用被删除来销毁对象 – 您可以通过将NULL分配给保存该对象的variables来执行此操作。 如果你没有明确地做到这一点,PHP将在脚本结束时自动closures连接。
尽快closures连接
在实践中,我build议在完成后立即closuresMySQL连接。
想象一下,在高负荷的情况下,你有:
在这种情况下,如果由于高负载PHP代码执行需要5秒,则MySQL连接保持打开状态。 以千分之一的请求/秒数倍,你很容易耗尽你的MySQL连接。 通过明确closures连接,可以帮助避免这个问题和其他问题。