php mysqli连接过多(HY000/1040)和(08004/1040)有区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21202620/
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
mysqli too many connections (HY000/1040) and (08004/1040) is there a difference?
提问by ahmadMarafa
I'm getting the following errors in my script:
我的脚本中出现以下错误:
mysqli_connect(): (08004/1040): Too many connections
mysqli_connect(): (HY000/1040): Too many connections
What is the difference and how can I solve this problem?
有什么区别,我该如何解决这个问题?
回答by Axel Amthor
"Too many connections"indicates, that your script is opening at least more than one connection to the database. Basically, to one server, only one connection is needed. Getting this error is either a misconfiguration of the server (which I assume isn't the case because max connections = zero isn't an option) or some programming errors in your script.
"Too many connections"表明您的脚本至少打开了多个与数据库的连接。基本上,对于一台服务器,只需要一个连接。得到这个错误要么是服务器配置错误(我认为不是这种情况,因为最大连接数 = 0 不是一个选项)或脚本中的一些编程错误。
Check for re-openings of your database connections (mysqli_connect). There should only be one per script (!) and usually you should take care of reusing open connections OR close them properly after script execution (mysqli_close)
检查您的数据库连接是否重新打开 ( mysqli_connect)。每个脚本应该只有一个 (!),通常您应该注意重用打开的连接或在脚本执行后正确关闭它们 ( mysqli_close)
回答by John
While I could not tell you the difference between the 2 error numbers above, I can tell you what causes this.
虽然我无法告诉您上述 2 个错误编号之间的区别,但我可以告诉您导致这种情况的原因。
Your MySQL database only allows so many connections at the same time. If you connect to MySQL via PHP, then you generally open a new connection every time a page on your site loads. So if you've got too much traffic to your site this can cause this issue.
您的 MySQL 数据库同时只允许这么多连接。如果您通过 PHP 连接到 MySQL,那么您通常会在每次加载站点页面时打开一个新连接。因此,如果您的网站访问量过多,则可能会导致此问题。
I think it is pretty common for people to have one connection to their database per page load, and multiple queries for sure. So really what it comes down to are 3 points:
我认为人们在每个页面加载时与他们的数据库建立一个连接,并且肯定有多个查询是很常见的。所以真正归结为3点:
(Let me just tell you now, persistent connections will not solve your issue.)
(让我现在告诉你,持久连接不会解决你的问题。)
If you have access to your server's CLI/SSH, try to increase the limit by modifying your MySQL configuration (don't forget to restart the service for changes to take affect). This will of course consume more system resources on your database server.
如果您有权访问服务器的 CLI/SSH,请尝试通过修改 MySQL 配置来增加限制(不要忘记重新启动服务以使更改生效)。这当然会在您的数据库服务器上消耗更多的系统资源。
If you have a lot of AJAX requests or other internal database connections you should try to get these down to a single script with a single call. Your site may make multiple AJAX calls to various PHP files that pulls MySQL data, which uses a whole database connection for each one. Instead, create a single PHP file to collect all the data you need on a given page, this script can get all the data you need while only using 1 database connection.
如果您有很多 AJAX 请求或其他内部数据库连接,您应该尝试通过一次调用将它们归结为单个脚本。您的站点可能对各种提取 MySQL 数据的 PHP 文件进行多次 AJAX 调用,每个文件都使用整个数据库连接。相反,创建一个 PHP 文件来收集给定页面上您需要的所有数据,此脚本可以在仅使用 1 个数据库连接的情况下获取您需要的所有数据。
回答by Everdata technologies
Steps to resolve that issue:
解决该问题的步骤:
- Check MySQL connection limit in configuration file or
my.cnf Run below command to check:
mysql -e "show variables like '%connection%';"You will see like this:
max_connections = 500Increase it as per you want:
max_connections = 50000Restart the MySQL service:
$ service MySQL restart
- 检查配置文件中的 MySQL 连接限制或
my.cnf 运行以下命令进行检查:
mysql -e "show variables like '%connection%';"你会看到这样的:
max_connections = 500根据需要增加它:
max_connections = 50000重启 MySQL 服务:
$ service MySQL restart
Now check your website, I hope the error will not occur!
现在检查您的网站,希望不会发生错误!
Thank You!
谢谢你!
回答by fylzero
As far as the difference between the two, I believe that HY000 is a PDO exception where 08004 is actually coming from MySQL. Error 1040 is the code for "Too Many Connections".
至于两者之间的区别,我认为 HY000 是 PDO 异常,其中 08004 实际上来自 MySQL。错误 1040 是“连接过多”的代码。
回答by rubo77
You should also check if your disk is full, this can cause the same error:
您还应该检查您的磁盘是否已满,这可能会导致相同的错误:
df -h
will show you the remaining space on each partition, you probably have to check the root partition /(or /var/ in case you have an extra partition for this):
将显示每个分区上的剩余空间,您可能需要检查根分区/(或 /var/ 以防您有额外的分区):
df -h /

