title: ActiveRecord::ConnectionNotEstablished for Rails3 mysql for rake db:migrate on EC2
body: <p>On Amazon's EC2 using Ubuntu, when I execute <strong>rake db:migrate</strong> as:</p>
  
  <pre><code>bundle exec rake db:migrate RAILS_ENV="production" --trace
  </code></pre>
  
  <p>I get the error <strong>ActiveRecord::ConnectionNotEstablished</strong> as shown below</p>
  
  <pre><code> ** Invoke db:migrate (first_time)
   ** Invoke environment (first_time)
   ** Execute environment
   ** Execute db:migrate
   rake aborted!
   ActiveRecord::ConnectionNotEstablished
   /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection'
  </code></pre>
  
  <p>I can login to mysql with the username and password in my config/database.yml file
  and the database is there. So mysql is running, and that is not the problem.</p>
  
  <p>My config/database.yml file looks like:</p>
  
  <pre><code> production:
     adapter: mysql2
     encoding: utf8
     reconnect: false
     database: app_production
     pool: 5
     username: root
     password: password
     host: localhost
     socket: /run/mysqld/mysqld.sock
  </code></pre>
  
  <p>Here are my gems from gem list:</p>
  
  <pre><code> abstract (1.0.0)
   actionmailer (3.0.3)
   actionpack (3.0.3)
   activemodel (3.0.3)
   activerecord (3.0.3)
   activeresource (3.0.3)
   activesupport (3.0.3)
   arel (2.0.10)
   builder (2.1.2)
   bundler (1.0.10 ruby)
   cgi_multipart_eof_fix (2.5.0)
   daemons (1.0.10)
   erubis (2.6.6)
   eventmachine (0.12.10)
   fastthread (1.0.7)
   gem_plugin (0.2.3)
   i18n (0.6.0)
   mail (2.2.19)
   mime-types (1.17.2)
   mongrel (1.2.0.pre2)
   mysql2 (0.2.7)
   polyglot (0.3.3)
   rack (1.2.5)
   rack-mount (0.6.14)
   rack-test (0.5.7)
   rails (3.0.3)
   railties (3.0.3)
   rake (0.9.2.2)
   rmagick (2.13.1)
   thin (1.2.7)
   thor (0.14.6)
   treetop (1.4.10)
   tzinfo (0.3.31)
   xmpp4r (0.5)
  </code></pre>
  
  <p>The error in connection_pool.rb is happening at the same place as described in this <a href="https://stackoverflow.com/questions/7390623/activerecord-3-1-0-multiple-databases">post</a> whose answer says that a connection needs to be established on ActiveRecord::Base.  The code is failing in connection_pool.rb because it is getting sent klass = ActiveRecord::Base which doesn't have a connection.  So I tried creating a model my_connection_base.rb that looks like the following</p>
  
  <pre><code> require 'active_record'
  
   class MyConnectionBase &lt; ActiveRecord::Base
  
        MyConnectionBase.establish_connection(
          :adapter =&gt; "mysql2",
          :host =&gt; "localhost",
          :username =&gt; "&lt;your database username&gt;",
          :password =&gt; "&lt;your database password&gt;",
          :database =&gt; File.dirname(__FILE__) + "app_production"
        )
     self.abstract_class = true
   end
  </code></pre>
  
  <p>And then all of my models inherit MyConnectionBase as in role.rb below:</p>
  
  <pre><code>require 'my_connection_base'
  
  class Role &lt; MyConnectionBase
    has_and_belongs_to_many :users
  end
  </code></pre>
  
  <p>But I still get ActiveRecord::ConnectionNotEstablished when I try to run a migration.  Is there something else I need to do with Rails 3 to ensure that the connection is made to mysql before performing the migration?</p>