How to Reset the MySQL or MariaDB Root Password

If you lose the password for the MySQL/MariaDB root user, you can reset it by restarting MySQL/MariaDB in Safe Mode, which does not require a password.

First, shut down the database with the command:

  • Ubuntu/Debian: MySQLsudo service mysql stop
  • Ubuntu/Debian: MariaDBsudo service mariadb stop
  • CentOS/Red Hat: MySQLsudo systemctl stop mysql
  • CentOS/Red Hat: MariaDBsudo systemctl stop mariadb
Be secure. Buy an SSL certificate.
  • Secures data transfers
  • Avoids browser warnings
  • Improves your Google ranking

Start MySQL/MariaDB in Safe Mode with the command:

mysqld_safe --skip-grant-tables &

You can now log in to MySQL/MariaDB as root without having to enter a password:

mysql -u root mysql

Reset the MySQL/MariaDB password with the following command. Change my-new-password to a secure password.

UPDATE user SET password=PASSWORD("my-new-password") WHERE user='root';

Reload the privilege tables:

FLUSH PRIVILEGES;

Exit the MySQL/MariaDB client:

quit;

Shut down MySQL/MariaDB:

mysqladmin -u root -p shutdown

You will need to enter the password for root which you set in the previous step.

Finally, restart the database in normal mode:

  • Ubuntu/Debian: MySQLsudo service mysql start
  • Ubuntu/Debian: MariaDBsudo service mariadb start
  • CentOS/Red Hat: MySQLsudo systemctl start mysql
  • CentOS/Red Hat: MariaDBsudo systemctl start mariadb
$1 Domain Names – Grab your favorite one
  • Simple registration
  • Premium TLDs at great prices
  • 24/7 personal consultant included
  • Free privacy protection for eligible domains
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top