Distributed Rails App - how to configure? - ruby-on-rails

I have a rather straight-forward Rails app that runs on puma. On a single node - it's great and works exactly how I want it to.
What I've been having a very hard time finding - I'm being asked to deploy this app across two nodes. If you connect to one node and do a transaction that effects the database - that change should also reflect on the other node. I cannot find any straightforward way to do this/I do not know how to.
Right now - my situation is the app is deployed on two nodes - I zipped it up, unzipped it, bundle install, rails s. Works fine on either node - but the two nodes are completely independent and have their own mysql databases. I'd want to unify them such that action on one node reflects on the other node. Sort of... some relationship between the two nodes such that they are able to talk to one another.
What are common tools or ways to accomplish this with Rails? My preference is to integrate as few software systems/install as few gems as possible - but of course, if it works, I must consider it.

As a simple setup, you can run your database on a single server. Let's call that server database.application.rails. Once you have that single server running your database and DNS or host entries pointing to it you'll need to update the Rails config/database.yml file to point to that single database as such:
production:
adapter: mysql2
host: database.application.rails
username: <%= ENV["DB_USER_NAME"] %>
password: <%= ENV["DB_PASSWORD"] %>
database: application_prod
The above configuration also assumes you have the username and password stored as environment variables - a best practice instead of storing them in the code base.
Now when you start your rails application you'll want to use bundle exec rails s -e production.
This is not exactly a high-availability setup since you're manually starting the servers and don't have a single web server in front of the puma servers.
For a more sophisticated deployment, use capistrano to deploy the application and configure nginx to point to both your back end puma servers.

Related

Connecting Rails to AWS MySQL database

I've recently created a rails app. I pushed the initial files onto github.
My problem is that I want to connect my rails app to AWS in order to use a MySQL database. I keep seeing tutorials on EC2 and Beanstalk, but I am not sure which one I should use. I have all the drivers needed for ruby through the gem installations.
I'm looking to figure out the main differences between Beanstalk and created a MySQL instance as well as what to put in my database.yml file in my rails app to connect to a database. Thank you in advance!
Just to give an idea, after you provision/create your instance on AWS (EC2 or wherever), you will then push your app's code to that remote server somewhere. You can do it manually via scripts, or you can use Capistrano for this. Once your app is deployed to the server, you need to connect to the server via SSH and manually edit the config/database.yml file to point to the staging/production MySQL database. (I'm generalizing, but I think you just need a step in the right direction.)

Should the database.yml be configured when testing locally for Heroku with foreman?

Can someone explain to me what they do when they initialise a rails app locally with foreman (part of Heroku toolbelt) (using postgreSQL), destined to run on Heroku?
I'm going by this guide: developing locally with foreman and what I don't understand is if we are expected to specify database username and passwords or if foreman is supposed to handle it as Heroku itself does?
This perplexes me a little as if we are supposed to modify the database.yml to hook it up to postgreSQL, then what is the point of using foreman instead of rails server?
If it does handle it, how does it handle it, and how would I configure my pg_hba.conf to respect it? Something like local all myuser trust?
Yes, database.yml needs to be configured with valid information for your development and test databases.
Foreman is only running what's in your Procfile, not ripping things out and plugging different things in like Heroku does.
So why do you want to use Foreman instead of rails server? Because it:
Runs all roles defined in your Procfile with one command
Automatically loads your .env
Will fail if any of your roles fail (so less scratching your head because some necessary backend service isn't running)

How do I deploy my rails app to Heroku using an existing SQL database?

I have an existing MySQL database and would like to build a Rails app on top of it. How do I deploy my app to Heroku but still use my existing database instead of the default PostgreSQL database?
EDIT
Rephrasing the question slightly (formerly, it was "Is it possible to deploy...").
Yes. You can configure your application any way you like including specifying a non-Heroku database in your config file, and including using the default Heroku paradigm of using environment variables for config but with manually set database URLs in the environment variables.
You can either continue hosting your MySQL database separately, or migrate your data to a MySQL database hosted within the Heroku platform, like ClearDB (provided as a Heroku add-on).
Yes, although Heroku recommends that you use PostgreSQL, it is possible to migrate MySQL to Postgres with their service. They have an article in their docs for just such a scenario: https://devcenter.heroku.com/articles/heroku-mysql
They recommend you first install the http://rubygems.org/gems/mysql2psql gem
If your needs are MySQL specific and you have to use heroku clearDB has and addon https://addons.heroku.com/cleardb that you can use with your heroku app.

How do I sync a local rails database with a remote database?

I've used Taps before on Heroku, but what is a good solution on non-heroku rails apps?
You could create a capistrano task/s to mysqldump on the source database, gzip the file, then scp it to the destination and execute the mysql script there to import.
I wrote a Capistrano recipe some time ago to sync a MySQL database and files between different environments: https://gist.github.com/111597
OK, there are some things you need to keep in mind. If you are using SQlite for development and MySQL / Postgre for production on the server, then sinking is almost impossible. On the contrary if you are using the same DB engines you can use administrator interface like MySQL administrator on your desktop and generate a backup file and upload it to server and vice versa.
May hosting providers provide PHPMyAdmin to take backups and restore it on the server.

Use Heroku DB into Engine Yard

I am using postgresql database for my Heroku Application.
I have very large database on AmazonAws as heroku not providing the Postgresql Database.
Now my client want to switch to EngineYard from Heroku.
Can i use same database (w/o taking backup and then reload) for my application on the EngineYard?
If YES
How can i use or steps for using the Existing AmazonAws Database with the new EngineYard Application.
You can, but only if you are using a dedicated database. From the Heroku database FAQ
Shared Database
No, connecting to your database from machines outside of Heroku is not
supported. We recommend that you encapsulate data access in an API to
manipulate it.
Dedicated Database
It's possible to connect to our dedicated databases using our
pg:ingress feature. Please see using the PG console for more
information.
The database connection string is available in the DATABASE_URL config. You can run
$ heroku config --long
to view it. However, it won't probably work if you use a shared database because it seems connection is restricted to the Heroku net.
Surely this is just a case of getting the correct connection credentials for the DB regardless of where it's hosted?
For instance, if the DB is on Heroku, then ENV['DATABASE_URL'] give you everything you need. All these details then go in your database.yml as normal (assuming you're using ActiveRecord)
For the record, Heroku do provide Postgres and it's part of their core business.

Resources