php、mysql - 数据库连接过多错误
祝大家美好的一天。我有一个奇怪的错误。我创建了一个这样工作的聊天:
- 每 2 秒将问题/答案插入到数据库中,
- 将 ajax 请求发送到获取新问题/答案的 php 脚本,
直到今天我收到此错误时一直工作正常:
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1040] Too many connections' in /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php:129
Stack trace: #0 /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php(129): PDO->__construct('mysql:host=loca...', '', '', Array)
s#1 /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Mysql.php(96): Zend_Db_Adapter_Pdo_Abstract->_connect()
s#2 /var/www/html/dbdev/include/Zend/Db/Adapter/Abstract.php(448): Zend_Db_Adapter_Pdo_Mysql->_connect()
s#3 /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('select profile_...', Array)
s#4 /var/www/html/dbdev/include/Zend/Db/Adapter/Abstract.php(782): Zend_Db_Adapter_Pdo_Abstract->query('select profile_...', Array)
s#5 /var/www/html/dbdev/include/Profile.php(43): Zend_Db_Adapter_Abstract->fetchPairs('select profile_...')
s#6 /var/www/html/dbdev/public_html/index.php(29): Profile->load()
s#7 {main} Next exception 'Zend_Db_Adapter_Exception' with in /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php on line 144
问题是我们没有那么多... (8) 而且我不认为数据库不能同时支持超过 8 到 16 个连接(规范上写的是聊天必须支持 50-100 个用户,所以 8 是.. 。 小的)。
那么...任何人都可以告诉我为什么会发生这种情况(昨天我们 15 岁并且工作正常)以及如何解决它?感谢您的帮助。如果您需要任何代码示例,请询问。
Good day to all. I have an odd error. I have created a chat that works like this:
- questions/answers are inserted into a db
- every 2 seconds an ajax request is sent to a php script that fetch the new questions/answers
It worked fine until today when I got this error:
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1040] Too many connections' in /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php:129
Stack trace: #0 /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php(129): PDO->__construct('mysql:host=loca...', '', '', Array)
s#1 /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Mysql.php(96): Zend_Db_Adapter_Pdo_Abstract->_connect()
s#2 /var/www/html/dbdev/include/Zend/Db/Adapter/Abstract.php(448): Zend_Db_Adapter_Pdo_Mysql->_connect()
s#3 /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('select profile_...', Array)
s#4 /var/www/html/dbdev/include/Zend/Db/Adapter/Abstract.php(782): Zend_Db_Adapter_Pdo_Abstract->query('select profile_...', Array)
s#5 /var/www/html/dbdev/include/Profile.php(43): Zend_Db_Adapter_Abstract->fetchPairs('select profile_...')
s#6 /var/www/html/dbdev/public_html/index.php(29): Profile->load()
s#7 {main} Next exception 'Zend_Db_Adapter_Exception' with in /var/www/html/dbdev/include/Zend/Db/Adapter/Pdo/Abstract.php on line 144
The problem is that we were not that many... (8) and I don't think the db could not support more than 8 to 16 connections simultaneously (on specifications is written that the chat must support 50-100 users so 8 is... small).
So... can any1 tell me why did this happen (yesterday we were 15 and worked fine) and how to solve it? Thank you for your help. If you need any code samples just ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
“连接过多”错误有多种不同的原因。
查看 MySQL.com 上的常见问题解答页面:http://dev.mysql。 com/doc/refman/5.5/en/too-many-connections.html
检查 my.cnf 文件中的“max_connections”。如果不存在,请尝试:
但是默认值为 151,所以应该没问题。
如果您位于共享主机上,则可能是其他用户占用了太多连接。
其他需要注意的问题是持久连接的使用和磁盘空间不足。
There are a bunch of different reasons for the "Too Many Connections" error.
Check out this FAQ page on MySQL.com: http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
Check your my.cnf file for "max_connections". If none exist try:
However the default is 151, so you should be okay.
If you are on a shared host, it might be that other users are taking up too many connections.
Other problems to look out for is the use of persistent connections and running out of diskspace.
错误
SQLSTATE[HY000] [1040] Too Many Connections
是一个SQL错误,与SQL Server有关。可能有其他应用程序连接到服务器。服务器有最大可用连接数。如果您有 phpmyadmin,您可以使用“变量”选项卡来检查设置是什么。
您还可以像这样查询状态表:
或者对此有一些变化。检查手册了解有哪些变量
(请注意,“连接' 不是当前连接,请检查该链接:))
The error
SQLSTATE[HY000] [1040] Too many connections
is an SQL error, and has to do with the sql server. There could be other applications connecting to the server. The server has a maximum available connections number.If you have phpmyadmin, you can use the 'variables' tab to check what the setting is.
You can also query the status table like so:
Or some variance on that. check the manual for what variables there are
(be aware, 'connections' is not the current connections, check that link :) )
如果您达到了 Mac 连接限制
转到
/etc/my.cnf
并在[mysqld]
部分添加max_connections = 500
并重新启动 MySQL。
If you are reaching the mac connection limit
go to
/etc/my.cnf
and under the[mysqld]
section addmax_connections = 500
and restart MySQL.
如果您需要在不重新启动 MySQL 的情况下增加 MySQL 连接,请执行以下操作,如果您不知道配置文件,请使用 mysqlworkbench 或 phpmyadmin 或命令提示符来运行以下查询。
这些设置将在 MySQL 重新启动时更改。
对于永久更改,请在 my.cnf 中添加以下行并重新启动 MySQL。
If you need to increase MySQL Connections without MySQL restart do like below, also if you don't know configuration file, below use the mysqlworkbench or phpmyadmin or command prompt to run below queries.
These settings will change at MySQL Restart.
For permanent changes add below line in my.cnf and restart MySQL.
请检查您是否为每个请求打开了一个新连接(mysql_connect(...))。如果这样做,请确保随后关闭连接(使用 mysql_close($link))。
此外,您应该考虑更改此行为,因为为每个用户保持一个稳定的连接可能是完成任务的更好方法。
如果您还没有,请看一下这个明显但仍然有用的信息资源:http ://php.net/manual/function.mysql-connect.php
Please check if you open up a new connection with each of your requests (mysql_connect(...)). If you do so, make sure you close the connection afterwards (using mysql_close($link)).
Also, you should consider changing this behaviour as keeping one steady connection for each user may be a better way to accomplish your task.
If you didn't already, take a look at this obvious, but nonetheless useful information resource: http://php.net/manual/function.mysql-connect.php
发生这种情况的原因可能是同时连接太多或同时聊天太多。由于会话太多,也可能会发生这种情况。
解决这个问题的最好方法是重新启动MySQL。
或
或
This can happen due to too many connection same time or many chat at same time. Also it can happen due too many session.
The best way to sort out this issue is restart MySQL.
or
or