Correctly reset the MySQL password

Source: Internet
Author: User
Tags mysql command line

No one wants to lose the key to the house, but no matter how careful or long, such a thing will always happen several times. The same is true for MySQL passwords. It is not safe to write them in documents, and it will inevitably be forgotten in your mind.

If you forget the MySQL password, how can you reset it?

The following is an incorrect answer:

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

Shell>/etc/init. d/mysql stop
Shell> mysqld_safe -- skip-grant-tables & you can access the MySQL command line without authorization, and reset the MySQL password using SQL:

UPDATE mysql. user SET Password = PASSWORD ('...') WHERE User = '...' AND Host = '...';
Flush privileges; why is it an incorrect answer? After the service is started simply using the skip-grant-tables parameter, unless the database server shields Internet access, other users except themselves may also access the database, although it takes a short time to reset the password, as the saying goes, if you are not afraid of thieves, you will be afraid of thieves. Any leaks may cause a big disaster.

The following is the correct answer:

The key point is to add the skip-networking parameter while using the skip-grant-tables parameter:

Shell> mysqld_safe -- skip-grant-tables -- skip-networking & then use SQL to reset the password. Remember to remove skip-networking and restart the MySQL service normally:

Shell> the method above/etc/init. d/mysqld restart needs to restart the service twice. In fact, it can be more elegant. restart the service once:

First, save the SQL statement used to a text file (/path/to/init/file ):

UPDATE mysql. user SET Password = PASSWORD ('...') WHERE User = '...' AND Host = '...';
Flush privileges; 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 time, the password has been reset, and finally do not forget to delete the file content, so as not to leak the password.

Tip: the parameters used in this article are passed through the command line mysqld_safe, and can also be passed through my. cnf.

For more information, see How to Reset the Root 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: 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.