How to Create a New Ruby on Rails Application Using MySQL Instead of SQLite

When starting a new Ruby on Rails project, you might notice that the default database is set to SQLite. While SQLite is great for development, many developers prefer to use MySQL for its robustness and feature set. If you’re looking to create a new Rails application using MySQL, you’ve come to the right place! In this post, we’ll walk you through the steps to set up a Ruby on Rails application with MySQL as the database adapter. Let’s dive in!

Step 1: Install MySQL

Before we can configure MySQL for our Rails app, we need to ensure that MySQL is installed on your system. You can install it via your operating system’s package manager. For example, on macOS, you might use Homebrew:

brew install mysql

Make sure to start the MySQL service after installation:

brew services start mysql

If you are on a different operating system, refer to the respective documentation to install MySQL.

Step 2: Create a New Rails Application

Now that we have MySQL installed, let’s create a new Ruby on Rails application. You can do this by running the following command in your terminal:

rails new your_app_name -d mysql

The -d mysql flag specifies that MySQL should be used as the database for this application.

Step 3: Configure Database Settings

After creating your Rails application, you need to configure it to connect to your MySQL database. Open the config/database.yml file in your project directory. Here’s where you will adjust the settings:

development:
  adapter: mysql2
  database: db_name_dev
  username: your_username
  password: your_password
  host: localhost
  socket: /tmp/mysql.sock

Explanation of Parameters:

  • adapter: This specifies which database adapter to use. For MySQL, it should be mysql2.
  • database: Name of your database. You can choose any name you like for your development database.
  • username: Your MySQL username. Ensure it has the right permissions for the database.
  • password: The password for your MySQL user (leave blank if none).
  • host: The server where MySQL is running. “localhost” is the default.
  • socket: This is optional but typically points to the socket location for MySQL.

Step 4: Update Your Gemfile

To use MySQL with Rails, you’ll need the mysql2 gem. Open your Gemfile and add or uncomment the following line:

gem 'mysql2'

If you are using JRuby, you would use:

gem 'activerecord-jdbcmysql-adapter'

Then run the following command to install the necessary gems:

bundle install

Step 5: Create and Migrate the Database

Now that we’ve set everything up, let’s create the database and run any necessary migrations. Execute the following commands in your terminal:

rails db:create
rails db:migrate

These commands will create your database, applying any existing migrations to ensure your database schema is up to date.

Conclusion

And there you have it! You’ve successfully configured a new Ruby on Rails application to use MySQL instead of SQLite. This setup allows you to take full advantage of the capabilities of MySQL, providing a solid foundation for building your applications. Happy coding!