Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix: mysql_connect(): Too many connections

Tags:

mysql

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.

How do I fix this?

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

like image 702
johnnietheblack Avatar asked Apr 08 '09 17:04

johnnietheblack


People also ask

How do I fix too many connections in postgresql?

As the same user or as superuser you can cancel all (other) connections of a user: SELECT pg_cancel_backend(pid) -- (SIGINT) -- pg_terminate_backend(pid) -- the less patient alternative (SIGTERM) FROM pg_stat_activity WHERE usename = 'user_name' AND pid <> pg_backend_pid(); Better be sure it's ok to do so.

How do I avoid too many connections error in MySQL?

If clients encounter Too many connections errors when attempting to connect to the mysqld server, all available connections are in use by other clients. The permitted number of connections is controlled by the max_connections system variable. To support more connections, set max_connections to a larger value.

What causes MySQL too many connections?

The MySQL “Too many connections” error occurs when more queries are sent to a MySQL database than can be processed. The error can be fixed by setting a new number of maximum connections in the configuration file or globally.


1 Answers

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.

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 = 1024 line within the [mysqld] section; then restart if you couldn't do the live change.

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.

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

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 will see this in their logs anyway, also.

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.

like image 127
christianparpart Avatar answered Sep 18 '22 10:09

christianparpart