How to change MariaDB user password

Have you or one of your MariaDB users forgotten the password to a MariaDB account? It’s very easy to reset a MariaDB user password on Linux, and we’ll show you the commands and step by step instructions below.

Resetting the MariaDB root password requires a different set of instructions, which we also cover below. Depending on which account you need to change the password for (a normal user or root), follow the appropriate section below.

In this tutorial you will learn:

  • How to change MariaDB user password
  • How to change MariaDB root password
Changing MariaDB user password

Changing MariaDB user password

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software MariaDB
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Change MariaDB User Password

Open a command line terminal on your machine and follow the steps below to change the password to a normal MariaDB user account (not root).

  1. Start by logging into MariaDB as the root user.
    $ mariadb -u root -p
  2. Next, switch to the mysql database.
    MariaDB [(none)]> use mysql;

  3. Switch the user’s password by using the following syntax (replace values where necessary).
    MariaDB [mysql]> ALTER USER 'user'@'localhost' IDENTIFIED BY 'new_password';
  4. Lastly, flush privileges and exit MariaDB.
    MariaDB [mysql]> FLUSH PRIVILEGES;
    MariaDB [mysql]> exit

That’s all there is to it. Make sure the changes worked by trying to login from your shell with the new password.

$ mariadb -u username -p

Change MariaDB Root Password

The process for changing the root password in MariaDB is a bit more involved. The step by step instructions below will help you to change the password or reset it in case you’ve forgotten it.

  1. Let’s start by stopping the currently running MariaDB database.
    $ sudo systemctl stop mariadb
  2. Start the database process again, but this time with the --skip-grant-tables option, which will allow us to connect to the database without needing a password. The & just tells Linux to background the process. Alternatively, you could omit the ampersand and just open a new terminal window for the next few steps.
    $ sudo mysqld_safe --skip-grant-tables --skip-networking &
  3. Login to MariaDB as root. You won’t be asked for a password.
    $ mariadb -u root

  4. Flush privileges and then change the root password using the following command. Replace our password example with whatever you’d like your password to be.
    MariaDB [(none)]> FLUSH PRIVILEGES;
    MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password_here';
    MariaDB [(none)]> exit
  5. Gracefully terminate the current mysqld process, and then start your MariaDB server back up.
    $ sudo pkill mysqld 
    $ sudo systemctl start mariadb
  6. To verify everything works, try to login to MariaDB as root, while specifying the password you just set.
    $ mariadb -u root -p

Closing Thoughts

In this guide, we saw how to change/reset a user password in MariaDB. We also saw how to reset the root password, which involves taking the database offline and relaunching it with different permissions. If you happen to forget the password again, you’ll know where to find this guide.

Comments and Discussions
Linux Forum