Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku help deploying Rails app that uses Mysql database

I am trying to deploy a Rails app that uses Mysql

I have:

  1. Created a Heroku app and have pushed my app to heroku.

  2. I have added Amazon RDS I have created a Amazon RDS database instance. My Heroku Amazon RDS Database URL is: mysql://mysusername:[email protected]/mydatabasename

My Amazon RDS DB Security Group is set to default

  1. I have tried to push my local database but get the following error Heroku help Amazon RDS rails push database error

What am I doing wrong ?

What is my Rdshostname? Is it the Amazon endpoint?

like image 512
Rails beginner Avatar asked Mar 19 '11 13:03

Rails beginner


People also ask

Does Heroku support MySQL database?

Table of Contents. MySQL is a popular relational database maintained by Oracle. Although Heroku Postgres is the recommended relational database for Heroku apps because of its tight integration with the platform, there are options for applications that currently run on MySQL.


1 Answers

I just wanted to first say that I feel your pain. I was recently a complete newbie to Rails 3 + Heroku + Amazon RDS. But, tonight I conquered the problem and immediately got on Stack Overflow to let others who were having issues know how. I'll blog about it later.

Some of this, you might skip, but I'll try to be comprehensive in my response here, including steps you'll need to take as well as gotchas and issues I encountered as well.

Gotcha #1: heroku fails to install mysql2 gem with the following error:

       You have added to the Gemfile:
      * mysql2
      FAILED: http://devcenter.heroku.com/articles/bundler
!     Heroku push rejected, failed to install gems via Bundler

Solution: I almost always use PostgreSQL for anything demanding I am working on and for sandboxes and experiments, I just use SQLite3 to get in and out fast. The real issue was that I wasn't running MySQL on my local machine. When I came back and tried to run the bundle install locally, naturally the gem install failed because it could not find the mysql libraries. I'm sure there is a way around this, but I just bit the bullet and installed mysql locally. Afterwards, I was able to run bundle install with no problem. Also, git push heroku master pushed the application to Heroku with no failure:

   Installing activerecord (3.0.6) 
   Installing activeresource (3.0.6) 
   >>>>Installing mysql2 (0.3.2) with native extensions<<<< 
   Using bundler (1.0.7) 
   Installing thor (0.14.6) 
   Installing railties (3.0.6) 
   Installing rails (3.0.6) 
   Installing sqlite3 (1.3.3) with native extensions 
   Your bundle is complete! It was installed into ./.bundle/gems/
   Compiled slug size is 3.9MB
   -----> Launching... done
   http://myapp.heroku.com deployed to Heroku

Gotcha #2: My application choked, even after installing the mysql2 gem. heroku logs revealed:

RuntimeError (!!! Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2'):

What? I just added that to my gemfile, committed it and pushed it sucessfully! I really thought someone was playing a nasty joke on me. A little research and fiddling revealed that a heroku rake db:migrate was returning:

WARNING: This version of mysql2 (0.3.2) doesn't ship with the ActiveRecord adapter bundled anymore as it's now part of Rails 3.1
WARNING: Please use the 0.2.x releases if you plan on using it in Rails <= 3.0.x

Solution: I uninstalled the mysql2 gem with gem uninstall mysql2 and then modified the line in my gemfile to read:

gem 'mysql2', '< 0.3'

This installed the 0.2.7 version of the mysql2 gem, which also installed on Heroku sucessfully.

Gotcha #3: After installing the correct version of the mysql2 gem, a heroku rake db:migrate still returned the same error:

rake aborted!
!!! Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2'

Okay, so I did some more research and found this thread, which basically told me that the adapter was trying to use the "mysql" adapter instead of the "mysql2" adapter.

Solution: The way around this one is to manually set the DATABASE_URL in heroku config to use mysql2:// by doing this:

heroku config:add DATABASE_URL=mysql2://user:[email protected]/databaseName

(you can find the "dbInstanceName.hostname.us-east-1.amazonaws.com" portion of this url in your AWS config panel by clicking on the database you are using)

This has to be done using the command line tool, and cannot be added using the RDS Add-on GUI on the web control panel, because Heroku won't accept it as a valid db URL there.

Gotcha #4: Incorrect security configurations will not return informative or useful errors.

Solution: Ensure that you have added a security group to your RDS configuration to allow Heroku to access your RDS instance and added that security group to your instance. For CLI help doing this you can view the RDS docs on Heroku or login to your AWS console and do this:

  1. Click on RDS tab at the top.
  2. Click on DB Security Groups in the left column
  3. Click "Create DB Security Group"
  4. Enter a name and a description (ex: Name: Heroku, Description: Allow Heroku Access!")
  5. In the lower panel, with the new group selected, create a new authorization by selecting "EC2 Security Group" from the dropdown.
  6. For Security Group, enter: "default" and AWS Account ID, enter: "098166147350" (This is important; this account number belongs to Heroku, and it it must be added to the default security group in order to work.)
  7. Click "Add"
  8. IMPORTANT! Modify the existing database and add the new security group to it.

So, to recap, if you've:

  1. Installed the correct version of the mysql2 gem (<0.3)
  2. Setup your heroku configuration to use the mysql2 adapter by setting the DATABASE_URL correctly.
  3. and ensured that Heroku has access to your RDS by creating the appropriate security group on AWS RDS and added that security group to your database,

You should now be able to use RDS with your Rails 3 app on heroku. :)

like image 178
Timothy Britt Avatar answered Oct 14 '22 10:10

Timothy Britt