Fix Cannot Log in MySQL via Password

1. Problem description

Newly installed MySQL, unable to log in using phpMyAdmin, reported:

Text
1
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

2. Solution

2.1, configure password-free login

In the configuration file my.cnf add:

Text
1
skip-grant-tables

Then restart MySQL:

Bash
1
/etc/init.d/mysqld restart

2.2, delete empty users

implement:

Bash
1
mysql -uroot -p<password>

Change <password> to the root password, log in to the MySQL command line, and execute:

sql
1
2
3
USE mysql;
Delete FROM user Where User='' and Host='localhost';
FLUSH PRIVILEGES;

2.3, set permissions

If MySQL 5.6 and below:

sql
1
2
UPDATE mysql.user set password=password('root <password>') WHERE user='root' AND host='localhost';
FLUSH PRIVILEGES;

<password> to root password.

If MySQL 5.7 and above:

sql
1
2
3
update user set authentication_string='' where user='root';
ALTER user 'root'@'localhost' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;

<password> to root password.

2.4, close password-free login

In my.cnf delete

Text
1
skip-grant-tables

Restart the database:

Bash
1
/etc/init.d/mysqld restart

Fix Cannot Log in MySQL via Password

https://blog.tsinbei.com/en/archives/707/

Author
Hsukqi Lee
Posted on

2023-05-18

Edited on

2023-05-18

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet