简体   繁体   English

PHP + MySQL'数据库连接'减慢服务器速度

[英]PHP + MySQL 'DB connections' slowing server down

I have an ajax-loaded script which runs 2 mysql statements for every single visitor (~100 per second). 我有一个加载了ajax的脚本,它为每个访问者运行2个mysql语句(每秒约100个)。 It's a simple SELECT as well as UPDATE statement. 这是一个简单的SELECTUPDATE语句。

For my backend, I'm using AWS RDS. 对于我的后端,我正在使用AWS RDS。

I noticed my site went down today, I see the logs filled with the following... 我注意到我的网站今天关闭了,我看到日志填满了以下内容......

PHP-FPM-5.5 Error log: PHP-FPM-5.5错误日志:

[11-Jun-2014 17:18:47] WARNING: [pool www] child 12203, script '/var/www/track.php' (request: "GET /track.php") executing too slow (5.282239 sec), logging

Slow log 慢日志

script_filename = /var/www/track.php. [0x00007fcf11fda8f8] execute() /var/www/html/track.php:53

In RDS, my graphs looked like this: 在RDS中,我的图表看起来像这样:

在此输入图像描述

在此输入图像描述

You can see the DB connections became very high. 您可以看到数据库连接变得非常高。

At this point is where I'm a bit confused what to do to prevent this. 在这一点上,我有点困惑该怎么做以防止这种情况。

I can only assume something 'backed up', maybe the database server took just a fraction of a second too long to respond? 我只能假设一些“备份”的东西,也许数据库服务器只花了不到一秒的时间才能响应? Then PHP basically got overloaded because the requests were coming in too fast (too busy 'waiting'), which in turn started to take down my server. 然后PHP基本上超载了,因为请求进入太快(太忙'等待'),这反过来开始取消我的服务器。

The MySQL statements I have are using PHP-PDO . 我的MySQL语句正在使用PHP-PDO I attempted adding PDO::ATTR_TIMEOUT => 1 to my new PDO object, but I have no idea if that would even solve it (or even if it's working or not...). 我尝试将PDO::ATTR_TIMEOUT => 1到我的new PDO对象中,但我不知道是否会解决它(或者即使它正在工作或不工作......)。

My goal is to NOT have my server get taken down just because the DB server is slow or unresponsive. 我的目标是不让我的服务器因为数据库服务器缓慢或无响应而被取消。 I need PHP to be 'smart enough' to not wait for a really long time or get overloaded because MySQL isn't responding fast enough. 我需要PHP“足够聪明”,不要等待很长时间或者因为MySQL没有足够快的响应而过载。

All this is pure conjecture though, maybe it wasnt RDS at all, and somehow PHP got backed up? 所有这些都是纯粹的猜想,也许它根本就不是RDS,而且PHP得到了备份? I'm not exactly sure... 我不太确定......

Thanks! 谢谢!

I'm using MySQL in my most recent project but can't deny the fact that MySQL may have some performance issues. 我在最近的项目中使用MySQL,但不能否认MySQL可能存在一些性能问题。

You could try to send a select request to your database and if it returns true you can then update the row. 您可以尝试向数据库发送选择请求,如果返回true,则可以更新该行。 In this case you'll need two connections per user and per page load. 在这种情况下,每个用户和每页加载需要两个连接。 So I will recommend that you use stored procedures which will only require one connection from your script. 因此,我建议您使用只需要脚本连接的存储过程。 You could half your connection times with using stored procedures in your scenario. 在场景中使用存储过程可以将连接时间缩短一半。

My second solution is to configure your my.conf according to your required settings. 我的第二个解决方案是根据您所需的设置配置my.conf。 Here is an online tool to help make a suitable configuration https://tools.percona.com/wizard . 这是一个在线工具,可帮助您进行合适的配置https://tools.percona.com/wizard

MySQL can use up to 40% of system resources for logging so try to limit some logs such as access log, error log, and any additional logs you may be using. MySQL可以使用高达40%的系统资源进行日志记录,因此请尝试限制某些日志,例如访问日志,错误日志以及您可能正在使用的任何其他日志。

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

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