1

我正在使用 MediaTemple 的网格服务器(共享/网格托管)来运行我正在编写的一些 MySQL/PHP 站点,并注意到我没有关闭我的一个 MySQL 连接,这导致我的站点出错:

"Too Many Connections"

我无法在任何地方登录以手动关闭连接。

这是使用脚本或其他类型的命令关闭打开连接的任何方式吗?

我应该等吗?

4

5 回答 5

1

如果您根本无法登录 MySQL,您可能必须联系您的托管服务提供商以终止连接。

如果可以使用 MySQL shell,可以使用show processlist命令查看连接,然后使用kill命令删除连接。

不幸的是,根据我的经验,挂起的 SQL 连接往往会保持这种状态。

于 2008-09-16T03:29:45.070 回答
1

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.

于 2008-09-16T13:47:28.817 回答
0

确保您正在关闭与您的 PHP 代码的连接。此外,您可以增加 /etc/my.cnf 中允许的最大连接数。

max_connections=500

最后,您可以登录到 mysql 提示符并键入show statusshow processlist查看服务器的各种统计信息。

如果一切都失败了,重新启动服务器守护程序应该清除持久连接。

于 2008-09-16T03:32:17.123 回答
0

好吧,如果您永远无法通过连接潜入,我不知道',但如果您偶尔可以潜入,在 Ruby 中它将接近:

require 'mysql'

mysql = Mysql.new(ip, user, pass)
processlist = mysql.query("show full processlist")
killed = 0
processlist.each { | process |
  mysql.query("KILL #{process[0].to_i}")
} 
puts "#{Time.new} -- killed: #{killed} connections"
于 2008-09-16T03:50:09.453 回答
0

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!).

于 2008-09-16T04:26:06.950 回答