Access denied for user'root' @'localhost' (using password: NO) Corrective action for error

Details of the error

After doing rails s, when I looked at the browser, the following error occurred ↓

Mysql2::Error::ConnectionError Access denied for user 'root'@'localhost' (using password: NO)

in short, I can't access it because I don't have a password! about it.

Error resolution

There are two ways to resolve the error.

  1. Write the password set in MySQL in database.yml.
  2. Empty the root user password.

When developing individually, 1 is fine, but when developing as a team, it is necessary for the team to share the password, which is a little troublesome, so this time we will go into detail about 2. I will also briefly describe 1.

1. Write solid in database.yml.

database.yml


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

The easiest solution is to write your password in the above section. However, if you push it to a version control system such as GitHub, the password will be exposed, so you need to manage the password in the .env file and write it in .gitignore.

2. Empty the root user password.

Start MySQL.

$ mysql.server start

Log in to MySQL.

$ mysql -u root -p
Enter password: #Enter the current PW

Select the database you want to use.

mysql> use mysql;

Empty the password. Notice that in MySQL 5.7.6 the password column name changed from password to authentication_string.

mysql> update user set authentication_string='' where user='root';

If the above code works, you'll see that you've made the change.

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Close MySQL if the changes are successful.

mysql> exit

Stop MySQL once for the changes to take effect.

$ mysql.server stop

Start it again. I think it's okay to restart without stopping.

$ mysql.server start

And make sure you can log in without a password.

$ mysql -u root

Once you are logged in, close MySQL again and start the server.

$ rails s

And when I open the browser ... スクリーンショット 2020-11-05 17.05.11.png

The error has been resolved successfully. Thank you for your hard work!

Reference site

Daily Memorandum

Recommended Posts

Access denied for user'root' @'localhost' (using password: NO) Corrective action for error
Mysql2 :: Error :: ConnectionError: Access denied for user'root' @'localhost' (using password: YES)
Access denied for user'root' @'localhost' (using password: YES) and gave up