简体   繁体   English

MySQL连接太多

[英]Mysql connections too many

I'm preparing chat system with comet style programming.It delays(sleeps) page for a 60 second and I use mysql queries in this loop and it approximately sends 245.000 queries to mysql. 我正在准备使用彗星风格编程的聊天系统。它延迟60秒的页面(睡眠),并且在此循环中使用mysql查询,它大约向mysql发送245.000个查询。 Mysql status is like below: Mysql的状态如下:

Aborted_connects - 179391
Connections - 15673040
Max_used_connections - 59
Threads_connected - 6

May it be problem that Connections tab is 15 million, but I can't kill them. 可能“连接数”选项卡为1500万可能是个问题,但是我无法杀死它们。 Can I solve this problem adding mysql_close to end of while() ? 我可以解决这个问题,将mysql_close添加到while()末尾吗?

set_time_limit(60);
mysql_connect("...");
while(true){
  usleep(60000);
  clearstatcache();
  mysql_query("...");
  mysql_close();//<= this one
}

There are few things. 没什么

  1. Your code somehow connects to mysql way too much. 您的代码以某种方式过多地连接到mysql。 Debug your code. 调试您的代码。
  2. Try to pool your connections. 尝试合并您的连接。 Read this Connection pooling in PHP 阅读PHP中的连接池

Seeing your code and your problem, we should take into account and differentiate between connection and query . 看到您的代码和问题,我们应该考虑并区分connectionquery

It's useless to close the conection on the while true loop and it's an error. 关闭while true循环上的连接是没有用的,这是一个错误。

Your code should be something like this: 您的代码应如下所示:

mysql_connect("...");
while(true){
  //do stuff like:
  mysql_query("...");
}
//in case of we exit while true...
mysql_close();//<= this one

If we read the mysql_close manual, we can find this: 如果阅读mysql_close手册,我们可以找到以下内容:

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. 通常不需要使用mysql_close(),因为在脚本执行结束时会自动关闭非持久性打开链接。

So, for now, don't take into account when to close the mysql_connection. 因此,目前,不考虑何时关闭mysql_connection。 We should see how the connections are being created and where. 我们应该看到如何创建连接以及在哪里。

I recomend you to check inside your MySQL and debbug it. 我建议您检查一下MySQL并对其进行调试。 You can with this commands: 您可以使用以下命令:

show status where `variable_name` = 'Threads_connected';

With this you can check the current connections. 这样您可以检查当前连接。 Also you could check with: 您也可以检查:

show processlist;

And you will see all the process list. 您将看到所有过程列表。

Maybe with that we can start to debug and find the problem :) 也许我们可以开始调试并找到问题了:)

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

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