skip to Main Content

In today’s article, I’m going to show you the process to reset MySQL/MariaDB root passwords. This should work for all versions, but your mileage may vary.

This trick is especially useful if you run into the following error in the mysql/mariadb error log while trying other reset methods:

[Server] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
[Server] Aborting


1. Create the password reset file

Create the init file:

nano /root/mysql-init

Paste the following command into the file (change 123abc to whatever you want the new password to be):

ALTER USER 'root'@'localhost' IDENTIFIED BY '123abc';

Save the file and exit the editor


2. Start MySQL/MariaDB with the init file

Make sure mysqld is stopped:

ps aux | grep sql

Should display something similar to the following:

root     16632  0.0  0.0 112704   932 pts/0    S+   18:02   0:00 grep --color=auto sql

If it display something like this instead:

mysql     2938  0.0  0.0 113308  1556 ?        Ss   Dec04   0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     3259  2.8  2.7 1580000 217248 ?      Sl   Dec04  87:15 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --open-files-limit=4096 --pid-file=/var/run/mariadb/ --socket=/var/lib/mysql/mysql.sock
root     16582  0.0  0.0 112708   928 pts/0    S+   18:01   0:00 grep --color=auto sql

Then kill the mysqld process by running the following (replace the PID with the correct one from your output):

kill 3259

Verify that the mysqld process has been stopped, then start mysqld using the following command:

mysqld --user=root --skip-grant-tables --init-file=/root/mysql-init &

If all went smoothly, that should log you into MySQL/MariaDB and you’re done!

Don't be shy, leave a reply!