Upon installing MySQL server (MariaDB 8.0) on my CentOS 7 using yum. And once started the service and while connecting to the database, 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)
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
To 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';