title: I can't connect to a remote mysql server through ruby on rails
body: <p>I have a very strange problem.  I have a mysql database setup on an amazon ec2 server.  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:</p>
  
  <pre><code>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&gt;
  </code></pre>
  
  <p>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.  So now I try it from within ruby as follows</p>
  
  <pre class="lang-ruby prettyprint-override"><code>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
  </code></pre>
  
  <p>This prints out "AAPL" at the command line.  So everything is working from the ruby perspective.  Now I try connecting using ruby on rails.  </p>
  
  <pre><code>development:
    adapter: mysql2
    encoding: utf8
    reconnect: false
    database: redacted
    pool: 5
    username: redacted
    password: redacted
    host: redacted.amazonaws.com 
    port: 3306
  </code></pre>
  
  <p>So I start up the rails server, using the "rails server" command.  WebBRICK starts ok and displays this</p>
  
  <pre><code>me@me-VirtualBox:~/host/workspace/rails-apps/sm$ rails server
  =&gt; Booting WEBrick
  =&gt; Rails 3.2.7 application starting in development on http://0.0.0.0:3000
  =&gt; Call with -d to detach
  =&gt; 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
  </code></pre>
  
  <p>I open up my browser window and connect to </p>
  
  <pre><code>http://localhost:3000 
  </code></pre>
  
  <p>in Chrome.  Chrome spins indefinitely.  No message appears in the console window where WebBRICK was started.  This is all I see in the rails log:</p>
  
  <pre><code>Started GET "/" for 127.0.0.1 at Sat Aug 18 09:39:47 -0600 2012
  Connecting to database specified by database.yml
  </code></pre>
  
  <p>I leave it alone for 5 minutes and nothing happens.  I have to kill WebBRICK with a ctrl-c.</p>
  
  <p>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:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>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.  So I log into the amazon host, run the flush-hosts command, and can connect from the command line or the ruby script.  But I can never get rails to connect.</p>
  
  <p>Rails Version: 3.2.7
  Ruby Version: 1.8.7
  My SQL server version: 5.5.20</p>
  
  <p>Any ideas?  I'm at a loss for how to debug what the connection error inside rails could be.  I am not getting a stack trace, crash, or anything that could help me debug what is going on .</p>