Configuring a default root password for MySQL/MariaDB

On many OS distributions, MySQL and MariaDB are initialized with an unset root password, or a password that is logged into the MySQL/MariaDB error log. Use the following procedure to set a root password.

In the case where a root password has been set, you may find a temporary password in the MySQL/MariaDB error log. To check, attempt to log in as root using the command:

mysql -uroot

If this does not work, search the MySQL/MariaDB error log for the word ‘temporary’, like the example below. Your error log may be in a different location based upon the OS version:

grep ‘temporary’ /var/log/mariadb/mariadb.log

Based upon that response, you should be able to log in. For example, if the password you found was ‘abcdefghijk’, you should be able to login with the following:

mysql -uroot -pabcdefghikj

To change the root password, type the following at the MySQL/MariaDB command prompt:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd';
flush privileges;
exit;

Store the new password in a secure location.