简体   繁体   English

我无法通过Rails连接到远程mysql服务器

[英]I can't connect to a remote mysql server through ruby on rails

I have a very strange problem. 我有一个很奇怪的问题。 I have a mysql database setup on an amazon ec2 server. 我在Amazon EC2服务器上安装了mysql数据库。 I have opened up the amazon firewall and my router firewalls appropriately so I can connect through port 3306. I can connect to the ec2 server easily from my linux VM at the command line as follows: 我已经适当地打开了亚马逊防火墙和路由器防火墙,以便可以通过端口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>

So the connection works. 这样连接就可以了。 This verifies that I can successfully punch through the firewall, and there is no funny business due to running inside a VM. 这证明我可以成功突破防火墙,并且由于在VM内运行而不会产生任何麻烦。 So now I try it from within ruby as follows 所以现在我从红宝石内部尝试如下

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

This prints out "AAPL" at the command line. 这将在命令行中打印出“ AAPL”。 So everything is working from the ruby perspective. 因此,一切都从红宝石的角度进行。 Now I try connecting using ruby on rails. 现在,我尝试在轨道上使用红宝石进行连接。

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

So I start up the rails server, using the "rails server" command. 因此,我使用“ rails server”命令启动了Rails服务器。 WebBRICK starts ok and displays this 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

I open up my browser window and connect to 我打开浏览器窗口并连接到

http://localhost:3000 

in Chrome. 在Chrome中。 Chrome spins indefinitely. Chrome会无限旋转。 No message appears in the console window where WebBRICK was started. 启动WebBRICK的控制台窗口中没有消息。 This is all I see in the rails log: 这就是我在rails日志中看到的全部内容:

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

I leave it alone for 5 minutes and nothing happens. 我独自呆了5分钟,什么也没发生。 I have to kill WebBRICK with a ctrl-c. 我必须使用ctrl-c杀死WebBRICK。

My only clue as to what could be happening is that after webrick is shutdown, I try to connect using mysql as shown above and I get this error: 关于可能发生的事情,我的唯一线索是在关闭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'

So this is indicating that ruby on rails is failing to connect (probably in a loop with a retry) and the mysql db on the amazon server has detected this and blocked that host from connecting. 因此,这表明ruby on rails无法连接(可能是在重试的循环中),并且Amazon服务器上的mysql db已检测到此情况并阻止了该主机的连接。 So I log into the amazon host, run the flush-hosts command, and can connect from the command line or the ruby script. 因此,我登录到Amazon主机,运行flush-hosts命令,并且可以从命令行或ruby脚本进行连接。 But I can never get rails to connect. 但是我永远无法获得连接。

Rails Version: 3.2.7 Ruby Version: 1.8.7 My SQL server version: 5.5.20 Rails版本:3.2.7 Ruby版本:1.8.7我的SQL Server版本:5.5.20

Any ideas? 有任何想法吗? I'm at a loss for how to debug what the connection error inside rails could be. 我不知道如何调试rails内部的连接错误。 I am not getting a stack trace, crash, or anything that could help me debug what is going on . 我没有得到堆栈跟踪,崩溃或任何可以帮助我调试正在发生的情况的信息。

Try writing a simple ruby script to test the mysql2 gem (it looks like you already tested mysql, but your rails app uses mysql2). 尝试编写一个简单的ruby脚本来测试mysql2 gem(看起来已经测试过mysql,但是您的rails应用程序使用mysql2)。

Also, try stepping through the code with ruby-debug or some other debugger. 另外,尝试使用ruby-debug或其他调试器逐步检查代码。

finally got this stack trace: 终于得到了这个堆栈跟踪:

it looks like there is an internal error which then causes rails to connect to the database again... causing a deadlock: 似乎有一个内部错误,然后导致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'

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

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