php SQLSTATE[HY000] [1040] 连接太多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24225164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY000] [1040] Too many connections
提问by developer
Sometimes when I open my error log file I see this this error
有时,当我打开错误日志文件时,会看到此错误
[14-Jun-2014 19:09:55 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1040] Too many connections' in /home/root/products/db.php:2
Stack trace: /home/root/products/db.php(2): PDO->__construct('mysql:host=loca...', 'database', 'password')
/home/root/products/by-brand.php(2): include_once('/home/root/...')
{main} thrown in /home/root/products/db.php on line 2
[14-Jun-2014 19:15:11 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1286 Unknown storage engine InnoDB in /home/root/products/detail.php:8
Stack trace: /home/root/products/name.php(8): PDO->prepare('select * from p...')
{main} thrown in /home/root/products/name.php on line 8
[2014 年 6 月 14 日 19:09:55 UTC] PHP 致命错误:未捕获的异常“PDOException”,消息为“SQLSTATE[HY000] [1040] /home/root/products/db.php:2 中的连接过多”
堆栈跟踪:/home/root/products/db.php(2): PDO->__construct('mysql:host=loca...', 'database', 'password')
/home/root/products/by-brand.php(2): include_once('/home/root/...')
{main} 在第 2 行的 /home/root/products/db.php 中抛出
[2014 年 6 月 14 日 19:15:11 UTC] PHP 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1286 /home/root/products/detail 中的未知存储引擎 InnoDB .php:8
堆栈跟踪:/home/root/products/name.php(8): PDO->prepare('select * from p...')
{main} 在第 8 行的 /home/root/products/name.php 中抛出
I am trying to solve this issue by closing every script by using $db=null;
but it doesn't seem to work.
我试图通过使用关闭每个脚本来解决这个问题,$db=null;
但它似乎不起作用。
I contacted the service provider. They say everything works fine. You need to check errors at the end. I have just a few users.
我联系了服务提供商。他们说一切正常。您需要在最后检查错误。我只有几个用户。
Why do I somestimes get this error?
为什么我有时会收到此错误?
My hosting is unlimited: unlimited disk space. unlimited bandwidth but still I get this error. I don't know how can I solve this issue. I hope to make my website larger but these error make me confuses
我的主机是无限的:无限的磁盘空间。无限带宽,但我仍然收到此错误。我不知道我该如何解决这个问题。我希望让我的网站更大,但这些错误让我感到困惑
My db.php file has this code. Can you check if this fine? Maybe this is causing the problem.
我的 db.php 文件有这个代码。你能检查一下这是否正常?也许这是导致问题的原因。
db.php
数据库文件
$db = new PDO('mysql:host=localhost;dbname=mobiles;charset=utf8', 'root', '**somePassword**');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
回答by fntneves
This is a limit of database's configuration. If you have permission to edit the configuration file of your database service, you can change max_connections
value.
这是数据库配置的限制。如果您有权编辑数据库服务的配置文件,则可以更改max_connections
值。
Take a look: http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
看看:http: //dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
You can try running this SQL query (take care about this value!)
你可以尝试运行这个 SQL 查询(注意这个值!)
SET GLOBAL max_connections = 512;
and this to get current value of all variables:
这是为了获取所有变量的当前值:
SHOW VARIABLES;
or (for specific variable):
或(对于特定变量):
SHOW GLOBAL VARIABLES LIKE '%max_connections%'
By the way, try to set PDO object
to null
when you don't need it. This will close connection to your database and PHP does not wait for script to finish to close active connections. (Oh, you did it, sorry, I will not edit it to delete. Get it as a note)
顺便说一句,请尝试在不需要时设置PDO object
为null
。这将关闭与您的数据库的连接,并且 PHP 不会等待脚本完成关闭活动连接。(哦,你做的,对不起,我不会编辑删除。拿来做笔记)
回答by Your Common Sense
There is nothing wrong with your connection code.
您的连接代码没有任何问题。
On a sharedhosting, "unlimited" means otherusers are unrestricted with their resource using. Means they can eat up whole pool, while your consumption remains modest.
在共享主机上,“无限制”意味着其他用户的资源使用不受限制。意味着它们可以吃光整个游泳池,而您的消费量保持适度。
As you've been told in comments, don't use this host.
正如您在评论中被告知的那样,不要使用此主机。
回答by SteAp
In situations of low memory or few resources, first determine the root cause.
在内存不足或资源很少的情况下,首先确定根本原因。
Does this problem appear in your development system? Or does it appear in your production system?
你的开发系统有没有出现这个问题?或者它是否出现在您的生产系统中?
If seen in the dev system, it is very likely a severe technical error. If seen in the production system, the problem might be due to system limits(not necessarily system overload).
如果在开发系统中看到,很可能是严重的技术错误。如果在生产系统中看到,问题可能是由于系统限制(不一定是系统过载)。
Most server applications allocate a fixed amount of RAM for a fixed number of incoming connections (due to technical advantages).
大多数服务器应用程序为固定数量的传入连接分配固定数量的 RAM(由于技术优势)。
As most other network-server applications, MySQL allocates a fixed number of connectionstoo. Each time a client connects, a slot in the connection pool gets assigned. Each time a client disconnects, a slot gets marked as free.
与大多数其他网络服务器应用程序一样,MySQL 也分配固定数量的连接。每次客户端连接时,都会分配连接池中的一个插槽。每次客户端断开连接时,都会将一个插槽标记为 free。
While you can enlarge the pre-allocated connection pool, do this in small steps. Your system administration (hopefully) has chosen the connection pool's size wisely.
虽然您可以扩大预先分配的连接池,但要分步进行。您的系统管理员(希望如此)明智地选择了连接池的大小。
If a connection pool gets too large, your application - or the whole server - might get non-responsive: Each computing environment can only process a certain number of requests per unit of time. The number depends of the average cost per request / the distribution of cost over requests.
如果连接池过大,您的应用程序或整个服务器可能会停止响应:每个计算环境每单位时间只能处理一定数量的请求。该数量取决于每个请求的平均成本/请求的成本分布。
That said, collect system load and connection amount data over time. Figure out, at which situations the error message gets written.
That said, collect system load and connection amount data over time. Figure out, at which situations the error message gets written.
Once you know, you might change MySQL's max_connectionssetting.
一旦你知道了,你可能会改变 MySQL 的max_connections设置。
PHP and MySQL
PHP 和 MySQL
Assigning $db = null
is fine as explained here.
分配$db = null
是细如这里解释。
Regarding your problem scenario, disk-space and bandwidth most likely aren't relevant.
关于您的问题场景,磁盘空间和带宽很可能不相关。
In general, you might prepare a class conforming to the singleton pattern, which provides the one and only interface talking to your database-server. Other patterns may be applicable too.
通常,您可能会准备一个符合单例模式的类,它提供了与您的数据库服务器通信的唯一接口。其他模式也可能适用。
All other scripts should require_oncethis classe's file and do all DB-related stuff using this class.
所有其他脚本都应该require_once这个 classe 的文件,并使用这个 class 做所有与 DB 相关的事情。
Correctly implemented and called, the singleton class uses a single MySQL connection per requestprocessed.
正确实现和调用,单例类使用单个 MySQL 连接处理每个请求。
The correct place to set the amount of connections is my.cnf
, often found in the /etc/
directory. There, you might set a value like so:
设置连接数量的正确位置是my.cnf
,通常可以在/etc/
目录中找到。在那里,您可以像这样设置一个值:
[mysqld]
set-variable=max_connections=250
In case of trouble, SHOW processlistmight be of help. It provides details of all active connections:
如果遇到麻烦,SHOW processlist可能会有所帮助。它提供了所有活动连接的详细信息:
In a top-level catch
block that handles PDOException
, you may save the outcome of SHOW processlistin a log-file for later analysis. But ensure, that the amount of such log entries is limited per unit of time to not flood your log-file.
在处理 的顶级catch
块中PDOException
,您可以将SHOW processlist的结果保存在日志文件中以供以后分析。但请确保每单位时间限制此类日志条目的数量,以免泛滥您的日志文件。
Note, that MySQL permits one more connection than max_connectionsfor a client connection of a user having SUPER privileges.
请注意,对于具有SUPER 权限的用户的客户端连接,MySQL 允许比max_connections 多一个连接。
You might use such a user to gather the process list information. Or - in case of a PDOException
- you might delay your application for a short period of time and try to connect again and then save the processlist.
您可以使用这样的用户来收集进程列表信息。或者 - 在这种情况下PDOException
- 您可能会延迟您的应用程序一小段时间并尝试再次连接,然后保存进程列表。
Unlimited disk space
无限磁盘空间
Something like unlimited disk space doesn't exist: Due to the simple fact, that the amount of HDDs and SSDs ever produced is a finite number ;-)
不存在无限磁盘空间之类的东西:由于一个简单的事实,曾经生产的 HDD 和 SSD 的数量是有限的;-)
While your provider doesn't set a limit, technical limits exist - which quite likely aren't important in your case.
虽然您的提供商没有设置限制,但存在技术限制——这对您来说很可能并不重要。
Practically, there's another limit:Write and run an application which allocates more and more disk space. Within a short amount of time, your provider will turn down your contract...
实际上,还有另一个限制:编写和运行分配越来越多磁盘空间的应用程序。在很短的时间内,您的提供商将拒绝您的合同......