Skip to content
Follow
Ubuntu Linux

How to Reset MySQL Root Password on Ubuntu

Richard
Written by
Richard
Updated 5 min read
How to Display Seconds on Ubuntu Top Menu Clock
How to Display Seconds on Ubuntu Top Menu Clock

Resetting the MySQL root password on Ubuntu gets you back into your database when you have forgotten the administrator login, and you do not have to reinstall anything. This fix works for current database versions, including MySQL 8.0 and MariaDB 10.3 and newer.

Advertisement

The process has three parts. You stop the database server, start it in a maintenance mode that skips the security checks, and run a short command to set a new password. Then you restart the server normally. You do all of this in the terminal, and it takes a few minutes. You need a user with sudo rights on the server.

⚡ Quick Answer

To reset the MySQL root password on Ubuntu, stop the server with sudo systemctl stop mysql, then start it with sudo mysqld_safe –skip-grant-tables &. Log in with sudo mysql, run the ALTER USER command to set a new root password, and restart the service. No reinstall is needed, and the whole process takes a few minutes.

Advertisement

MySQL and MariaDB using root passwords

On Ubuntu, the root database user often uses the socket plugin by default. That plugin does not ask for a password. It lets the system root account in, so sudo alone gets you to the database prompt. This is why tools such as phpMyAdmin, or a remote client, are often refused even when you type the right details. If a local sudo login is all you need, you may not have to reset anything.

Switching the root user to a standard password makes it easier to reset the MySQL root password later and lets external tools connect. This change applies to MySQL 5.7 and newer, and to MariaDB 10.1 and newer. Run the command below and choose a strong password, so external tools can connect without permission errors.

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';

If you have already lost the password and cannot log in at all, the commands above will not help. Use the reset steps below instead.

Advertisement

How to reset MySQL or MariaDB root password

To reset the root password, you stop the database server and start it again in a safe mode that skips the normal login checks. This lets you reach the database prompt without a password, so you can create a new login. Use this process on Ubuntu when you lose access to MySQL or MariaDB version 5.7 or later.

⚠️WarningDo the work from a terminal on the server itself, either at the console or over SSH.

Plan to finish in one sitting. While the server runs in safe mode, it is not protected by a password.

These steps work for both MySQL and MariaDB. Use the stop command for the database you actually have installed. If you are not sure which one it is, check the version before you start, because some commands differ by version.

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

Once the server has stopped, start it in safe mode. This bypasses standard authentication and does not load the grant tables (the internal system tables that store user privileges). Run the commands below:

🐧Bash / Shell
sudo mysqld_safe --skip-grant-tables &
⚠️WarningThe –skip-grant-tables option lets anyone connect to the database server without a password.

This gives full privileges, but only when you connect directly from the local server terminal. Do not leave the server in this state longer than you need to.

Advertisement

Depending on how the start command runs, your terminal may stay busy afterwards. If it does, open a second terminal window and use that one for the next steps.

On MySQL servers, safe mode can fail to start with an error about a missing directory. If that happens, run the commands below to fix it.

These commands create a new mysqld directory and give ownership of it to the mysql user. Normally the service creates this directory when it starts. In safe mode that does not happen, so you have to do it yourself. With the directory in place, the server can start and you can go on to the password reset.

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

While you are still on the local server, run the commands below to log in to the database server as root. No password is needed in safe mode. You should end up at the database prompt.

Advertisement

MySQL 5.7.6 and later, and MariaDB 10.1.20 and later, need specific commands to change the root password. These commands handle the change correctly on those newer versions. Replace the example password with your own strong one before you run them.

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

If the SQL commands above do not work, try the one below. Different versions and builds can reject one form of the command and accept the other.

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

If you run an earlier database server version, use the commands below to reset the password instead.

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

If everything went well, you should see output like the following, with no errors. If you see an error, check the command for typos and try the other form from the steps above.

Advertisement

After you reset the MySQL or MariaDB root password, shut the server down in the normal way. This closes safe mode, so nobody can get in without a password. Run the commands below to stop the database server safely.

The database will ask for the new root password you just created. Type it, and the shutdown continues. If the password is rejected here, the reset did not take effect, so go back and repeat the password step.

Next, start the server normally with the commands below. This puts it back in regular mode, with password checks on.

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

Now try to log in with the new password. Enter it when you are prompted.

Advertisement
🐧Bash / Shell
sudo mysql -u root -p

If you reach the database prompt, the reset worked and you are all set. If the login still fails, stop the server and repeat the safe mode steps. Check that you typed the new password the same way both times.

Conclusion:

  • Resetting the root password for MySQL or MariaDB is a straightforward process that you can finish without reinstalling the database server.
  • Always note your MySQL or MariaDB version first, as the steps can vary slightly between versions.
  • The default authentication method for the root user is auth_socket. Switching to mysql_native_password allows password authentication.
  • Run commands with care, especially in safe mode with --skip-grant-tables, because the server accepts connections without a password until you restart it normally.
  • Change your passwords regularly and manage user privileges carefully to keep your database secure.
  • If you run into problems, consult the official documentation or community forums for guidance.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

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

Leave a Comment

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