Mysql ActiveRecord::ConnectionTimeoutError:无法在5.000秒内获得数据库连接(已等待5.000秒)
我有一个rails应用程序正在生产中,前几天我部署了一些更改。现在我突然发现一个错误Mysql ActiveRecord::ConnectionTimeoutError:无法在5.000秒内获得数据库连接(已等待5.000秒),mysql,ruby-on-rails,nginx,puma,airbrake,Mysql,Ruby On Rails,Nginx,Puma,Airbrake,我有一个rails应用程序正在生产中,前几天我部署了一些更改。现在我突然发现一个错误ActiveRecord::ConnectionTimeoutError:无法在5.000秒内(等待5.000秒)一天多次获得数据库连接,必须重新启动puma才能修复该问题 我完全搞不懂这是什么原因造成的。我没有在服务器上更改任何内容,所做的更改非常简单(添加到视图和添加到控制器方法) 我没有在日志文件中看到太多东西 我正在使用rails 4.1.4和ruby 2.0.0p481 你知道为什么我的人脉都满了吗?我
ActiveRecord::ConnectionTimeoutError:无法在5.000秒内(等待5.000秒)
一天多次获得数据库连接,必须重新启动puma才能修复该问题
我完全搞不懂这是什么原因造成的。我没有在服务器上更改任何内容,所做的更改非常简单(添加到视图和添加到控制器方法)
我没有在日志文件中看到太多东西
我正在使用rails 4.1.4和ruby 2.0.0p481
你知道为什么我的人脉都满了吗?我的连接池设置为10,我使用默认的puma配置
以下是堆栈跟踪:
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)):
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `block in wait_poll'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `loop'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `wait_poll'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:136:in `block in poll'
/usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:146:in `synchronize'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:134:in `poll'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:418:in `acquire_connection'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
/usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
activerecord (4.1.4) lib/active_record/connection_handling.rb:113:in `retrieve_connection'
activerecord (4.1.4) lib/active_record/connection_handling.rb:87:in `connection'
activerecord (4.1.4) lib/active_record/query_cache.rb:51:in `restore_query_cache_settings'
activerecord (4.1.4) lib/active_record/query_cache.rb:43:in `rescue in call'
activerecord (4.1.4) lib/active_record/query_cache.rb:32:in `call'
activerecord (4.1.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.4) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
airbrake (4.1.0) lib/airbrake/rails/middleware.rb:13:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
dragonfly (1.0.5) lib/dragonfly/cookie_monster.rb:9:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
airbrake (4.1.0) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (4.1.0) lib/airbrake/user_informer.rb:12:in `call'
railties (4.1.4) lib/rails/engine.rb:514:in `call'
railties (4.1.4) lib/rails/application.rb:144:in `call'
railties (4.1.4) lib/rails/railtie.rb:194:in `public_send'
railties (4.1.4) lib/rails/railtie.rb:194:in `method_missing'
puma (2.9.0) lib/puma/configuration.rb:71:in `call'
puma (2.9.0) lib/puma/server.rb:490:in `handle_request'
puma (2.9.0) lib/puma/server.rb:361:in `process_client'
puma (2.9.0) lib/puma/server.rb:254:in `block in run'
puma (2.9.0) lib/puma/thread_pool.rb:92:in `call'
puma (2.9.0) lib/puma/thread_pool.rb:92:in `block in spawn_thread'
Puma init.d脚本
#!/bin/sh
# Starts and stops puma
#
case "$1" in
start)
su myuser -c "source /etc/profile && cd /var/www/myapp/current && rvm gemset use myapp && puma -d -e production -b unix:///var/www/myapp/myapp_app.sock -S /var/www/myapp/myapp_app.state"
;;
stop)
su myuser -c "source /etc/profile && cd /var/www/myapp/current && rvm gemset use myapp && RAILS_ENV=production bundle exec pumactl -S /var/www/myapp/myapp_app.state stop"
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
编辑
我想我终于缩小了gem的问题范围,使用了current\u user
或user\u signed\u in?
方法,在application\u controller.rb
中,在操作之前
这是我的应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!, :get_new_messages
# Gets the unread messages for the logged in user
def get_new_messages
@num_new_messages = 0 # Initially set to 0 so login page, etc works
# If the user is signed in, fetch the new messages
if user_signed_in? # I also tried !current_user.nil?
@num_new_messages = Message.where(:created_for => current_user.id).where(:viewed => false).count
end
end
...
end
class ApplicationControllercurrent_user.id)。where(:viewsed=>false)。计数
结束
结束
...
结束
如果我删除If
块,我就没有问题了。自从我引入了这段代码,我的应用程序似乎已经没有连接了。如果我将If
块保留在原位并移除airbrake gem,我的应用程序似乎运行正常,在我的数据库.yml
文件中,我的池中只设置了默认的5个连接
编辑
我最终发现,如果我在config/environments/production.rb
文件config.exceptions\u app=self.routes
中注释掉这一行,我就不会得到错误。这似乎是因为在采取行动之前,应用程序控制器中的自定义路由+设计造成的。我在github上创建了一个问题和一个可复制的项目
我也遇到了同样的问题,这些问题是由太多打开的数据库连接引起的。当您在控制器之外(在模型、邮件、pdf生成器等中)进行数据库查询时,可能会发生这种情况
我可以通过在这个块中包装这些查询来修复它,它会自动关闭连接
ActiveRecord::Base.connection_pool.with_connection do
# your code
end
由于Puma是多线程的,所以池大小(正如eabraham所提到的)也可能是一个限制。试着增加(一点)
我希望这有帮助 在“设计人”的帮助下,我想我终于解决了这个问题。通过将自定义错误页面与它自己的控制器一起使用,我似乎没有跳过before_操作get_new_messages
。因此,非常简单的解决方法是添加:
skip_before_filter :get_new_messages
到我的自定义错误控制器
这个问题详细解释了这背后的原因:最终这个问题仍然困扰着我一年左右。我终于从与美洲狮的合作中找到了一个很好的解决办法
将puma升级至至少2.15.x
是否相关?这可能是相关的,但奇怪的是,我已经有一年没有遇到任何类似的问题了,在过去的一年中,我已经部署了更改。前几天是我第一次遇到这个问题。你在运行多少Puma线程?还有,你们有工人吗(sidekiq、延迟工作等)?没有工人。我相信我只跑了一条美洲狮的线。我只是使用默认的puma配置。你能增加你的数据库池大小吗?这是否会减少或停止超时?问题仍然存在。你现在使用的是什么ruby和puma版本?我在JRuby 9.1.2.0
和puma 3.4.0
上。我仍然在puma 2.15.x上,我相信我正在运行Ruby的MRI版本。绝对不是在JRuby上。可能是版本2.1.x或2.2.x.@bibstha最新的Puma没有问题。由于使用不当,我收到了错误。帮助了我。