简体   繁体   English

mySQL“Too many connections”错误受到杂种实​​例数量的影响?

[英]mySQL “Too many connections” error influenced by number of mongrel instances?

Recently I have started getting mySQL "too many connection" errors at times of high traffic. 最近我开始在高流量时获得mySQL“太多连接”错误。 My rails app runs on a mongrel cluster with 2 instances on a shared host. 我的rails应用程序在共享主机上有2个实例的mongrel集群上运行。 Some recent changes that might be driving it: 最近的一些变化可能会推动它:

  • Traffic to my site has increased. 我网站的流量增加了。 I am now averaging about 4K pages a day. 我现在每天平均大约4K页。
  • Database size has increased. 数据库大小增加了。 My largest table has ~ 100K rows. 我最大的表有~100K行。 Some associations could return several hundred instances in the worst case, though most are far less. 在最坏的情况下,一些协会可能会返回数百个实例,尽管大多数情况都要少得多。
  • I have added some features that increased the number and size of database calls in some actions. 我添加了一些功能,在某些操作中增加了数据库调用的数量和大小。

I have done a code review to reduce database calls, optimize SQL queries, add missing indexes, and use :include for eager loading. 我已经完成了代码审查,以减少数据库调用,优化SQL查询,添加缺少的索引,并使用:include进行预先加载。 However, many of my methods still make 5-10 separate SQL calls. 但是,我的许多方法仍然会进行5-10个单独的SQL调用。 Most of my actions have a response time of around 100ms, but one of my most common actions averages 300-400ms, and some actions randomly peak at over 1000ms. 我的大多数动作都有大约100毫秒的响应时间,但我最常见的一个动作平均为300-400毫秒,而一些动作随机峰值超过1000毫秒。

The logs are of little help, as the errors seem to occur randomly, or at least the pattern does not appear related to the actions being called or data being accessed. 日志几乎没有帮助,因为错误似乎是随机发生的,或者至少该模式似乎与被调用的动作或正在访问的数据无关。

Could I alleviate the error by adding additional mongrel instances? 我可以通过添加额外的mongrel实例来缓解错误吗? Or are the mySQL connections limited by the server, and thus unrelated to the number of processes I divide my traffic across? 或者mySQL连接是否受服务器限制,因此与我划分流量的进程数无关?

Is this most likely a problem with my coding, or should I be pressing my host for more capacity/less load on the shared server? 这很可能是我的编码问题,还是我应该按主机以获得更多容量/更少的共享服务器负载?

ActiveRecord has pooled database connections since Rails 2.2, and it's likely that that's what's causing your excess connections here. 自Rails 2.2以来,ActiveRecord汇集了数据库连接,这可能是导致你的多余连接的原因。 Try turning down the value of pool in your database.yml for that environment (it defaults to 5). 尝试在database.yml中为该环境调低pool的值(默认为5)。

Docs can be found here . 可以在这里找到文档。

Are you caching anything? 你在缓存什么吗? It's an important part of alleviating application and database load. 它是缓解应用程序和数据库负载的重要部分。 The Rails Guides have a section on caching . Rails指南有一节关于缓存

Something is wrong. 出了点问题。 A Mongrel instance processes 1 request at a time so if you have 2 Mongrel instances then you should not be seeing more than 2 active MySQL connections (from the mongrels at least) 一个Mongrel实例一次处理1个请求,所以如果你有2个Mongrel实例,那么你不应该看到超过2个活动的MySQL连接(至少来自mongrels)

You could log or graph the output of SHOW STATUS LIKE 'Threads_connected' over time. 您可以记录或绘制SHOW STATUS LIKE'Threads_connected'随时间的输出。

PS: this is not very many Mongrels. PS:这不是很多Mongrels。 if you want to be able to service more than 2 simultaneous requests then you'll want more. 如果您希望能够同时为2个以上的请求提供服务,那么您将需要更多。 ...if memory is tight, you can switch to Phusion Passenger and REE. ...如果记忆力很紧,你可以切换到Phusion Passenger和REE。

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

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