<p>I'm using MediaTemple's Grid Server (shared/grid hosting) to run some MySQL/PHP sites I'm writing and noticed that I wasn't closing one of my MySQL connections, which caused my site to error out: </p>

<pre><code>"Too Many Connections"
</code></pre>

<p>I can't log in anywhere to close the connections manually. </p>

<p><strong>Is that any way to close open connections using a script or other type of command?.</strong> </p>

<p>Should I just wait?</p>

[{'answer_id': 69171, 'author': 'owenmarshall', 'author_id': 9806, 'author_profile': 'https://Stackoverflow.com/users/9806', 'pm_score': 1, 'selected': False, 'text': '<p>If you can\'t log into MySQL at all, you will probably have to contact your hosting provider to kill the connections.</p>\n\n<p>If you can use the MySQL shell, you can use the <a href="http://dev.mysql.com/doc/refman/5.0/en/show-processlist.html" rel="nofollow noreferrer">show processlist</a> command to view connections, then use the <a href="http://dev.mysql.com/doc/refman/5.0/en/kill.html" rel="nofollow noreferrer">kill</a> command to remove the connections.</p>\n\n<p>It\'s been my experience that hung SQL connections tend to stay that way, unfortunately.</p>\n'}, {'answer_id': 69179, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 0, 'selected': False, 'text': "<p>Make sure that you're closing the connections with your PHP code.  Also, you could increase the maximum connections allowed in /etc/my.cnf.</p>\n\n<pre><code>max_connections=500\n</code></pre>\n\n<p>Finally, you can login to a mysql prompt and type <code>show status</code> or <code>show processlist</code> to view various statistics with your server.</p>\n\n<p>If all else fails, restarting the server daemon should clear the persistent connections.</p>\n"}, {'answer_id': 69244, 'author': 'Larry OBrien', 'author_id': 10116, 'author_profile': 'https://Stackoverflow.com/users/10116', 'pm_score': 0, 'selected': False, 'text': '<p>Well, if you cannot ever sneak in with a connection, I dunno\', but if you can occasionally sneak in, in Ruby it would be close to:</p>\n\n<pre><code>require \'mysql\'\n\nmysql = Mysql.new(ip, user, pass)\nprocesslist = mysql.query("show full processlist")\nkilled = 0\nprocesslist.each { | process |\n  mysql.query("KILL #{process[0].to_i}")\n} \nputs "#{Time.new} -- killed: #{killed} connections"\n</code></pre>\n'}, {'answer_id': 69376, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 0, 'selected': False, 'text': "<p>If you can access the command line with enough privileges, restart the MySQL server or the Apache (assuming that you use Apache) server - because probably it is keeping the connections open. After you successfully closed the connections, make sure that you are not using persistent connections from PHP (the general opinion seems to be that it doesn't create any significant performance gain, but it has all kinds of problems - like you've experienced - and in some cases - like using it PostgreSQL - it can even significantly slow down your site!).</p>\n"}, {'answer_id': 72386, 'author': 'longneck', 'author_id': 8250, 'author_profile': 'https://Stackoverflow.com/users/8250', 'pm_score': 1, 'selected': False, 'text': "<p>blindly going in an terminating connections is not the way to solve this problem. first you need to understand why you are running out of connections. is your max_connections setting selected to correctly match the number of max/anticipated users? are you using persistent connections when you really don't need them? etc.</p>\n"}]
['https://Stackoverflow.com/questions/69159', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9803/']