How to change the MariaDB password for root and regular users
Forgot your MariaDB root password? Without it, you won’t be able to manage users, databases, or perform critical configurations. In this article, you’ll learn not only how to quickly reset the MariaDB root password but also how to reset the password for regular users.
For MySQL please, refer to our separate guide.
The root user is the main administrator of the database. They have full access to all data and settings. If you lose this password, you will not be able to change some settings or execute commands.
All operations will be performed via the command line over SSH. You can find detailed instructions on how to connect to your server using SSH in this article.
Before proceeding, check the contents of the /root/.my.cnf file. Often, it contains the current root password for accessing MariaDB.
To test the connection, use the command:
mysql -u root -p
Then, enter the password from the .my.cnf file.
If the password doesn’t work, follow the instructions below.
Resetting the password
In most commands below, the mysql command will be used instead of mariadb to interact with the MariaDB server. This is because, on some operating systems, such as RHEL-based distributions (RockyLinux, AlmaLinux, etc.), the mariadb command is unavailable. Instead, the mysql command is used for compatibility with MySQL.
Using the mysql command provides universal compatibility regardless of the distribution or implementation of the server.
Step 1: Stop MariaDB
To reset the password, first stop the MariaDB server. Enter the following command:
systemctl stop mariadb
Step 2: Restart the server in safe mode
- For Debian and Ubuntu:
Make sure the directory MariaDB will run from exists and has the correct owner:
mkdir -p /var/run/mysqld/ && chown -R mysql: /var/run/mysqld/