How to deploy a Rails app using Mysql on heroku?
I find out that my app did not need Amazon RDS (Too expensive for a small app).
Here is my answer how to use Amazon RDS
Heroku help deploying Rails app that uses Mysql database
Include mysql2 gem in your gemfile:
gem 'mysql2'
Now, your choice can be: https://addons.heroku.com/cleardb add-ons. You can get upto 5mb free storage but you need to fill your credit card information for accessing it.
Steps for using clearDB add-ons are:
# add cleardb add-ons to your app
$ heroku addons:add cleardb:ignite
-----> Adding cleardb to sharp-mountain-4005... done, v18 (free)
# retrieve your database URL:
$ heroku config | grep CLEARDB_DATABASE_URL
CLEARDB_DATABASE_URL => mysql://adffdadf2341:adf4234#us-cdbr-east.cleardb.com/heroku_db?reconnect=true
# copy CLEARDB_DATABASE_URL config variable and set it to your DATABASE_URL config variable
$ heroku config:set DATABASE_URL='mysql://adffdadf2341:adf4234#us-cdbr-east.cleardb.com/heroku_db?reconnect=true'
Adding config vars:
DATABASE_URL => mysql2://adffd...b?reconnect=true
Restarting app... done, v61.
# NOTE: since we are using ```mysql2``` in our gemfile so replace mysql:// scheme in the CLEARDB_DATABASE_URL to mysql2://
$ heroku config:set DATABASE_URL='mysql2://adffdadf2341:adf4234#us-cdbr-east.cleardb.com/heroku_db?reconnect=true'
$ heroku config:set CLEARDB_DATABASE_URL='mysql2://adffdadf2341:adf4234#us-cdbr-east.cleardb.com/heroku_db?reconnect=true'
Please follow: https://devcenter.heroku.com/articles/cleardb for more information
Hope that can help you.
If you do a heroku db:push from your MySql data, it'll automatically get pushed into the heorku PostgreSQL database structure.
You can then do db:pulls and pull back into mysql. Taps provides this database magic.
It's really great -- I'd try it out first before trying to get RDS working.
Related
I am learning ruby on rails and heroku.
I have some questions.
Could somebody help me?
After running heroku create, the terminal is somehow connected to the repository/project in heroku. After that the developer can see logs, access database, ect. But, how can I access via terminal my completed project,which is online already, in heroku without creating the same apps and upload it again to heroku?
How can access my pg database in heroku or offline?
I saw this and try to run heroku pg:psql. it gives me hidden-atoll-4790::DATABASE=> help I try \? to see pg command but I cannot use it.
I saw this link to see pg db offline. But why cannot I access /var/lib/postgresql/9.3/main directory ? it is said that I don't have permission eventhough my account is administrator account.
1.Run heroku run rails console
Run
$ heroku apps # to see all apps created by your account
$ heroku open # to open you current project in browser (run it form root of your project)
$ heroku pg:psql DATABASE_URL # to gain access to heroku postgresql terminal, here you can run SQL queries
Refer to
CLI and Heroku PG for more info
I'm kind newbie on Rails. I developed an small CMS running on Heroku and using Postgres.
I manually inserted a few data on my Development db. And I'd like to fill the Production DB with such data.
Both DB are equals. Same structure, fields etc.
I tried through gem install yaml_db
--> gem 'yaml_db' ->rake db:data:dump → rake db:data:load
But I got: “rake aborted!
Don't know how to build task 'db:data:load'
”
Is that the proper approach? What am I doing wrong?
Thanks
You can import your local database to heroku database easily using heroku pgbackups by follow the instructions here.
Please check here in Heroku Docs
heroku db:push
I've spent most of the last three days struggling with installing RefineryCMS on Heroku.
There are a lot of questions on SO and on various blogs, as well as documentation from Refinery and Heroku (and Rails) but none of the walkthroughs have helped 100%... Every page seems to be missing some vital piece of information.
I've tried to document all the necessary steps having gone through them three or four times, refining the procedure each time (working out what is and isn't necessary).
References included where they were obvious.
Run the refinery initialisation script, with Heroku option
refinerycms myapp --heroku
From http://refinerycms.com/guides/heroku
The output should give you a new heroku app and its name listed in the output:
"Creating Heroku app.. run heroku create --stack cedar from "."
Creating ... done, stack is cedar
http://[your heroku app].herokuapp.com/ | git#heroku.com:[your heroku app].git
Git remote heroku added"
Create bucket on Amazon AWS…
Should be self-explanatory.
Set connection info for Amazon in the Heroku environment
We need both sets of credentials.
AWS_* and FOG_* is for Heroku (and the rails precompile, I believe).
S3_* stuff is for Refinery to be able to upload images etc.
heroku config:add AWS_ACCESS_KEY_ID="<your key>" AWS_SECRET_ACCESS_KEY="<your secret>" FOG_DIRECTORY="<your bucket name>" FOG_PROVIDER="AWS" FOG_REGION="<your aws region>"
heroku config:add S3_BUCKET="<your bucket name>" S3_KEY="<your key>" S3_REGION="<your aws region>" S3_SECRET="<your secret>"
Add required gems to your Gemfile
gem 'globalize3', '0.3.0'
From refinerycms not working when adding page
gem 'unf'
(fixes some warnings)
gem 'rails_12factor'
From Why is the rails_12factor gem necessary on Heroku?
gem 'asset_sync'
From https://github.com/rumblelabs/asset_sync.
This gem seems the only way to get the assets pushed up to the cloud... Although perhaps you can make do without it; perhaps someone else can confirm.
ruby '2.0.0'
[ place this at the end of the Gemfile. (Needed to clear Heroku warnings) ]
Add asset_sync asset host path in config/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
From https://github.com/rumblelabs/asset_sync
Set the site name in config/initializers/refinery/core.rb
config.site_name = <your site name>
Set the s3_backend in the config/environments/production.rb
Refinery::Core.config.s3_backend = true
From https://github.com/refinery/refinerycms/issues/1879
Configure database details
Remove sqlite3 in config/database.yml and setting postgresql instead: this is optional but recommended by Heroku and others
For adapter:
sqlite3 => postgresql
For database name:
db/foo.sqlite3 => <sitename>_foo
Set user-env-precompile settings
heroku labs:enable user-env-compile -a myapp
From https://devcenter.heroku.com/articles/labs-user-env-compile
Run the Bundler
bundle install
Note: First of all, I had to run, as prompted:
1. rvm use 2.0.0 in order to match the version we're using in Gemfile
2. bundle update globalize3
From refinerycms not working when adding page
Create (local) production database
RAILS_ENV=production rake db:create
Set environment variables needed before asset precompile can work
(this is for *nix, do whatever you need to on your platform)
export FOG_DIRECTORY="<your bucket name>"
export FOG_PROVIDER="AWS"
export AWS_SECRET_ACCESS_KEY="<your secret>"
export AWS_ACCESS_KEY_ID="<your key>"
Precompile the assets (???)
NOTE: This MAY NOT be required... (I did do this step each time but cannot be sure whether it's required. The next steps suggest to me it's not necessary to manually precompile: we need to change the "initialize_on_precompile" to false, run a git push to heroku (i.e. without assets), then set the "initialize_on_precompile" back to true for future pushes. Not sure why we need to do this, and it may be an issue only with Rails 3.* (see: https://devcenter.heroku.com/articles/rails-asset-pipeline)
RAILS_ENV=production bundle exec rake assets:precompile
Set precompile false in config/application.rb
config.assets.initialize_on_precompile = false
From http://refinerycms.com/guides/heroku…
This setting is required the first time you git push to heroku, because otherwise the precompile step of git push heroku master always fails with:
Connecting to database specified by DATABASE_URL
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
NOTE: The reference is not clear on this (although setting intially to false then true is mentioned elsewhere).
Check in files to git and commit changes
Note: add the Gemfile.lock along with all the other changes.
Push to heroku
git push heroku master
Set precompile option back to true in config/application.rb
config.assets.initialize_on_precompile = true
From http://refinerycms.com/guides/heroku…
Add config/application.rb to git and commit (!!)
... if you don't, the next push will fail
Push to heroku (Demonstrates that this time it succeeds)
git push heroku master
Migrate and seed on the Heroku database
heroku run rake db:migrate
heroku run rake db:seed
From http://refinerycms.com/guides/heroku
Ready to go!
Hopefully from here you have access to your RefineryCMS page, with all the Refinery CSS and images displaying correctly (both on the admin screens and when 'viewing website' but still logged in.
If you add an image using the Refinery menu you should subsequently be able to see that image added to your AWS bucket. I don't have thumbnails working yet.
I have now my rails 3.2.1 app running on Heroku.
I've tried to upload the database to mongohq via the heroku mongo:push command, after installing the heroku mongo plugin.
https://github.com/pedro/heroku-mongo-sync
I get the message asking me to confirm if I want to push, but once the push is done, there is nothing my db.
I'm not sure if it is a problem with heroku or if i'm missing a step.
Could it be that i need to put my app in production mode and migrate the database to production?
I'm not sure how to do that either.
Cheers
does your local heroku connection conform to the plugins assumptions [in the readme's config section]? if not you'll have to set it via:
export MONGO_URL = mongodb://user:pass#localhost:1234/db
i'll also note, that even after doing this i had to uninstall the heroku plugin and reinstall it from this fork: http://github.com/fjg/heroku-mongo-sync.git
heroku plugins:install http://github.com/fjg/heroku-mongo-sync.git
Check out the MongoSync Ruby Gem
It's a gem I wrote for that very purpose when I had to constantly copy my Local MongoDB database to and from my Production DB for a Project (I know it's stupid). It's extremely easy to use. Once you've entered your DB details in the mongo_sync.yml file, you can push and pull DBs using these rake tasks:
$ rake mongo_sync:push # Push DB to Remote
$ rake mongo_sync:pull # Pull DB to Local
Note: It's also available as shell script for non-ruby apps: mongo-sync
After three evenings on this problem and reading all the posts about this, I have to ask this question finally!
I want to deploy the most simple Rails app to Heroku:
rails new test_appli
cd test_appli
git init
git add .
git commit -m "initial commit"
heroku create
git push heroku master
Everything's OK, the application works well on Heroku. After that, I'll create a SQLite3 database:
rails generate scaffold User name:string email:string
rake db:migrate
Everything's OK on the local machine. I can see localhost:3000/users well. Then I want to put the DB on Heroku. First I modify my Gemfile:
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
Then I send the whole thing to Heroku:
git init
git add .
git commit -m "with Database"
git push heroku master
heroku rake db:migrate
Then there are no errors in the batch, everything is OK, the DB is sent, but the page heroku.com/users gives the error
Rails 500, "We're sorry, but something went wrong"
I don't know more what to do. Can you help me?
I suspect you're trying to deploy a Rails 3.1 application to the bamboo stack (heroku create defaults to the 1.9.2 bamboo stack and doesn't run Rails 3.1 out of the box.). The Cedar stack is much better suited to Rails 3.1 sites -
try
heroku create --stack cedar
when creating your application on Heroku and repush it up. Also note your rake command on Heroku will become
heroku run rake db:migrate
Do:
heroku run rake db:schema:load
I had the same issue. It works for me after git push heroku master
Don't do the git init in the second set of commands - you only need to initialise your Git repo once.
Other than that this looks fine - are you seeing any errors anywhere?
Why would you use the pg gem in your production group, but the sqlite3 gem in your development group? Seems to me that your problem is likely due to the fact that you're developing with a different database then you're using in the production environment. If I were you, I would stick to one, which would make it much easier to debug.
If you really want/need to get the application running ASAP, then just run it in production with sqlite... Gemfile:
gem 'rails'
gem 'sqlite3'
Also, a quick way to figure out what the error is would be to run heroku logs from the unix console.
Which version of Rails?
Can you try creating the application running on the Cedar stack?
heroku create myapp --stack cedar
Given the application is running on cedar you need to modify the commands a bit, for example:
heroku run rake db:migrate
In any case you really need to checkout your logs, because your problem might not even be database related, but assets related.
Do you have the heroku shared 5mb database instance added?
When you create your heroku app (on cedar) it doesn't necessarily create the database automatically.
airlift:projects $ heroku create --stack cedar testapp9
Creating testapp9... done, stack is cedar
http://testapp9.herokuapp.com/ | git#heroku.com:testapp9.git
airlift:projects $ heroku addons --app testapp9
logging:basic
releases:basic
When you view your heroku config, you get nothing:
heroku config
airlift:projects $ heroku config --app testapp9
airlift:projects $
To add a database:
heroku addons:add shared-database:5mb
airlift:projects $ heroku addons:add shared-database:5mb --app testapp9
-----> Adding shared-database:5mb to testapp9... done, v3 (free)
airlift:projects $ heroku config
No app specified.
Run this command from an app folder or specify which app to use with --app <app name>
airlift:projects $ heroku config --app testapp9
DATABASE_URL => postgres://blah:blah#blah.compute-1.amazonaws.com/blah
SHARED_DATABASE_URL => postgres://blah:blah#blah.compute-1.amazonaws.com/blah
airlift:projects $
Then you should be able migrate your db.
Hey #redronin thanks for helping me find a way to connect to my postgres database on Heroku, however as a newbie to Heroku and postgresql I had to reverse engineer what "blah" was. So I figure I would break it down to help others as you helped me.
postgres://[user]:[password]#[servername].compute-1.amazonaws.com/[database]