How to use MySQL With Your Ruby on Rails Application

Leave your reply

Introduction

Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL. SQLite is an excellent alternative to a traditional database like MySQL, but it has some limitations, particularly with regards to concurrency and scaling to a high load, which may make MySQL a better choice for your project.

Requirements

  • A Cloud Server running Linux (Ubuntu 16.04)
  • MySQL installed and running.
  • The root MySQL password.
  • Ruby on Rails installed and running.
  • A basic familiarity with Ruby on Rails.

For more information on installing and using Ruby on Rails, see our article Install Ruby on Rails using rbenv on a 1&1 IONOS Linux Cloud Server.

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.

Add the MySQL Gem

To install the MySQL client and the development libraries, run the following commands:

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

Once the installation is done, install the mysql2 gem, which will allow Rails to connect to MySQL:

gem install mysql2

Configure The Rails Application

The next step is to enable MySQL support in your Ruby on Rails application.

Create the Application

First, create the application using the -d mysql flag:

rails new [application name] -d mysql

For example, the command to create an application 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 application.

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 administrator, 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;

Edit the Application's Configuration File

Next, move into the directory which Ruby on Rails created for the application:

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 Application Databases

Use the following rake command to create the databases for your application:

rake db:create

Test the Configuration

To test the configuration, simply start the rails application and check it in a browser.

From the application'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 application 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.

rails splash page