与Rails和MySQL的奇怪脱节

时间:2014-08-11 10:34:46

标签: mysql ruby-on-rails disconnect

Rails 4应用程序已经完美地运行了5个月,但几分钟前它开始遇到错误:

ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds))

DBMS是本地安装的MySQL。

尝试重新加载页面或引用应用程序的不同URL并不重要。

只有在我重新启动Rails应用程序后,问题才消失。

它是什么以及如何让应用程序在没有管理员干预的情况下自动解决此问题?

1 个答案:

答案 0 :(得分:1)

这是您在database.yml

中的内容
{environment_name}:
  adapter: mysql2
  encoding: utf8
  database: {product_name}_environment_name
  username: username
  password: password
  socket: /var/run/mysqld/mysqld.sock 

在连接参数中为'reaping_frequency'和'pool'添加正确的值,它可能看起来像

{environment_name}:
  adapter: mysql2
  encoding: utf8
 database: {product_name}_environment_name
 pool: 15
 reaping_frequency: 3
 username: username
 password: password
 socket: /var/run/mysqld/mysqld.sock

选项说明

pool: number indicating size of connection pool (default 5)

checkout_timeout: number of seconds to block and wait for a connection before giving up and raising a timeout error (default 5 seconds).

reaping_frequency: frequency in seconds to periodically run the Reaper, which attempts to find and close dead connections, which can occur if a programmer forgets to close a connection at the end of a thread or a thread dies unexpectedly. (Default nil, which means don't run the Reaper).

dead_connection_timeout: number of seconds from last checkout after which the Reaper will consider a connection reapable. (default 5 seconds).