Skip to content
Follow
Ubuntu Linux

How to Reset MySQL or MariaDB Root Password

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

You reset your MySQL or MariaDB root password on Ubuntu Linux by stopping the database server, restarting it in safe mode without privilege checking, and then using SQL commands to change the password.

This process is crucial when you’ve forgotten your database root credentials and need to regain administrative access to your MySQL or MariaDB instance, typically running version 10.3 or newer on Ubuntu.

Forgetting your root password, especially on a production server, can be a stressful experience, but this guide provides a direct solution.

You can accomplish this task directly from your command terminal without needing to uninstall and reinstall your database.

⚡ 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

MySQL and MariaDB sometimes use a special login called auth_socket for the root user, meaning you can’t just type a password to log in. This method can be tricky if you forget your password or need to change it. Luckily, you can switch to a standard password method to make managing your root access easier.

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

Resetting your MySQL or MariaDB root password involves a few key steps, starting with stopping the database server. You’ll then restart it in a special mode that lets you bypass the usual login checks. This allows you to set a new password without needing the old one, making it a straightforward process to regain access.

You can do this 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.

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

Then run the commands below to create a new mysqld directory and make the mysql user its owner.

🐧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.

💻Code
mysql -u root

Next, run the following commands if you’re using MySQL 5.7.6 and later, or MariaDB 10.1.20 and later:

💻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.

💻Code
Query OK, 0 rows affected (0.00 sec)

Now that the root password has been reset, you can stop the server normally by running the commands below.

💻Code
mysqladmin -u root -p shutdown

You will be prompted for the new password you just created. Type it to continue with the shutdown.

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.

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 *