PHP Composer is a package management system for PHP which prevents users from having to "reinvent the wheel" when it comes to commonly-used website components like user authentication or database management. Composer is modelled on other popular package management systems like Ruby's Bundler.
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.
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.
$1 Domain Names
Register great TLDs for less than $1 for the first year.
Why wait? Grab your favorite domain name today!
- 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.
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:
Dedicated Server from IONOS
Hardware meets cloud: dedicated server with cloud integration and per-minute billing, including a personal assistant!
Edit the application's configuration file
Next, move into the directory which Ruby on Rails created for the application:
Edit the config/database.yml file:
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:
Save and exit the file.
Create the new application databases
Use the following rake command to create the databases for your application:
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
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 220.127.116.11 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