Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
FREEDOM251

Freedom251 › Blog › Database administration

How to Reset a Lost MySQL Root Password on Windows, Linux, and Unix

By Freedom251 Team · · 9 min read

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you can stop MySQL and control how it starts, use a temporary --init-file: it changes the password during startup without disabling authentication for the running server. Use --skip-grant-tables only as an emergency recovery path, because it starts MySQL without its privilege system and lets anyone with local access connect without a password.

The procedures below cover Windows and Unix-like systems, explain why ALTER USER can fail in recovery mode, and show how to return the server to its normal, authenticated configuration.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the reset method before stopping MySQL

There are two documented approaches. The init-file method is the controlled choice: you place one ALTER USER statement in a protected text file, start MySQL with that file, remove it after the password is applied, and restart normally. The generic method starts MySQL with --skip-grant-tables; this is broader but deliberately unsafe while it is running.

Characteristic Temporary init file --skip-grant-tables
Platform coverage Documented separately for Windows and Unix-like systems Generic client-based recovery procedure
Authentication Remains part of the normal server design; the password change is executed during startup Disabled because grant tables are not read
Remote networking Not changed by the method itself MySQL enables skip_networking, so remote connections are disabled while this mode is active
Exposure Limited to the startup interval and to users who can read the init file Anyone who can connect locally can connect without a password and has unrestricted privileges
Operational complexity Requires a readable, protected file and a controlled restart Requires a temporary server, a local client connection, FLUSH PRIVILEGES, and a second restart
Cleanup Delete the file containing the replacement password, then restart normally Stop the temporary instance and restart without --skip-grant-tables or skip_networking

Oracle’s manual summarizes the danger of the emergency option this way: “This enables anyone to connect without a password and with all privileges.” Treat that as a recovery window, not as a configuration to leave enabled.

Prepare the reset safely

  • Confirm which MySQL instance you are changing. On a host with more than one instance, identify the normal data directory, configuration file, service definition, and port before stopping anything.
  • Plan a new password. Use a unique, long value and keep it available while you complete the restart and verification. Do not put it in a shell history, ticket, or chat message unnecessarily.
  • Identify the complete account name. MySQL accounts include a user and a host part. The examples use 'root'@'localhost'; if the actual root account has another host component, substitute that value in every command.
  • Ensure you can stop and start the service. The exact command depends on your service manager or hosting environment. Preserve the sequence in this guide, but use the commands appropriate for your installation.
  • Protect temporary files. The init file contains the replacement password in plain text. Store it where the MySQL service account can read it but ordinary users cannot.

Preferred method: reset with a temporary init file

The init file is read during server startup. Its one-line account-management statement runs before normal client authentication is useful, so you do not need the old password.

Windows procedure

  1. Sign in as a Windows Administrator. You need authority to stop the MySQL service, start the server with a different option, and write a file readable by the service account.
  2. Stop the MySQL service. Use the Services console or the service-management command used by your installation. Confirm that the intended MySQL instance has stopped before continuing.
  3. Create a one-line init file. For example, create a file such as C:mysql-init.txt containing exactly:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
    Replace both the host part and the password as required. Do not add unrelated statements.
  4. Restrict the file. Save it in a location the account that runs the MySQL service can read, while preventing other Windows users from reading it. The file is a secret, not a permanent configuration file.
  5. Start MySQL with the init file. Start the server using the normal executable and its normal configuration options, adding an option such as --init-file=C:mysql-init.txt. If your installation normally uses a specific defaults file or data directory, retain those options.
  6. Wait for successful startup. Check the MySQL error log or service status for a normal startup. A startup failure usually means the path, permissions, configuration options, or account host value needs correction.
  7. Delete the init file immediately after the statement has run. Removing it prevents the replacement password from remaining on disk.
  8. Stop the temporary server and start the service normally. Remove the temporary --init-file option. The ordinary service definition must be used for the final start.

Unix and Unix-like procedure

  1. Log in as the Unix account that normally runs MySQL. This is often an account named mysql. Using the normal account avoids ownership changes in the data directory.
  2. Stop mysqld. Use your distribution’s service manager or the deployment’s normal stop command, and verify that the old process is no longer serving connections.
  3. Create a protected init file. Put one statement in a file such as /var/lib/mysql/mysql-init:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
    Set ownership and permissions so only the MySQL service account (and an administrator who must manage it) can read the file. A mode such as 0600 is appropriate when the file is owned by the service account.
  4. Start the server with the file. The documented pattern is:
    mysqld --init-file=/path/to/mysql-init &
    Add the normal options your installation requires, such as --defaults-file=/path/to/my.cnf. Use the same data directory, socket, and other startup settings as the regular instance.
  5. Do not start the data directory as root by accident. Starting mysqld as root without --user=mysql can create root-owned files and lead to permission failures on later starts. Run the daemon as its normal account or explicitly provide the appropriate user option.
  6. Confirm startup and the password change. Review the error log for successful startup and then test a local login with the new password.
  7. Remove the init file. Delete it as soon as the startup statement has executed; it contains the replacement password in clear text.
  8. Stop the temporary process and restart normally. Start MySQL through the normal service configuration, without the temporary --init-file option.

Verify the normal service

After the final restart, connect through the normal local client path and prompt for the new password, for example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mysql -u root -p

A successful prompt and login confirms that the account can authenticate in the ordinary configuration. If the test fails, stop before making more changes: check that you changed the correct host component, that the final start did not retain a temporary option, and that you are connecting to the intended instance.

Emergency method: use --skip-grant-tables

Use this method when the init-file procedure cannot be completed, such as when you cannot arrange the startup file but can control the server process. It disables the privilege system while the temporary server is running. MySQL also enables skip_networking, so the server accepts local connections only.

Start an isolated temporary server

  1. Stop MySQL if it is running. Do not launch a second process against the same data directory while the normal instance is active.
  2. Start the server with --skip-grant-tables. Keep the process on the local machine and retain the normal configuration options needed to identify the correct data directory. Because this mode is unsafe, do not expose a temporary listener or leave the process unattended.
  3. Connect with the MySQL client without a password. Use a local socket or local connection to the temporary instance, for example:
    mysql -u root

Re-enable account-management statements, then change the password

In this mode, MySQL does not read the grant tables and disables account-management statements such as ALTER USER and SET PASSWORD. The first SQL statement must therefore be:

FLUSH PRIVILEGES;

After the flush succeeds, change the account password:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Adjust 'localhost' if the root account uses another host part. Account-management statements apply privilege changes immediately; you do not need to edit grant tables directly.

Return to the authenticated configuration

  1. Exit the client and stop the temporary server.
  2. Start MySQL normally. Remove --skip-grant-tables and do not retain skip_networking from the recovery start.
  3. Test the new password locally. Use the ordinary client command with -p and verify that the expected instance accepts it.
  4. Check the service definition. A persistent option in a unit file, script, container entrypoint, or configuration file can silently put the server back into recovery mode on the next restart.

Why ALTER USER fails in recovery mode

The failure is normally a consequence of the mode, not a bad password. --skip-grant-tables tells MySQL not to read the grant tables and to start without the privilege system. As a result, account-management statements are disabled until you run FLUSH PRIVILEGES;.

The required order is therefore:

  1. FLUSH PRIVILEGES;
  2. ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Run both statements in the same temporary local session, then restart without the unsafe option. If you try ALTER USER first, the server can reject it because account management has not been re-enabled.

Why mysqladmin password may not work

mysqladmin password does not apply a password change while the server is running with --skip-grant-tables. Flush privileges first, then use an account-management statement such as ALTER USER. This avoids relying on a password-change command that the recovery mode does not permit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Once MySQL has been restarted normally, ordinary password-management commands operate under the normal privilege system. The recovery sequence itself should still use the explicit FLUSH PRIVILEGES followed by ALTER USER.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

The host part of the root account is significant

MySQL identifies an account by the pair user and host, not by the username alone. The commonly documented example is 'root'@'localhost', which matches a local root account. An account such as 'root'@'127.0.0.1', 'root'@'::1', or another host pattern is a different account.

If you alter 'root'@'localhost' when the active account has another host component, the command may target the wrong account or fail because that account does not exist. Determine the host part from the installation’s account configuration and substitute it consistently in the init file, the recovery command, and your login test.

Account changes, grant tables, and FLUSH PRIVILEGES

Statements such as ALTER USER and SET PASSWORD are account-management statements. Changes made through them are loaded immediately. Direct edits to grant tables are a different operation and require FLUSH PRIVILEGES or a server restart before MySQL uses the edits.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a lost root password, do not edit grant tables directly. The documented recovery uses ALTER USER; in skip-grant-tables mode, FLUSH PRIVILEGES is required first only to re-enable that statement class.

Troubleshooting by symptom

Symptom Likely cause Corrective action
ALTER USER is rejected while using --skip-grant-tables Account-management statements are disabled in this mode Run FLUSH PRIVILEGES; in the local client, then run ALTER USER
The new password works for one connection but not another The connections resolve to different host entries or different MySQL instances Verify the account’s host component, socket/port, data directory, and service instance; retest the intended path
Remote clients cannot connect during recovery --skip-grant-tables enables skip_networking Use a local client for the reset, then restart normally before testing remote access
MySQL starts once but later reports data-directory permission errors The temporary daemon was started as root without the normal MySQL user Stop it, restore ownership and permissions as required by the installation, and start as the normal service account or with the correct --user option
The service starts with the old behavior after a successful reset The temporary startup option remains in a service definition or script Remove --init-file, --skip-grant-tables, and any unintended skip_networking setting, then restart through the normal service manager
The password is still exposed after recovery The init file was not removed Delete the file and verify that backups, temporary copies, and shell history do not contain the replacement secret

Service managers, containers, and vendor packages

The documented procedure does not provide one universal stop/start command for systemd, launchd, Docker, or every vendor-managed MySQL package. The service wrapper may supply the executable path, defaults file, data directory, socket, user, and environment variables. Substitute that environment’s commands while keeping the essential order:

  1. Stop the normal instance.
  2. Start one temporary instance with the required recovery option.
  3. Perform the password change locally.
  4. Remove the temporary file or option.
  5. Stop the temporary instance.
  6. Start the normal service and verify authentication.

For containers, make sure the temporary process uses the same persistent data volume as the original instance and that the container entrypoint does not automatically launch a second server. For packaged services, inspect the effective startup configuration rather than assuming the binary’s default paths.

Final security checklist

  • Use the init-file method whenever you can control a clean restart.
  • If skip-grant-tables is unavoidable, keep the server local and unattended time to a minimum.
  • Run FLUSH PRIVILEGES before ALTER USER in that mode.
  • Change the correct user@host account.
  • Delete every init file containing the new password.
  • Restart without --skip-grant-tables, --init-file, and unintended skip_networking.
  • Test with the normal client path and confirm that the expected MySQL instance, not a second local instance, accepted the password.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Database administrationLinuxMySQLpassword recoveryUnixWindows

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.