Ruby on Rails uses SQLite3 as its default database. While Sqlite works great with Rails, some times it may not be sufficient for your Rails application. If you want scalability, concurrency, centralization, and control, you may want to try more robust databases like MySQL or PostgreSQL. In this guide, we will see how to use MySQL with Ruby on Rails application in Ubuntu Linux.
Table of Contents
1. Install MySQL in Ubuntu
MySQL is available in the default repositories of most Linux and Unix-like distributions.
To install MySQL on Debian, Ubuntu and its derivatives, run:
$ sudo apt install mysql-server mysql-client libmysqlclient-dev
The libmysqlclient-dev
package provides the necessary files to compile the mysql2
gem. Ruby on Rails uses mysql2
gem to connect to MySQL when you setup a Rails application.
1.1. Setup MySQL Root password
Next, setup MySQL root user password by running the following command:
$ sudo mysql_secure_installation
Enter "y" to setup VALIDATE PASSWORD component:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
Choose the level of password validation. The available password validations are low, medium and strong. Here, I choose low level password validation by entering zero (0).
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Next, enter MySQL Root password twice. The password should be strong and should have minimum 8 characters. Press Y to continue:
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
Next, you will be asked a series of questions. Type y and hit ENTER for each question. This will remove anonymous user, disallow root user login remotely and remove test database.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!
Done! We have set MySQL root user's password.
1.2. Install MySQL driver
Install MySQL driver named mysql2
gem that allows the Rails application to connect to the MySQL database.
$ gem install mysql2
All Done! The Rails applications can now connect to the MySQL database server.
Let us move forward and create a new Rails application with MySQL database.
2. Use MySQL with Ruby on Rails application
To create a new Rails application called ostechnixapp
and use MySQL database as its default database, run:
$ rails new ostechnixapp -d mysql
This will create a Rails application called ostechnixapp in a directory named ostechnixapp
and install the gem dependencies that are already mentioned in Gemfile
using bundle install
.
After creating the ostechnixapp
application, switch to its directory:
$ cd ostechnixapp/
Edit the application's database configuration file:
$ vi config/database.yml
Under the default section, replace MySQL root
user password with the one that you created earlier:
[...] default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: Password123#@! socket: /var/run/mysqld/mysqld.sock [...]
Save and close the file.
Now create the new database for your Rails application using command:
$ rake db:create
This will create two databases with your Rails application's name. For example, if the application name is ostechnixapp, then it will create ostechnixapp_development
and ostechnixapp_test
.
You can verify it if the databases are successfully created from MySQL as well.
Login to MySQL as root
user:
$ mysql -u root -p
Verify if the databases are created by entering the following command:
mysql> show databases;
Sample output:
+--------------------------+
| Database |
+--------------------------+
| information_schema |
| mysql |
| ostechnixapp_development |
| ostechnixapp_test |
| performance_schema |
| sys |
+--------------------------+
6 rows in set (0.01 sec)
mysql>
Exit from the MySQL prompt.
Now start your Rails web server using command:
$ rails server -b 0.0.0.0
This will start your Rails application on default port 3000
. Here, we passed the argument -b 0.0.0.0
in order to access the Rails application from any machine on the local area network.
You can access your Rails test page on your web browser by navigating to the URL: http://ip-address:3000
If you want to use different port, pass -p flag along with the port number like below:
$ rails server -b 0.0.0.0 -p 8080
Now, you can access your Rails application using this URL: http://ip-address:8080
3. Delete Rails application
If you have used the default database which is SQLite, you can delete the Rails application by simply deleting its app directory.
$ rm -fr ostechnixapp
If you've used MySQL, first you need to drop the related databases of your application using command:
$ rake db:drop
And then delete the application directory:
$ rm -fr ostechnixapp
Conclusion
In this tutorial, we discussed why we need to use client/server model databases for our Rails application and how to create a Rails application with MySQL database. We've also seen how to delete a Rails application.
If several copies of the Rails application running at the same time, several applications and users using the same database, you should move away from Sqlite and try client/server model databases like MySQL. If you want local data storage for individual applications, Sqlite is more than enough.
Related read: