2

我有一个非常奇怪的问题。我在亚马逊 ec2 服务器上设置了 mysql 数据库。我已经适当地打开了亚马逊防火墙和我的路由器防火墙,因此我可以通过端口 3306 进行连接。我可以通过命令行从我的 linux VM 轻松连接到 ec2 服务器,如下所示:

me@me-VirtualBox:~/host/workspace/rails-apps/sm$ mysql -h redacted.amazonaws.com -u redacted -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4012389
Server version: 5.5.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

所以连接有效。这验证了我可以成功打穿防火墙,并且没有因为在VM内部运行而导致的搞笑业务。所以现在我从 ruby​​ 中尝试如下

require 'rubygems'
require 'mysql'

mycon = Mysql::new("redacted.amazonaws.com", "redacted", "redacted", "redacted")

symres = mycon.query("select * from symbols where symbol = \"AAPL\";")  

symres.each do |symbol|
  puts symbol[0].to_s
end

mycon.close

这会在命令行打印出“AAPL”。所以一切都从红宝石的角度来看。现在我尝试使用 ruby​​ on rails 进行连接。

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: redacted
  pool: 5
  username: redacted
  password: redacted
  host: redacted.amazonaws.com 
  port: 3306

因此,我使用“rails server”命令启动了 rails 服务器。WebBRICK 启动正常并显示此

me@me-VirtualBox:~/host/workspace/rails-apps/sm$ rails server
=> Booting WEBrick
=> Rails 3.2.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-08-18 10:00:08] INFO  WEBrick 1.3.1
[2012-08-18 10:00:08] INFO  ruby 1.8.7 (2011-06-30) [x86_64-linux]
[2012-08-18 10:00:14] INFO  WEBrick::HTTPServer#start: pid=9977 port=3000

我打开浏览器窗口并连接到

http://localhost:3000 

在铬。铬无限旋转。启动 WebBRICK 的控制台窗口中不会出现任何消​​息。这就是我在 rails 日志中看到的全部内容:

Started GET "/" for 127.0.0.1 at Sat Aug 18 09:39:47 -0600 2012
Connecting to database specified by database.yml

我让它静置 5 分钟,没有任何反应。我必须用 ctrl-c 杀死 WebBRICK。

关于可能发生的事情,我唯一的线索是在 webrick 关闭后,我尝试使用 mysql 进行连接,如上所示,我收到此错误:

me@me-VirtualBox:~/host/workspace/rails-apps/sm$ mysql -h redacted.amazonaws.com -u redacted -p
Enter password: 
ERROR 1129 (HY000): Host 'redacted.co.comcast.net' is blocked because of many    connection errors; unblock with 'mysqladmin flush-hosts'

因此,这表明 ruby​​ on rails 无法连接(可能在重试的循环中),并且亚马逊服务器上的 mysql db 检测到了这一点并阻止了该主机的连接。所以我登录亚马逊主机,运行flush-hosts命令,可以从命令行或ruby脚本连接。但我永远无法连接铁轨。

Rails 版本:3.2.7 Ruby 版本:1.8.7 My SQL 服务器版本:5.5.20

有任何想法吗?我不知道如何调试导轨内的连接错误可能是什么。我没有收到堆栈跟踪、崩溃或任何可以帮助我调试正在发生的事情的信息。

4

2 回答 2

0

尝试编写一个简单的 ruby​​ 脚本来测试 mysql2 gem(看起来您已经测试过 mysql,但您的 rails 应用程序使用 mysql2)。

此外,尝试使用 ruby​​-debug 或其他调试器逐步执行代码。

于 2012-08-18T16:56:20.620 回答
0

终于得到了这个堆栈跟踪:

看起来有一个内部错误,然后导致 rails 再次连接到数据库......导致死锁:

ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5 seconds (waited 5.40695462 seconds). The max pool size is currently 25; consider increasing it.):
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:258:in `block (2 levels) in checkout'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
  /home/ubuntu/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
  /home/ubuntu/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
  activerecord (3.2.11) lib/active_record/query_cache.rb:67:in `rescue in call'
于 2013-01-31T21:36:34.930 回答