Method to reset the root user's password in the MySQL database

  • 2020-05-06 11:45:35
  • OfStack

If you have never set a root password for MySQL, the server does not need a password to connect as root. However, it is recommended that you set a password for each account.

If you previously set the root password, but forgot the password, you can set a new password. The following steps are for the Windows platform. The steps for the Unix platform are described later in this section.

Under the Windows platform, this step is:

Log in to the system as the system administrator.

If the MySQL server is running, stop it. For servers running as Windows services, go to service manager:

Start menu -> Control panel -> Management tools -> Service

Then find the MySQL server in the list and stop it.

If the server is not running as a service, you may need to use the task manager to force it to stop.

Create a text file and place the following command on a single line:

SET   PASSWORD   FOR   '@'localhost'   =   PASSWORD('MyNewPassword');

Save the file with any name. In this case, the file is C:\ mysql-init.txt.

Open the console window and enter the DOS command prompt:

Start menu -> Run - >   cmd

Suppose you have installed MySQL to C:\mysql. If you have installed MySQL in another location, please adjust the following commands accordingly.

At the DOS command prompt, execute the command:

C: \ >   C:\mysql\bin\mysqld-nt   --init-file=

C: \ mysql - init txt

When the server starts, change the root password by executing the contents of the file named by the "-- es95-file" option. When the server starts successfully, C:\ mysql-init.txt should be deleted.

If you installed MySQL using the MySQL setup wizard, you may need to specify the "-- es1064en-file" option:

C: \ >   C:\Program   Files\MySQL\MySQL   Server   5.1\bin\mysqld-nt.exe

-- defaults-file ="C:\Program   Files\MySQL\MySQL     5.1\my.ini"

- init - file = C: \ mysql - init txt

Using the service manager, you can find the appropriate "-- defaults-file" setting:

Start menu -> Control panel -> Management tools -> Service

Find the MySQL service in the list, right-click, and select the properties option. Include the "-- defaults-file" setting in the Path(path) of the executable field.

Stop the MySQL server and restart it in normal mode. If the server is running as a service, it should be started from the Windows service window. If the server is started manually, the command can be used as normal.


Related articles: