MySQL is one of the most widely used relational database management system, and one of the most widely used open source database system. It is released under GNU General Public License. SQL is abbreviated to Structured Query Language. If you are looking for the scalability, centralization and control of your applications while you are using Ruby on Rails. So you will need to use MySQL with them and for that we will show you within this article how to set up a Ruby on Rails development environment to enable your applications use the MySQL database on the Ubuntu 14.04 server. We will discover how to install the MySQL and later how to develop Ruby on Rails application using MySQL too.
Before starting the installation there are some required things before: so for that you will need to have the Ruby on Rails development environment already installed and also to have access to a “superuser” or “sudo” to be able to install the MySQL database system.
Installing the MySQL
Before starting installing the MySQL database software type the following command in order to update the “apt-get”:
sudo apt-get update
Then use the following command to start the installation, while you will be asking to choose a password for the MySQL root user:
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
After installing the MySQL database software, we need to create the database directory where our information will be stocked. For that, you have just to use the following command:
sudo mysql_install_d
Now it is recommended to add some security script by using the following command while you be asked also to add the same password entered previously. Of course you can change this password:
sudo mysql_secure_installation
By default you can keep the some chosen values by hitting the “Enter” key through each prompt. Now your MySQL is installed but it is missed to install the MySQL gem.
Installing the MySQL Gem
In order to enable Rails application to connect to the MySQL database, it is important to install the MySQL adapter. So you have to install the “mysql2” using:
gem install mysql2
Develop Ruby on Rails application using MySQL
It is recommended to create a new Rails application in the home directory by using “-d mysql” in order to setting MySQL as the used database:
cd ~rails newappname -d mysql
Then move to the application’s directory using the following command:
cd appname
Configure database connection:
Now you have to configure the application’s connection for that you will need the chosen password during the previous steps. So start by opening your application’s database configuration file in a text editor. For us we have chosen vi by typing the following command:
vi config/database.yml
Then search the “password” under the default section, once you find it add your password and save and exit the application.
Create application database:
Now, you have to create the application’s development and test database by typing the following command:
rake db:create
By default, two databases will be created within your MySQL. It means that if you have an application called “myapp” so the created databases will be: “myapp_development” and “myapp_test”.
Test configuration
Now after installing and configuring the recommended things, it is time to test your created application and check if it uses the MySQL database already installed. So you have just to run it. For that use the following command under the development environment which will run your Rails application on your local host under the port 3000:
rails server
In the other hand, if you want to have access to your Rails application through a web browser which is on a remote server, so it is recommended to bind it to the public IP address of your server. Use the following command after finding the public IP address of your server:
rails server --binding=server_public_IP
Now you will have access to your Rails application through a web browser and if there is “Welcome Abroad” Ruby on the Rails page, so your application is configured and connected to the MySQL database. For that you have to use the following command:
http://server_public_IP:3000
Conclusion
Within this article you are able to start developing Ruby on Rails application using MySQL database on Ubuntu 14.04. We hope that we helped you and gave responses to your requests.