If you never set the root user 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 have previously set the root user password, but have forgotten 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 on to the system as a system administrator.
If the MySQL server is running, stop it. For a server running as a Windows service, go to Service Manager:
Start Menu-> Control Panel-> management tools-> Services
Then locate the MySQL server in the list and stop it.
If the server is not running as a service, you may need to use Task Manager to force it to stop.
Create 1 text files and place the following command on a single line:
SET PASSWORD for ' root ' @ ' 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
Assume that you have installed MySQL to C:\mysql. If you have MySQL installed in another location, please adjust the following command accordingly.
At the DOS command prompt, execute the command:
C:\> C:\mysql\bin\mysqld-nt--init-file=c:\mysql-init.txt
When the server is started, the contents of the file named by the "--init-file" option are executed, and the root user password is changed. After the server has successfully started, you should delete the C:\mysql-init.txt.
If you installed MySQL with the MySQL Installation Wizard, you may need to specify the "--defaults-file" option:
C:\> C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld-nt.exe
--defaults-file="C:\Program Files\MySQL\MySQL Server 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-> Services
Locate the MySQL service in the list, right-click, and select the Properties option. Include the "--defaults-file" setting in Path (path) of the executable field.
Stop the MySQL server and restart it in normal mode. If you run the server as a service, you should start it from the Windows Services window. If you start the server manually, you can use the command as you normally would.
You should be able to connect using the new password.