If you forget the MySQL/MariaDB root password, you can reset it by starting MySQL/MariaDB in safe mode, which doesn’t require authentication.

How to reset a MySQL root password step by step

The root password for MySQL can be reset and changed in just a few steps directly in the terminal.

Step 1: Shutdown database

First, shut down the database. Choose the option that suits your system. In both cases, the sudo command is used to act as a superuser:

Managed Databases
Time-saving database services
  • Enterprise-grade architecture managed by experts
  • Flexible solutions tailored to your requirements
  • Leading security in ISO-certified data centers

Step 2: Start MySQL in safe mode

Then restart the database in safe mode to perform a MySQL root password reset:

sudo mysqld --skip-grant-tables --skip-networking --pid-file=/tmp/mysqld-reset.pid &

You can now access MySQL as the root user without entering a password.

mysql -u root

Step 3: Set a new MySQL root password

In the next step, you can change your MySQL root password to a new, secure password by using the following command:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'New!Secure!Password';

Then reload the privilege tables:

FLUSH PRIVILEGES;

Step 4: Exit and restart MySQL

Exit the MySQL client:

quit;

Then exit MySQL:

mysqladmin -u root -p shutdown

Enter the root password you created in the previous step.

Next, restart the database in normal mode. Once again, use the command that matches your system:

  • Ubuntu/Debian: sudo systemctl start mysql
  • CentOS/Red Hat: sudo systemctl start mysqld
Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flexibility with no minimum contract
  • 24/7 expert support included

How to reset a MariaDB root password step by step

You can also change your root password in MariaDB in just a few steps. This works similarly to MySQL.

Step 1: Shut down the database

The first step with MariaDB is to shut down the database. You can do this with the following command:

sudo systemctl stop mariadb

Step 2: Start MariaDB in safe mode

To perform a MariaDB root password reset, you must now start the database in safe mode:

sudo mysqld_safe --skip-grant-tables --skip-networking --pid-file=/tmp/mariadb-reset.pid &

You can then log in to MariaDB as the root user. You no longer need a password:

mysql -u root

Step 3: Set a new MariaDB root password

Finally, change the MariaDB root password to a new password of your choice. Use the following command:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'New!Secure!Password';

Reload the privilege tables:

FLUSH PRIVILEGES;

Step 4: Stop and restart MariaDB

Exit the MariaDB client:

quit;

Next, stop MariaDB:

mysqladmin -u root -p shutdown

You can then restart your database in normal mode:

sudo systemctl start mariadb
Was this article helpful?
Go to Main Menu