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

Source: Internet
Author: User
Tags mysql client reset mysql database

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:mysqlbinmysqld-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 filesmysqlmysql Server 5.1binmysqld-nt.exe

--defaults-file= "C:Program filesmysqlmysql Server 5.1my.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. A total of 2 pages.

You should be able to connect using the new password.

In a UNIX environment, the steps to reset the root user password are as follows:

Log on to the system as a UNIX root user, or as the same identity as running the MYSQLD server.

Locate the. pid file that contains the server process ID. The exact location and name of the file depends on your distribution, host name, and configuration. Common locations are/var/lib/mysql/,/var/run/mysqld/and/usr/local/mysql/data/. In general, the file name extension is. PID and starts with the MYSQLD or the system's host name.

Use the pathname of the. pid file in the following command to send a normal kill (instead of kill-9) to the MYSQLD process to stop the MySQL server:

Shell> kill ' Cat/mysql-data-directory/host_name.pid '

Note that the cat command uses the symbol "" instead of "'": This causes the output of the cat to be taken into the kill command.

Create a text file and place the following command on the 1 lines in the file:

SET PASSWORD for ' root ' @ ' localhost ' = PASSWORD (' Mynewpassword ');

Save the file with any name. For this example, the file is ~/mysql-init.

To reboot the MySQL server with a special "--init-file=~/mysql-init" option:

Shell> Mysqld_safe--init-file=~/mysql-init &

The contents of the file Init-file are executed at server startup and the root user password is changed. After the server has successfully started, the ~/mysql-init should be deleted.

You should be able to connect using the new password.

Optionally, on any platform, you can use the MySQL client to set up a new password (but the method is not secure):

Stop mysqld and restart it with the "--skip-grant-tables--user=root" option (Windows users can omit the--user=root section).

Connect to the MYSQLD server using the following command:

shell> Mysql-u Root

The following statement is issued on the MySQL client:

mysql> UPDATE mysql.user SET password=password (' newpwd ')

-> WHERE user= ' root ';

mysql> FLUSH privileges;

Replace "Newpwd" with the actual root user password you intend to use.

You should be able to connect using the new password.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.