I am trying to deploy a Rails app that uses Mysql
I have:
Created a Heroku app and have pushed my app to heroku.
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
What am I doing wrong ?
What is my Rdshostname? Is it the Amazon endpoint?
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.
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:
So, to recap, if you've:
You should now be able to use RDS with your Rails 3 app on heroku. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With