I get Mysql2 :: Error :: ConnectionError in the production environment

If you push to the production environment (heroku) and go to the new registration screen or login screen, an error will occur

ruby '2.6.5' gem 'rails', '~> 6.0.0' gem 'mysql2', '0.5.3'

This time I created an application from scratch, and since I had a framework to some extent, I pushed it to heroku. The top screen was displayed safely, but when I go to the new registration screen or login screen, an error occurs, so I will write the method to read the error statement and resolve it.

First of all, from the result, it was because the database and the application were not connected well.

Create a production database

   %heroku create application name

Now create a database. Create and confirm with the code below.

   % git config --list | grep heroku

If anything other than fatal: not in a git directory is displayed, it's OK.

Make the database Mysql on heroku

In Heroku, the database settings used are PostgreSQL by default. This time, we will configure the settings to use the same MySQL as the development environment.

Added ClearDB add-on

A tool for using MySQL. An add-on provided by a database service called ClearDB that allows you to use MySQL on Heroku.

   % heroku addons:add cleardb

You have now set your database to MySQL.

Next, since the version of MySQL needs to match the version of the corresponding Gem, execute the following command.

   % heroku config | grep CLEARDB_DATABASE_URL

CLEARDB_DATABASE_URL is created. In this application Since we are using a gem called mysql2, we need to change the mysql: // part to something like mysql2: //.

 heroku config:set DATABASE_URL='mysql2://000000000000:[email protected]/heroku_aaa00000000?reconnect=true'

、mysql://  → mysql2://

//000000000000:[email protected]/heroku_aaa00000000?

The above part is different for each application.

I wasn't able to do mysql: // → mysql2: // here. So it was said that the database could not be connected well.

Do I have to delete the database to reset it? I searched for things like deleting CLEARDB_DATABASE_URL, but it wasn't.

CLEARDB_DATABASE_URL can be reset.

   % heroku config:set DATABASE_URL='mysql2://[Application CLEARDB_DATABASE_URL]=true'

I solved it here.

And after this

   % git push heroku master

Push to heroku,

   % heroku run rails db:migrate

to hold. It's a pattern that beginners tend to forget, so don't forget to migrate.

I accessed the application and checked the new posting screen, but I was able to access it safely.

Recommended Posts

I get Mysql2 :: Error :: ConnectionError in the production environment
(Capistrano) After deploying, I get a We're sorry… error in the production environment.
[Error] The app is not displayed in the production environment
Error in production environment (The asset "~" is not present in the asset pipeline.)
I want to display the images under assets/images in the production environment
[Rails] Reset the database in the production environment
Display the background image in the production environment
[Rails] About the error that the image is not displayed in the production environment
500 Internal Server Error occurs in Rails production environment
I want to get the value in Ruby
Resolve Mysql2 :: Error :: ConnectionError
[Java] Get the file in the jar regardless of the environment
[Rails] How to delete production environment MySQL data after putting it in the development environment
Create a database in a production environment
I don't see an error in Rails bundle install ... the solution
[Rails] How to reset the database in production environment (Capistrano version)
[Android] I want to get the listener from the button in ListView
Check MySQL logs in Docker environment
Spring Boot + MyBatis I get this error if I don't set the database
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I had to lower the PHP version in Docker + Composer + Laravel environment
I get got errors: User must exist in the unit model test
How to reflect seeds.rb in production environment
I tried the new era in Java
Edit Mysql with commands in Docker environment
Creating a Servlet in the Liberty environment
How to get the date in java
Image is not displayed in production environment
I tried the AutoValue library in Intellij
Get the error message using the any? method
A solution to an error that makes you angry that you are not following the MySQL default setting ONLY_FULL_GROUP_BY in a production environment and it is not unique.
When I renew the certificate with CircleCI × fastlane, I get an exit status: 65 error.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
Solution when you get the error Webpacker :: Manifest :: MissingEntryError in Devise :: Sessions # new
When I try to run docker-compose, I get the error "Cannot locate specified Dockerfile: Dockerfile"
[When using MiniMagick] A memorandum because I stumbled in the CircleCI test environment.
MYSQL Lock TimeOut error I don't remember while running RSpec on the container
For those who get the error Unprintable ASCII character found in source file
Sample code to get the values of major SQL types in Java + MySQL 8.0
I tried to organize the session in Rails
rails tutorial About account activation in production environment
When you get lost in the class name
I get an error when adding a dependency
Rails5.1 + puma SSL connection in local production environment
Get your version number in the Android app
MySQL error in Spring Unknown column'Value 1'in'field list'
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
I stumbled when I tried using neo4j in the jenv environment, so make a note
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
[JavaScript] I can't get the response body at the time of error with axios (ajax)
Easily monitor the indoor environment ~ ⑨ Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / Pi4J) ~
I have a question. I get an error when playing a video in Listview on android.
# 1_JAVA I want to get the index number by specifying one character in the character string.