Zend Framework 应用程序未正确关闭 mysql 连接
我在使用 Zend_Db 的 PDO 适配器管理我的数据库连接时遇到了一个奇怪的问题.突然之间,我不断达到 MySQL 服务器的连接限制.
I'm having a weird issue using Zend_Db's PDO adapter for managing my database connections. All of a sudden, I'm constantly hitting the connection limit on my MySQL server.
无论出于何种原因,我每三四个小时左右就会收到此错误,并且 Apache 会在每个 HTTP 请求中出现此错误:
For whatever reason, every three or four hours or so I end up getting this error, and Apache craps out on every HTTP request with this error:
PHP Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[00000] [1129] Host 'my.internal.mysql.server.DNS.here' 由于许多连接错误而被阻塞;在
PHP Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[00000] [1129] Host 'my.internal.mysql.server.DNS.here' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'' in
奇怪的是,我没有更改任何代码,而且流量实际上比过去有所下降,所以我知道我的应用不应该产生更多并发连接.最奇怪的是,我在将近一周的时间里没有更改任何主要应用程序文件的代码,从那以后一切都运行得很顺利.
The weird thing is, I haven't changed any code and traffic volumes are actually down from what they've been in the past, so I know my app shouldn't be generating more concurrent connections. The weirdest thing is I haven't changed code on any of the main app files in nearly a week and everything has ran swimmingly since.
重新启动 mysql 服务或使用 mysqladmin flush-hosts 刷新连接确实可以解决问题,而且我确信增加 my.cnf 中的最大并发连接数也将有助于缓解其中的一些问题,但似乎有一个应用程序本身创建的连接过多的核心问题.
Rebooting the mysql service or flushing the connections with mysqladmin flush-hosts does do the trick, and I'm sure increasing the max concurrent connections in my.cnf would also help alleviate some of these issues, but there seems to be a core problem with the application itself creating too many connections.
关于如何深入研究和关联导致挂起连接的代码段有什么想法吗?在外部,网络应用的所有不同页面都可以正常加载且速度很快.
Any thoughts on how I can drill down and correlate the piece of code causing the hanging connection? Externally, all of the different pages of the web app load fine and with good speed.
推荐答案
我认为您的脚本之一正在运行超长查询或卡住了?
I think one of your script is running an ultra long query or get stuck ?
最好的解决方案是创建一个日志工具并使用它来了解问题的确切位置.
The best solution would be to create a logging tool and use that to know where exactly the problem is.
Zend_Log 类应该可以帮助您编写日志工具,显然你最好使用文件编写器而不是数据库编写器.由于您的问题与 DB 相关,您应该创建或修改 DB 类以发出一些日志.
The Zend_Log classes should help you to write that logging tool, obviously you have better to user a file writer and not a DB one. Since your problem is DB related you should create or modify the DB class to emit some logs.
我不知道您项目的体积,但它可能会产生一些性能副作用.
I don't know the volumetric of your project but it might have some performance side effects.
也在玩长查询 MySQL 的特性可能会给你一些提示.
Also playing with the long query feature of MySQL would probably give you some hint.
希望您能找到问题所在.
Hope you are going to find where is your problem.
相关文章