DB error on deploying with Heroku

In this article, I will explain in an easy-to-understand manner how I got to solve the error that occurred when deploying on Heroku.

Introduction

-The deployed application will be created with the TECH_CAMP curriculum.

・ Because the poster is a beginner, he / she may post incorrect information. At that time, I would appreciate it if you could point out without any refraining.

Steps taken for deployment

  1. Install Heroku CLI → To access Heroku from the terminal. Execute the following command in the terminal.
brew tap heroku/brew && brew install heroku
  1. Log in to Heroku You can log in by using the following command.
heroku login --interactive

~~ 3. Install rails_12factor Gem. ~~ ~~ → A Gem required to run a Rails application on Heroku. ~~

Rails_12factor gem is no longer needed in Rails 5 and later. </ b>

For details, see the Qiita article here.

  1. Create an application on Heroku.
heroku create application name
  1. Make MySQL available on Heroku. → Heroku's default DB is PostgreSQL. Since this Rails application built a DB with MySQL, it must be set to be applied.
heroku addons:add cleardb
  1. You can now use MySQL on Heroku, but you need to change the settings for Gem as well.
heroku_cleardb=`heroku config:get CLEARDB_DATABASE_URL`

Now you can assign the URL of the ClearDB database to a variable called heroku_cleardb.

heroku config:set DATABASE_URL=mysql2${heroku_creardb:5}

DATABASE_URL was reset by config: set.

  1. Set the value of master.key using environment variables. → For security reasons, master.key is not managed by Git, so it cannot be deployed on Heroku as it is. By setting the value of taster.key as an environment variable, it can be used on Heroku.
heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
  1. Push the application.
git push heroku master
  1. Run the migration on Heroku. → By using the heroku run command, you can also use the rails command on Heroku.
heroku run rails db:migrate
  1. Confirm the publication.
heroku apps:info

Was executed to move to the displayed URL.

Error and hypothesis that occurred

error massage

When transitioning to the URL, the above error screen appeared.

Therefore, I returned to the terminal and checked the error log.

heroku logs --tail

The error log was displayed with the above command. The first thing I noticed was

2020-11-11T22:07:09.588453+00:00 (Omitted) dyno=web.1 connect=14ms service=35ms status=500 bytes=1827 protocol=https

Since status = 500, it can be seen that this is a server-side problem.

If you take a closer look,

 Mysql2::Error::ConnectionError (Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)):

So, isn't MySQL not recognized on Heroku? I thought </ b>.

Solved based on hypothesis

Looking back at the terminal code, I also found something strange about the MySQL settings.

nayuta@MacBookPro-Nayuta ajax_app % heroku_cleardb='heroku config:get CLEARDB_DATABASE_URL'
nayuta@MacBookPro-Nayuta ajax_app % heroku config:set DATABASE_URL=mysql2${heroku_creardb:5}
Setting DATABASE_URL and restarting ⬢ ajax-app-30306... done, v5
DATABASE_URL: mysql2

DATABASE_URL is not stored correctly.

My result


DATABASE_URL: mysql2

Correct result


DATABASE_URL: mysql2://000000000000:[email protected]/heroku_aaa00000000?reconnect=true

I thought there was no mistake here, but looking back at the work done in step 6, there was no misspelling of the command, and it seemed that it was entered correctly.

My input


heroku_cleardb='heroku config:get CLEARDB_DATABASE_URL'

Correct input


heroku_cleardb=`heroku config:get CLEARDB_DATABASE_URL`

I glared at the code, looked it up, and finally found out.

Where it should have been enclosed in `` (back quotation marks), it was enclosed in'' (single quotation marks).

It was a very simple mistake, but I was distracted by the spelling mistakes and was late to notice.

nayuta@MacBookPro-Nayuta ajax_app % heroku_cleardb=`heroku config:get CLEARDB_DATABASE_URL`
nayuta@MacBookPro-Nayuta ajax_app % heroku config:set DATABASE_URL=mysql2${heroku_cleardb:5}
Setting DATABASE_URL and restarting ⬢ ajax-app-30306... done, v12
DATABASE_URL: mysql2://bf691ece578431:[email protected]/heroku_b4193d2f6e19e50?reconnect=true

If you enclose it properly in ``, the URL reset is completed.

However, even if you push it as it is, the following error message will appear, so

Everything up-to-date

I made an empty commit and had it pushed together.

 git commit --allow-empty -m "Empty commit"
 git push heroku master 

It seems that it was pushed normally, so when I transitioned to the URL, the created application was deployed.

Summary

I was careful about spelling mistakes because I often get errors due to typos, but I never imagined that I would make typos in'' and ``. It was a very terrible mistake, but I was fortunate because I gained more experience points saying "I made an error because of such a mistake!".

Recommended Posts