How to get back the MySQL password (linux/windows)

Source: Internet
Author: User
Tags flush mysql command line

Like *unix version:

Here is the wrong answer:

First stop the MySQL service and then start it with the Skip-grant-tables parameter:

The code is as follows Copy Code
Shell>/etc/init.d/mysql Stop
Shell> mysqld_safe--skip-grant-tables & You can access the MySQL command line without authorization at this time, using SQL reset mysql password: UPDATE mysql.user SET password= PASSWORD (' ... ') WHERE user= ' ... ' and host= ' ... ';

FLUSH privileges; Why do you say it's the wrong answer? Because after using the Skip-grant-tables parameter only to start the service, unless the database server blocked the extranet access, otherwise, in addition to their own, other people with ulterior motives may also access the database, although the time required to reset the password is very short, but the saying is not afraid to steal the thief is afraid of thinking about, Any mistake can lead to catastrophe.

Here is the correct answer:

The key point is: When using the Skip-grant-tables parameter, add the skip-networking parameter as well:

The code is as follows Copy Code
shell> Mysqld_safe--skip-grant-tables--skip-networking

& then after using the SQL Reset password, remember to remove skip-networking and restart the MySQL service in the normal way:

The code is as follows Copy Code
shell>/etc/init.d/mysqld Restart

The above method requires two times to restart the service, and in fact it can be a little more elegant, restart it:

First you need to save the SQL statements used in a text file (/path/to/init/file):

The code is as follows Copy Code

UPDATE mysql.user SET password=password (' ... ') WHERE user= ' ... ' and host= ' ... ';
FLUSH privileges and then use the Init-file parameter to start the MySQL service.

Shell>/etc/init.d/mysql Stop
Shell> Mysqld_safe--init-file=/path/to/init/file

& at this point, the password has been reset, and finally do not forget to delete the contents of the file, so as not to disclose the password.

Tip: The parameters used in this article are passed through the command line Mysqld_safe and can actually be passed through MY.CNF.

Reference: About resetting the password, the official document has a special description: How to Reset the Root Password.

Windows version:

1. Log in as a system administrator.
2. Open cmd-----net start to see if MySQL starts. Stop the net stop MySQL when you start.
3. My MySQL is installed under D:mysql5bin.
4. Skip permission check to start MySQL.
    d:mysql5binmysqld-nt--skip-grant-tables--skip-networking
5. Re-open cmd. Go to D:mysql5bin:
    d:mysql5binmysqladmin-uroot flush-privileges password "NewPassword"
    d:mysql5binmysqladmin-u root-p shutdown  This sentence prompts you to re-enter the password.
6. In cmd net start MySQL
7. It's done.

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.