简体   繁体   English

mySQL 错误 1040:连接过多

[英]mySQL Error 1040: Too Many Connection

How to fix these, "SQL Error 1040: Too Many Connection" even I try to put如何解决这些问题,“SQL 错误 1040:连接太多”,即使我尝试输入

max_user_connection=500

still "Too many connection"仍然“连接太多”

MySQL: ERROR 1040: Too many connections MySQL:错误 1040:连接太多

This basically tells that MySQL handles the maximum number of connections simultaneously and by default it handles 100 connections simultaneously.这基本上告诉 MySQL 同时处理最大数量的连接,默认情况下它同时处理100 个连接。

These following reasons cause MySQL to run out connections.以下这些原因导致 MySQL 耗尽连接。

  1. Slow Queries慢查询

  2. Data Storage Techniques数据存储技术

  3. Bad MySQL configuration错误的 MySQL 配置

I was able to overcome this issues by doing the followings.通过执行以下操作,我能够克服这个问题。

Open MySQL command line tool and type,打开MySQL command line tool并输入,

show variables like "max_connections";

This will return you something like this.这会给你返回这样的东西。

+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 100   |
+-----------------+-------+

You can change the setting to eg 200 by issuing the following command without having to restart the MySQL server.您可以通过发出以下命令将设置更改为例如 200,而无需重新启动 MySQL 服务器。

set global max_connections = 200;

Now when you restart MySQL the next time it will use this setting instead of the default.现在,当您下次重新启动 MySQL 时,它将使用此设置而不是默认设置。

Keep in mind that increase of the number of connections will increase the amount of RAM required for MySQL to run.请记住,连接数的增加将增加 MySQL 运行所需的 RAM 量。

It is worth knowing that if you run out of usable disc space on your server partition or drive, that this will also cause MySQL to return this error.值得知道的是,如果您的服务器分区或驱动器上的可用磁盘空间不足,这也会导致 MySQL 返回此错误。 If you're sure it's not the actual number of users connected then the next step is to check that you have free space on your MySQL server drive/partition.如果您确定这不是实际连接的用户数,那么下一步是检查您的 MySQL 服务器驱动器/分区上是否有可用空间。

Edit January 2019: 2019 年 1 月编辑:
This is true of MySQL 5.7 and 5.8 . MySQL 5.7 和 5.8 也是如此。 I do not know for versions above this.我不知道上面的版本。

If you are running out of connections like this, chances are excellent that you are not closing the connections that you have open.如果您用完了这样的连接,则很有可能您没有关闭已打开的连接。

Review code that opens connections and ensure the connections are closed as soon as practical.查看打开连接的代码并确保尽快关闭连接。 Typically you want to make use of the using keyword around anything that implements IDisposable (including database connections) to ensure that such objects are disposed as soon as they leave the scope where they are needed.通常,您希望在实现IDisposable的任何对象(包括数据库连接)周围使用 using关键字,以确保这些对象在离开需要它们的范围后立即被释放。

You can check the current number of active connections with this query:您可以使用此查询检查当前的活动连接数:

show processlist

Reference: MySQL show status - active or total connections?参考: MySQL 显示状态 - 活动或总连接数?

This error occurs, due to connection limit reaches the maximum limit, defined in the configuration file my.cnf .发生此错误是由于连接限制达到配置文件my.cnf中定义的最大限制。

In order to fix this error, login to MySQL as root user (Note: you can login as root, since, mysql will pre-allocate additional one connection for root user ) and increase the max_connections variable by using the following command:为了修复这个错误,以root用户登录MySQL(注意:你可以以root用户登录,因为mysql会为root user预先分配一个额外的连接)并使用以下命令增加max_connections变量:

SET GLOBAL max_connections = 500;

This change will be there, until next server restart.此更改将一直存在,直到下一次服务器重新启动。 In order to make this change permanent, you have to modify in your configuration file.为了使此更改永久生效,您必须在配置文件中进行修改。 You can do this by,你可以这样做,

vi /etc/my.cnf

[mysqld]
max_connections = 500

This article has detailed step by step workaround to fix this error . 本文详细介绍了解决此错误的分步解决方法 Have a look at it, I hope it may help you.看看它,我希望它可以帮助你。 Thanks.谢谢。

You have to change max_connections to increase total permitted connections.您必须更改max_connections以增加允许的连接总数。

And set max_user_connections back to default 0 => no limit unless you need to limit this per user connections.并将max_user_connections设置回默认值 0 =>无限制,除非您需要限制每个用户的连接。

MySQL Too many connections MySQL 连接太多

In your program you would have opened the connections and left open without closing it.在您的程序中,您将打开连接并保持打开状态而不关闭它。

I faced a same problem and finally identified.我遇到了同样的问题,终于确定了。

I added this code, after try catch.我在尝试捕获后添加了此代码。

finally{
    try {
        rs.close();
        p.close();
        conn.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

To see how many connections are configured for your DB to use:要查看为您的数据库配置了多少个连接以供使用:

select @@max_connections;

To change it:要更改它:

set global max_connections = 200;

To see how many are connected at the current time:要查看当前连接的数量:

show processlist;

I also wrote this piece of software to help me create a nice spreadsheet of transactions over time so I can track down which queries are the problem:我还编写了这个软件来帮助我创建一个很好的交易电子表格,这样我就可以追踪哪些查询是问题所在: 在此处输入图像描述

Try this :尝试这个 :

open the terminal and type this command : sudo gedit /etc/mysql/my.cnf打开终端并输入以下命令: sudo gedit /etc/mysql/my.cnf

Paste the line in my.cnf file: set-variable=max_connections=500将行粘贴到 my.cnf 文件中: set-variable=max_connections=500

This issue occurs mostly when the maximum allowed concurrent connections to MySQL has exceeded.此问题主要发生在超过 MySQL 允许的最大并发连接数时。 The max connections allowed is stored in the gloobal variable max_connections.允许的最大连接数存储在全局变量 max_connections 中。 You can check it by show global variables like max_connections;您可以通过show global variables like max_connections; in MySQL在 MySQL 中

You can fix it by the following steps:您可以通过以下步骤修复它:

Step1:步骤1:

Login to MySQL and run this command: SET GLOBAL max_connections = 100;登录 MySQL 并运行以下命令: SET GLOBAL max_connections = 100;

Now login to MySQL, the too many connection error fixed.现在登录到 MySQL,连接太多错误已修复。 This method does not require server restart.此方法不需要重新启动服务器。

Step2:第2步:

Using the above step1 you can resolve ths issue but max_connections will roll back to its default value when mysql is restarted.使用上面的 step1 你可以解决这个问题,但是当 mysql 重新启动时 max_connections 将回滚到它的默认值。

In order to make the max_connection value permanent, update the my.cnf file.为了使 max_connection 值永久化,请更新 my.cnf 文件。

Stop the MySQL server: Service mysql stop停止 MySQL 服务器: Service mysql stop

Edit the configuration file my.cnf: vi /etc/mysql/my.cnf编辑配置文件my.cnf: vi /etc/mysql/my.cnf /etc/mysql/my.cnf

Find the variable max_connections under mysqld section.在 mysqld 部分下找到变量 max_connections。

[mysql]

max_connections = 300

Set into higher value and save the file.设置为更高的值并保存文件。

Start the server: Service mysql start启动服务器: Service mysql start

Note: Before increasing the max_connections variable value, make sure that, the server has adequate memory for new requests and connections.注意:在增加 max_connections 变量值之前,请确保服务器有足够的内存用于新的请求和连接。

MySQL pre-allocate memory for each connections and de-allocate only when the connection get closed. MySQL 为每个连接预分配内存并仅在连接关闭时取消分配。 When new connections are querying, system should have enough resources such memory, network and computation power to satisfy the user requests.当新的连接查询时,系统应该有足够的资源,如内存、网络和计算能力来满足用户的请求。

Also, you should consider increasing the open tables limit in MySQL server to accommodate the additional request.此外,您应该考虑增加 MySQL 服务器中的打开表限制以适应额外的请求。 And finally.最后。 it is very important to close the connections which are completed transaction on the server.关闭服务器上已完成事务的连接非常重要。

Check if your current running application is still accessing your mysql and has consumed all the DB connections.检查您当前正在运行的应用程序是否仍在访问您的 mysql 并已使用所有数据库连接。

So if you try to access mysql from workbench , you will get "too many connections" error.因此,如果您尝试从 workbench 访问 mysql ,您将收到“连接过多”错误。

stop your current running web application which is holding all those db connections and then your issue will be solved.停止当前正在运行的 Web 应用程序,该应用程序持有所有这些数据库连接,然后您的问题将得到解决。

The issue noted clearly in https://dev.mysql.com/doc/refman/8.0/en/too-many-connections.html :该问题在https://dev.mysql.com/doc/refman/8.0/en/too-many-connections.html中明确指出:

If clients encounter Too many connections errors when attempting to connect to the MySQL server, all available connections are in use by other clients.如果客户端在尝试连接 MySQL 服务器时遇到 Too many connections 错误,则所有可用连接都被其他客户端使用。

I got resolved the issue after a few minutes, it seems that connection was been released by other clients, also I tried with restarting vs and workbench at same time.几分钟后我解决了这个问题,似乎连接已被其他客户端释放,我也尝试同时重新启动 vs 和工作台。

I was getting this error even though I had all my Connections wrapped in using statements.即使我的所有连接都包含在 using 语句中,我也收到了这个错误。 The thing I was overlooking is how long those connections were staying open.我忽略的是这些连接保持开放的时间。

I was doing something like this:我正在做这样的事情:

using (MySqlConnection conn = new MySqlConnection(ConnectionString))
{
    conn.Open();
    foreach(var m in conn.Query<model>(sql))
    {
        // do something that takes a while, accesses disk or even open up other connections
    }
}

Remember that connection will not close until everything in the loop is done and if the loop creates more connections, they can really start adding up.请记住,在循环中的所有内容完成之前,连接不会关闭,如果循环创建更多连接,它们就可以真正开始加起来。

This is better:这个更好:

List<model> models = null;
using (MySqlConnection conn = new MySqlConnection(ConnectionString))
{
    conn.Open();

    models = conn.Query<model>(sql).ToList(); // to list is needed here since once the connection is closed you can't step through an IEnumerable   
}

foreach(var m in models)
{
    // do something that takes a while, accesses disk or even open up other connections
}

That way your connection is allowed to close and is released back to the thread pool before you go to do other things这样你的连接就可以关闭并在你去做其他事情之前被释放回线程池

How to fix these, "SQL Error 1040: Too Many Connection" even I try to put如何解决这些问题,即使我尝试将“ SQL错误1040:连接过多”

max_user_connection=500

still "Too many connection"仍然是“连接太多”

通过重新启动数据库 Windows 服务解决了问题

Latest Solution最新解决方案

If you are using windows go to the following path :如果您使用的是 Windows,请转到以下路径:

‪C:\xampp\mysql\bin\my.ini

And add the below under the [mysqld] section :并在[mysqld]部分下添加以下内容:

max_connections = 500

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM