简体   繁体   English

MySQL突然连接过多

[英]MySQL Suddenly Too Many Connection

Hi I a little bit confuse to define my question title, but here is my condition and problem: 嗨,我有点困惑定义我的问题标题,但这是我的条件和问题:

I have a cloud app created by PHP and MySQL (I use Google Cloud SQL) 我有一个由PHP和MySQL创建的云应用(我使用Google Cloud SQL)

My database is actively get thousand of new data every second (contain a lot of my user location and activities) 我的数据库每秒都在主动获取数千个新数据(包含很多用户位置和活动)

I create a PHP program to extract the insight from these data with query like: 我创建了一个PHP程序,使用以下查询从这些数据中提取见解:

SELECT username,time_of_activities FROM (table_name) WHERE latitude>=somevalue AND longitude>=somevalue

For every request I have several queries requested in sequential where each query will process million of rows data in my database. 对于每个请求,我依次请求多个查询,每个查询将处理数据库中的数百万行数据。 And the PHP program will show several insight information related with the user activities. PHP程序将显示一些与用户活动有关的洞察信息。

My problem is: Some times the PHP program failed to do the request. 我的问题是:有时PHP程序无法执行请求。 When the request is failed I check in CloudSQL active connection, the active connection suddenly increase 4000% just in several seconds. 当请求失败时,我签入CloudSQL活动连接,活动连接在几秒钟内突然增加了4000%。

活动连接的屏幕截图

This happen once the PHP request is failed. 一旦PHP请求失败,就会发生这种情况。 Once this happen the database will always return "Too many connection" error and I need to manually restart the CloudSQL instance. 一旦发生这种情况,数据库将始终返回“ Too many connection”错误,并且我需要手动重新启动CloudSQL实例。

Sometimes this PHP program will successfully run, but sometimes not and give the problem like this. 有时,该PHP程序将成功运行,但有时却无法运行,并出现类似问题。

So my question is: what is wrong? 所以我的问题是:怎么了?

NB. 注意 I have putted the mysql_close function to make sure the connection is closed each time the PHP successfully executed the queries. 我放置了mysql_close函数,以确保每次PHP成功执行查询时都关闭连接。

UPDATE QUESTION: I try to check the connection detail in my database and I found there are a lot of "Waiting for Insert" that happen because the database constantly get new data with insert query every second when at the same time I try to select the data. 更新问题:我尝试检查数据库中的连接详细信息,我发现发生了很多“等待插入”的情况,因为当我尝试选择数据。

Is this what caused my error? 这是引起我错误的原因吗? If yes how can the active connection suddenly 4000% times bigger while at normal condition I just have active 4-6 connections even though I constantly do the insert process to the database? 如果是的话,即使我不断地对数据库进行插入过程,在正常情况下,活动连接却突然增加4000%倍,而我却只有4-6个活动连接?

Thanks 谢谢

Did you put the mysql_close inside a finally statement? 您是否将mysql_close放在finally语句中?

http://php.net/manual/en/language.exceptions.php http://php.net/manual/zh/language.exceptions.php

If you don't put it inside a finally , it may not be executed if there is an error inside the program 如果您不将其放入finally ,则如果程序内部存在错误,则可能无法执行它

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

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