Skip to content
Follow
Ubuntu Linux

How to Reset MySQL or MariaDB Root Password

Richard
Written by
Richard
Oct 10, 2021 Updated Jul 14, 2026 4 min read
How to Display Seconds on Ubuntu Top Menu Clock
How to Display Seconds on Ubuntu Top Menu Clock

Resetting the MySQL or MariaDB root password on Ubuntu Linux involves stopping the database, restarting it in a special mode that skips security checks, and then using simple commands to set a new password. This is a vital step when you forget your database administrator login details, helping you get back into your MySQL or MariaDB system.

This method works even if you’re using newer versions like MySQL 8.0 or MariaDB 10.3 and newer. You can avoid the hassle of reinstalling your database entirely.

⚡ Quick Answer

Stop the MySQL or MariaDB server, then restart it with `mysqld_safe –skip-grant-tables`. Log in to the database as root, update the user’s password using SQL commands like `ALTER USER` or `SET PASSWORD`, and then flush privileges. Finally, restart the server normally.

MySQL and MariaDB using root passwords

Switching the MySQL or MariaDB root user from `auth_socket` to a password simplifies access management. This method helps if you forget the root password or need to change it later. The password method works for MySQL 5.7 and newer, and MariaDB 10.1 and newer.

MySQL:

💻Code
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'type_strong_password_here';

MariaDB:

💻Code
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User='root';

Now you can reset the forgotten password using the steps below.

How to reset MySQL or MariaDB root password

You can reset your MySQL or MariaDB root password by stopping the database server and starting the database server in a special mode. This special mode lets you set a new password without needing the old password, skipping normal login checks. This password reset process works for both MySQL and MariaDB, versions 5.7 and later.

This can be done for MySQL or MariaDB.

🐧Bash / Shell
sudo systemctl stop mysql
sudo systemctl stop mariadb

After that, run the commands below to start MySQL in safe mode, bypassing the standard authentication process without loading the grant tables:

🐧Bash / Shell
sudo mysqld_safe --skip-grant-tables &

Using the --skip-grant-tables option lets anyone connect to the database server without a password, granting them all privileges. However, this is only possible if you connect from the local server terminal console, so be mindful of this limitation.

You may need to run these commands for MySQL servers to resolve certain errors.

These commands create a new `mysqld` directory. The `mysql` user then receives ownership of this directory. This prepares the MySQL or MariaDB system for the root password reset, ensuring it’s ready for the next step.

🐧Bash / Shell
sudo mkdir /var/run/mysqld/
sudo chown mysql /var/run/mysqld

While on the local server, run the commands below to log on as root to the database server.

MySQL 5.7.6+ and MariaDB 10.1.20+ require specific commands for resetting the root password. These commands handle password changes correctly for these newer database versions.

💻Code
UPDATE mysql.user SET authentication_string = PASSWORD('type_new_password_here')
 WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

If the SQL commands above don’t work, try the one below.

💻Code
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'type_new_password_here';
FLUSH PRIVILEGES;

If you’re running earlier database server versions, run the commands below to reset the password.

💻Code
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('type_new_password_here');
FLUSH PRIVILEGES;

You should see the following output without errors if everything went well above.

⚠️Warning
After resetting the MySQL or MariaDB root password, normal server shutdown prevents unauthorized access. Execute the specific commands to safely stop the database server.

The MySQL or MariaDB database will ask for the new root password you just created. Typing this correct password continues the database shutdown process.

Next, you can start up either server using the commands below.

🐧Bash / Shell
sudo systemctl start mysql
sudo systemctl start mariadb

You can now try to log in with the newly created password.

🐧Bash / Shell
sudo mysql -u root -p

If it works, then you’re all set.

Conclusion:

  • Resetting the root password for MySQL or MariaDB is a straightforward process that can be completed without reinstalling the database server.
  • Always take note of your MySQL or MariaDB version as the steps may vary slightly based on the version you are using.
  • The default authentication method for root users is auth_socket, and switching to mysql_native_password allows password authentication.
  • Always ensure that you execute commands with caution, especially when working in safe mode with --skip-grant-tables.
  • Regularly updating your passwords and managing user privileges can contribute to the security of your database.
  • If you encounter issues, consult the official documentation or community forums for guidance.

What do I do if I forgot my MySQL root password?

If you forget your MySQL root password, you can reset it. This involves stopping the database server and then starting it again in a special mode that bypasses security. Once it's running without security, you can set a new password easily.

How to reset root password if forgot?

To reset a forgotten root password, stop your database server. Then, restart it in a special mode that skips password checks. This allows you to run a command to set a brand new password for the root user.

Can I login to MySQL as root without password?

Yes, during installation, MySQL sometimes sets up the root account without a password. However, this leaves your database unprotected. It's best practice to always set a strong password for the root user to keep your data secure.

How to reset MariaDB root password?

To reset your MariaDB root password, first stop the MariaDB server. Then, start it again in a mode that ignores security. This lets you run commands to update the user settings and assign a new password.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Turn "Week Numbers" On or Off for Calendar in Ubuntu Linux
Ubuntu Linux How to Turn "Week Numbers" On or Off for Calendar in Ubuntu Linux
How to Install GNOME Desktop on Ubuntu 24.04
Ubuntu Linux How to Install GNOME Desktop on Ubuntu 24.04
How to Uninstall Software in Ubuntu Linux
Ubuntu Linux How to Uninstall Software in Ubuntu Linux

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *