[Ruby] Resolves Mysql2::Error::ConnectionError

less than 1 minute read

Environment/Version

I’m doing it locally Ruby v2.5.1 Rails v5.2.4 MySQL v8.0.19

what happened?

After creating rails project, when I try `rails s’, I encounter such an error

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

How did you solve it?

As you can see from the error statement, you can easily tell if it is “MySQL”.

Since there is a ConnectionError, you can immediately see “Not connected ~”.

Examine and look at database.yml.

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") {5} %>
  username: root
  password:
  socket: /tmp/mysql.sock

  ---Omitted below---

The password in the yml file is missing. During this time, I forgot to set the password in MySQL.

I could connect even if I did not set it before, but it seems that this time it did not connect successfully.

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") {5} %>
  username: root
  password: password # add
  socket: /tmp/mysql.sock

I am relieved when this screen appears. Cute kids

Screenshot 2020-07-09 9.32.37.png

Thank you for reading to the end. I would be happy if it could be of any use.