# How to use MySQL with your Ruby on Rails application

[Ruby on Rails](https://www.ionos.com/digitalguide/websites/web-development/ruby-on-rails-creating-your-own-with-less-code/ "Ruby on Rails: creating your own with less code") 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.

---

### 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.

---

## 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.

## Add the MySQL Gem

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

```none
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:

```none
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:

```none
rails new [application name] -d mysql
```

For example, the command to create an application named my-app is:

```none
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:

```none
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:

```none
quit;
```

### Edit the application's configuration file

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

```none
cd my-app
```

Edit the config/database.yml file:

```none
nano config/database.yml
```

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

```none
password: [MySQL password]
```

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

```none
password: XPmMxZf
```

Save and exit the file.

### Create the new application databases

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

```none
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:

```none
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:

```none
[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.

[![Image: Rails Splash Page](https://www.ionos.com/digitalguide/fileadmin/_processed_/8/8/csm_rails-splash-page_ba2b63ac24.webp "Rails Splash Page")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/rails-splash-page.jpg)


This is a markdown version of: [https://www.ionos.com/digitalguide/server/know-how/use-mysql-with-ruby-on-rails/](https://www.ionos.com/digitalguide/server/know-how/use-mysql-with-ruby-on-rails/) for AI/LLM consumption.