【问题标题】:I can't connect to a remote mysql server through ruby on rails我无法通过 ruby​​ on rails 连接到远程 mysql 服务器
【发布时间】:2012-08-14 18:24:02
【问题描述】:

我有一个很奇怪的问题。我在亚马逊 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 

在 Chrome 中。铬无限旋转。启动 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 红宝石版本:1.8.7 我的 SQL 服务器版本:5.5.20

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

【问题讨论】:

  • 嗯,所以我确实安装了它并运行了“捆绑安装”。现在它因 ActiveRecord::ConnectionNotEstablished 而失败,我得到一个堆栈跟踪。虽然没有 mysql 错误。
  • 天哪,我刚刚开始工作了。 mysql2 在 Rails 中根本不起作用。我必须修改 database.yml 以使用“mysql”适配器而不是“mysql2”适配器,并且必须修改 Gemfile 以使用 mysql。

标签: mysql ruby-on-rails ruby amazon-ec2


【解决方案1】:

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

另外,尝试使用 ruby​​-debug 或其他一些调试器单步调试代码。

【讨论】:

  • 感谢您的建议。我试过了,使用 mysql2 的测试程序工作正常。我似乎无法将代码剪切并粘贴到此评论中。
【解决方案2】:

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

看起来有一个内部错误导致 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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2016-12-25
    • 1970-01-01
    • 2011-07-26
    • 2019-05-13
    相关资源
    最近更新 更多