Access denied for user ‘root’@’localhost’ after installing MySQL [Fix]

Updated on November 2, 2018

I Installed MySQL server (MariaDB 8.0) on my CentOS 7 using yum. Once started the service and when I tried connecting to the database, I got the below error:

# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

MySQL version is as below:

# mysql --version
mysql Ver 8.0.12 for Linux on x86_64 (MySQL Community Server - GPL)

Why do I get access denied error after MySQL installation?

Generally, after installing MySQL the UNIX users will be able to access the database immediately without entering the password. But starting from MySQL version 5.7 and above, the password is set for root user automatically and stored in the error log file. To know the password, run the below command:

# sudo grep 'temporary password' /var/log/mysqld.log
2018-10-16T10:07:24.491219Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: bgMlB#Xl7ls=

Copy the password from the error log and use it to login to MySQL.

# mysql -u root -p

Change the MySQL root password

After your first login to the database, remember to change the root password immediately as shown below.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

Also, learn to recover lost MySQL password here.

Was this article helpful?

Related Articles

Leave a Comment