reset mariadb root password

Posted on in linux
Last edited on
Tags: mariadb

Some few days ago I repeated one of the more common ‘mistakes’ I find myself making every once in a while: I setup a vm to test something out, install mariadb, set a root password and then immediately forget it.

What follows is usually me searching for the notes I made only to find out that there are none. 😅

So here’s my notes on resetting the root password on mariadb, so that I don’t have to search for them and maybe remember them in the future:

⚠️ This has last been tested with Mariadb 10.5

Necessary steps

  1. Stop the mariadb service systemctl stop mariadb.service
  2. Start mariadb in safe mode without networking: mysqld_safe --skip-grant-tables --skip-networking &
  3. Login to the service: mysql
  4. Reset the password: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newrootpassword');
  5. Flush privileges: FLUSH PRIVILEGES
  6. Logout from service: quit
  7. Stop mariadb: mysqladmin -uroot -p shutdown

For later versions this should still work, because from what I recall, mysqld_safe is already a symlink to mariadbd-safe for compatibility reasons. It takes some command line arguments and forwards the rest to mysqld, which also is a symlink to mariadbd. And last but not least, mysqladmin is a symlink to mariadb-admin.

I believe you could as well call these directly, but I have yet to test this.

Bonus

If you want to make sure that the password actually changed, you can run the following query before and after step 4 :
SELECT * FROM mysql.global_priv WHERE User = 'root';