Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL. SQLite is an excellent al­tern­at­ive to a tra­di­tion­al database like MySQL, but it has some lim­it­a­tions, par­tic­u­larly with regards to con­cur­rency and scaling to a high load, which may make MySQL a better choice for your project.

Note

All of the commands in this tutorial must be issued as the Rails user. This is the user account which you used to install and run Ruby on Rails.

Cheap domain names – buy yours now
  • Free website pro­tec­tion with SSL Wildcard included
  • Free private re­gis­tra­tion for greater privacy
  • Free Domain Connect for easy DNS setup

Re­quire­ments

  • A Cloud Server running Linux (Ubuntu 16.04)
  • MySQL installed and running.
  • The root MySQL password.
  • Ruby on Rails installed and running.
  • A basic fa­mili­ar­ity with Ruby on Rails.

Add the MySQL Gem

To install the MySQL client and the de­vel­op­ment libraries, run the following commands:

sudo apt-get update
sudo apt-get install mysql-client libmysqlclient-dev

Once the in­stall­a­tion is done, install the mysql2 gem, which will allow Rails to connect to MySQL:

gem install mysql2

Configure the Rails ap­plic­a­tion

The next step is to enable MySQL support in your Ruby on Rails ap­plic­a­tion.

Create the ap­plic­a­tion

First, create the ap­plic­a­tion using the -d mysql flag:

rails new [application name] -d mysql

For example, the command to create an ap­plic­a­tion named my-app is:

rails new my-app -d mysql

The -d flag tells Ruby on Rails that you will be using MySQL for this ap­plic­a­tion.

Root MySQL password

For the next step, you will need the root MySQL password. By default, this is the same as the password for the root server user when the server was built.

To log in to MySQL as an ad­min­is­trat­or, enter the following command:

mysql -u root -p

You will be prompted to enter a password.

If the password is correct, you will be logged into the MySQL client. You can exit back to the command line with:

quit;
Dedicated Server
Per­form­ance through in­nov­a­tion
  • En­ter­prise hardware
  • Con­fig­ur­able hardware equipment
  • ISO-certified data centres

Edit the ap­plic­a­tion's con­fig­ur­a­tion file

Next, move into the directory which Ruby on Rails created for the ap­plic­a­tion:

cd my-app

Edit the config/database.yml file:

nano config/database.yml

Scroll down to the password: line in the default section and add the root MySQL password:

password: [MySQL password]

For example, if the root MySQL password is "XPmMxZf", edit the line to read:

password: XPmMxZf

Save and exit the file.

Create the new ap­plic­a­tion databases

Use the following rake command to create the databases for your ap­plic­a­tion:

rake db:create

Test the con­fig­ur­a­tion

To test the con­fig­ur­a­tion, simply start the rails ap­plic­a­tion and check it in a browser.

From the ap­plic­a­tion's directory, use the command:

bin/rails s --binding=0.0.0.0
Note

Binding the server to 0.0.0.0 allows you to view the ap­plic­a­tion using your server's public IP address.

The server should respond with:

[user@localhost my-app]$ bin/rails server
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

Switch to a browser and visit "http://your-ip-address:3000". For example, if your IP address is 198.162.0.1 you would go to http://198.162.0:3000.

If all is well and Rails is able to connect to MySQL, you will see the default Rails welcome page.

Go to Main Menu