[RUBY] Mysql2 :: Error :: ConnectionError: Access denied for user'root' @'localhost' (using password: YES)

problem

I'm trying to deploy a portfolio (personal application) created in a local environment to AWS.

When I tried to create a database in a production environment, I got the following error.

error

[ec2-user@ip-00-0-0-000 pfc-master]$ rake db:create RAILS_ENV=production
Access denied for user 'root'@'localhost' (using password: YES)

Solution

① Check the temporary password

[ec2-user@ip-00-0-0-000 ~]$ sudo cat /var/log/mysqld.log | grep "temporary password"

Check the temporary password automatically generated when you installed MySQL in the MySQL log.

② Change password setting rules

If you change your MySQL password without following this procedure, the following error will be displayed.

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

The default rule is ** "Password length must be at least 8 characters, contains both lowercase / uppercase letters, and contains special characters" **.

If the password already set in database.yml meets this rule, there is no need to change the rule, but if it does not meet the rule like" password ", it is troublesome to change the rule. That would disable the check.

$ sudo vi /etc/my.cnf

Open vim with /etc/my.cnf

[mysqld]
validate-password=OFF

To add.

Restart MySQL for the settings to take effect.

$ sudo systemctl restart mysqld.service

Reference: About the troublesome matter when matching the password to Policy

③ Change password

[ec2-user@ip-00-0-0-000 ~]$ mysql_secure_installation

First setup after installing MySQL 5.7 ↑ This article describes how to change the password, so refer to it when changing the password.

Enter the temporary password confirmed in ① in "Initial password" and enter it. Enter the password set in database.yml in "New Password".

Answer all subsequent Yes or No questions with y (Yes).

$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:Enter the initial password

The existing password for the user account root has expired. Please set a new password.

New password:Enter new password

Re-enter new password:Enter the same new password again

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:Enter a new password according to the policy

Re-enter new password:Enter the new password again

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user, a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Reference article

-Where is the root password when I enter mysql on CentOS? It became --First setup after installing MySQL 5.7 -Mysql 5.7 * About the troublesome matter when matching the password to Policy

Recommended Posts

Mysql2 :: Error :: ConnectionError: Access denied for user'root' @'localhost' (using password: YES)
Access denied for user'root' @'localhost' (using password: NO) Corrective action for error
Access denied for user'root' @'localhost' (using password: YES) and gave up
Building a Docker environment on EC2 (ArgumentError: Missing `secret_key_base`, Mysql2 :: Error :: ConnectionError: Access denied for user'root'@ '172.18.0.4' (using password: NO) solution)
Resolve Mysql2 :: Error :: ConnectionError