How to Reset MySQL Root Password

Reset the password on Windows, macOS, or Linux.

Most default installations of MySQL require updates to the root password that governs the database.

What Is the MySQL Root Password?

The root account is the equivalent of an Administrator in Windows or someone who can change any part of the system. On Unix-like systems, root can indeed do anything, but it's different in that it's created automatically when the system is installed, and can't normally be deleted. Compare this to Windows, where any user account can be deemed an Administrator.

In both macOS and many Linux distributions, this user is disabled by default. Instead, you'll normally use the su/sudo commands. These execute your command as if you were a superuser. It's the same as using Run as Administrator to run Windows apps.

Now, consider that MySQL is a multi-user subsystem. You install it on your computer or server and assign it its own users, then assign those users their own permissions. These might be completely different from the users on the system in general, especially on *nix systems like Linux. So just like the overall system has a root user, the MySQL subsystem needs a superuser who can fix it when things go wrong. This is the MySQL root user.

When Is the MySQL Root User's Password Created?

Like the system root user, the MySQL root user is a default account and is created when you install MySQL. You'll create the initial password for the MySQL root user at this time. When you install the MySQL server package, the installer will ask you for a password, then set it for the root user.

Creating an Init File to Reset MySQL Password

Changing a root user's password in Unix-like systems is generally a serious task. You can't make it easy to do, or else anyone could do it and take control of your system. Normal password-reset tools will work just fine if you have the current password, but let's assume you don't and you need to reset the password to something new.

The trick we'll use to reset the password uses an init file when the MySQL server starts. This is a file that can contain commands that are executed along with the normal configurations. In this case, one of those commands is going to change the password for the root user.

Regardless of your OS, you can prepare for this by creating a one-line text file containing the following:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

This command modifies a user called root on localhost, or the machine you're working on. The second half sets the password for this user to new_password.

When you start the MySQL server and feed it this init file, it will execute the commands inside, and the effect will be updating the root user's password as the server starts up.

Reset MySQL Password by Starting MySQL With the Init File

The basic sequence we'll do is to stop the server if it's running, start it again manually using the init file, stop it again, and finally restart it in the normal fashion. The specific steps are as follows:

  1. Stop MySQL if it's running. Find the instructions for the operating system you use (Windows, Mac, or Linux), and then skip to Step 5.

    Starting and Stopping MySQL on Windows Using the Services Tool
  2. On Windows, from the Start Menu, open Control Panel > Administrative Tools. Select Services, then look for MySQL in the list. If it shows as running, click Stop the service to shut it down.

    Administrative Tools might be under System and Security if that's how you have Control Panel arranged.

  3. On macOS, go to System Preferences > MySQL. The Preferences Pane will appear and show you if the server is running, as well as give you the option to stop it.

  4. On Linux, you can issue "sudo service mysqld stop" to shut the server down.

  5. Next, start the server again and point to the init file you created. The command is:

    If you're running Windows, make sure you run the above command from the executable's directory, usually C:\ProgramData\MySQL\MySQL Server 8.0\bin

    If you used the MySQL Windows Installer package (.msi), you'll also need to add the "defaults file" to load all other configs MySQL needs: C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" --init-file=C:\Users\aaron\Desktop\name-of-the-file

  6. Finally, restart the MySQL server:

    • On Windows, go to the Services screen, then select Start the service for MySQL.
    • On macOS, start the server back up again from MySQL's Preferences screen.
    • On Linux, run sudo service start mysqld.

    While this isn't strictly necessary, it's a good idea to make sure it starts with all the normal configurations.

  7. At this point, try logging into the server with the command line client using the updated password.