MySQL 如何修复:mysql_connect():连接过多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/730953/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:04:39  来源:igfitidea点击:

How to fix: mysql_connect(): Too many connections

mysql

提问by johnnietheblack

I am getting the following error:

我收到以下错误:

mysql_connect(): Too many connections

It has completely shut down my site, which has been running seamlessly for several years.

它完全关闭了我的网站,该网站已经无缝运行了好几年。

Note: I have shared hosting with GoDaddy.

注意:我已与 GoDaddy 共享主机。

How do I fix this?

我该如何解决?

ALSO: is there a way to close all connections and restart when on a shared hosting plan?

另外:有没有办法在共享主机计划中关闭所有连接并重新启动?

采纳答案by Quassnoi

Most probably, you have been a subject of a DDoSattack.

最有可能的是,您已成为DDoS攻击对象。

People on this forumcomplain on exactly same thing with exactly same provider.

本论坛上的人们抱怨与完全相同的提供商完全相同的事情。

The answer is this:

答案是这样的:

VB told me it was a DOS attack - here is their message:

This is not an 'exploit'. This is a DoS attack (Denial of Service). Unfortunately there is nothing we can do about this. DoS attacks can only be fought at the server or router level, and this is the responsibility of your host. Instead of doing this they have decided to take the easy way out and suspend your account.

If you cannot get them to take this seriously, then you should look for another host. Sorry for the bad news.

VB 告诉我这是一次 DOS 攻击 - 这是他们的消息:

这不是“利用”。这是一种 DoS 攻击(拒绝服务)。不幸的是,我们对此无能为力。DoS 攻击只能在服务器或路由器级别进行打击,这是您的主机的责任。他们没有这样做,而是决定采取简单的方法并暂停您的帐户。

如果你不能让他们认真对待这件事,那么你应该寻找另一个主人。关于这些坏消息我很遗憾。

A possible workaround can be this: if your connection fails with mysql_connect(): Too many connections, you don't quit, but instead sleep()for half a second and try to connect again, and exit only when 10attempts fail.

一种可能的解决方法是:如果您的连接失败mysql_connect(): Too many connections,您不会退出,而是sleep()等待半秒钟并再次尝试连接,并仅在10尝试失败时退出。

It's not a solution, it's a workaround.

这不是解决方案,而是一种解决方法。

This of course will delay your page loading, but it's better than an ugly too many connectionsmessage.

这当然会延迟您的页面加载,但总比丑陋的too many connections消息要好。

You also can come with a some kind of a method which tells bots and browsers apart.

您还可以使用某种方法来区分机器人和浏览器。

Like, set a salted SHA1cookie, redirect to the same page and then check that cookie and connect to MySQLonly if the user agent had passed the test.

例如,设置一个加盐SHA1cookie,重定向到同一页面,然后检查该 cookie 并MySQL仅在用户代理通过测试时连接。

回答by christianparpart

This is a Technical Response

这是技术回复

You will get this "too many connections" error upon connecting to MySQL when the MySQL server has reached its software configurable artificial limit of maximum concurrent client connections.

当 MySQL 服务器达到其最大并发客户端连接的软件可配置人为限制时,您将在连接到 MySQL 时收到此“连接过多”错误。

So, the proper way to fix this is:

因此,解决此问题的正确方法是:

  1. Directly connect to the MySQL server, and perform the query: SET GLOBAL max_connections = 1024;to change the connection limit at runtime (no downtime).
  2. Make your change permanent, and edit the /etc/my.cnf (or similar) and add the line max_connections = 1024line within the [mysqld]section; then restart if you couldn't do the live change.
  1. 直接连接MySQL服务器,执行查询:SET GLOBAL max_connections = 1024;在运行时改变连接限制(无停机)。
  2. 使您的更改永久化,并编辑 /etc/my.cnf(或类似文件)并max_connections = 1024在该[mysqld]部分中添加行;如果您无法进行实时更改,则重新启动。

The chosen limit of 1024 was pure subjective; use whatever limit you want to. You can also inspect your current limit via query SHOW VARIABLES LIKE "max_connections";. Keep in mind, that these limits are there for good use, in order to prevent unnecessary overload of your backend database. So always choose wise limits.

选择的 1024 限制纯粹是主观的;使用您想要的任何限制。您还可以通过 query 检查您的当前限制SHOW VARIABLES LIKE "max_connections";。请记住,这些限制是有好处的,以防止后端数据库不必要的过载。所以总是选择明智的限制。

However, for those steps you are required to have direct access to your database MySQL server.

但是,对于这些步骤,您需要直接访问您的数据库 MySQL 服务器。

As you said, you are using GoDaddy (I do not know them that much), you are left out with the option to contact your service provider (i.e. GoDaddy). However, they willsee this in their logs anyway, also.

正如您所说,您正在使用 GoDaddy(我不太了解他们),您无法选择联系您的服务提供商(即 GoDaddy)。但是,无论如何,他们也会在日志中看到这一点。

Possible Root Causes

可能的根本原因

This of course means, that too many clients are attempting to connect to the MySQL server at the same time - as of the per configuration specified artificial software limit.

这当然意味着,太多客户端同时尝试连接到 MySQL 服务器 - 根据每个配置指定的人工软件限制。

回答by CookieOfFortune

Do you close your connection when you're done with them? Are you using some type of connection pooling? Sounds like you're opening connections and not closing them.

当你处理完它们后,你会关闭你的连接吗?您是否使用某种类型的连接池?听起来您正在打开连接而不是关闭它们。

EDIT: Already answered by Quassnoi. In the case it is a DDoS, and you're using shared hosting, you may be left with just contacting your host and working it out with them. Unfortunately this is a risk when you don't have control of your whole system.

编辑:Quassnoi 已经回答了。如果是 DDoS,并且您使用的是共享主机,则可能只需要联系您的主机并与他们一起解决即可。不幸的是,当您无法控制整个系统时,这是一种风险。

回答by Matt

Consider using mysql_pconnect(). Your host may have adding some sort of throttling for connections. Like a maximum of 100 per 20 minutes or something weird.

考虑使用 mysql_pconnect()。您的主机可能为连接添加了某种限制。就像每 20 分钟最多 100 个或奇怪的东西。

回答by TChadwick

Another thing that can cause this error is if the Database has run out of space. I recently had this occur, and the issue wasn't connections, it was Disk space. Hope this helps someone else!

可能导致此错误的另一件事是数据库空间不足。我最近发生了这种情况,问题不是连接,而是磁盘空间。希望这对其他人有帮助!