Skip to content

Deploying an app that uses MySQL to Heroku

To push a Ruby on Rails app that uses MySQL to Heroku, you’ll need to follow these steps:

  1. Sign up for a Heroku account if you don’t have one already:

    Go to Heroku’s website (https://www.heroku.com/) and sign up for a free account.

  2. Install the Heroku CLI:

    Download and install the Heroku CLI (Command Line Interface) for your operating system from the Heroku Dev Center (https://devcenter.heroku.com/articles/heroku-cli#download-and-install).

  3. Open a terminal or command prompt and log in to your Heroku account:

    Run the following command and follow the prompts to log in:

    heroku login
  4. Create a new Heroku app:

    Navigate to your Rails app’s root directory in the terminal and run the following command to create a new Heroku app:

    heroku create

    This will generate a new app and provide you with a URL (e.g., https://your-app-name.herokuapp.com).

  5. Add the Heroku MySQL addon:

    Run the following command to add the Heroku MySQL addon to your app:

    heroku addons:create jawsdb

    This will provision a MySQL database for your app on Heroku and set the necessary environment variables.

  6. Configure your Rails app for deployment:

    Make sure your Rails app’s Gemfile includes the mysql2 gem. If it’s missing, add the following line:

    gem 'mysql2'

    Update your Rails app’s config/database.yml file with the appropriate Heroku environment configuration. Replace the existing MySQL configuration with the following code:

    production:
    <<: *default
    url: <%= ENV['JAWSDB_URL'] %>
  7. Commit your changes

  8. Run the following command to deploy your app to Heroku:

    git push heroku main

    This will push your code to Heroku’s Git repository and start the deployment process.

  9. Migrate the database:

    Once the deployment is complete, run the following command to migrate the database on Heroku:

    heroku run rails db:migrate
  10. Seed the database (if needed):

    If your app uses seed data, run the following command to seed the database on Heroku:

    heroku run rails db:seed
  11. Open your deployed app:

    After the migration and seeding are complete, run the following command to open your app in a web browser:

    heroku open

That’s it! Your Ruby on Rails app using MySQL is now deployed on Heroku. You can continue making changes to your local codebase and deploying updates using the git push heroku main command whenever needed.