Forget the root password of Mysql

Source: Internet
Author: User

Read Catalogue

    • Workaround One
    • Workaround Two

When you forget the MySQL password, we try to log in and report the following Error

[Email protected] ~]# mysql-u root-penter password:error 1045 (28000): Access denied for user ' root ' @ ' localhost ' (using Password:no)
Workaround One
    • Stop MySQL Service
[[Email protected] ~]# service mysqld stopstopping mysqld:                                           [  OK  ]
    • Edit/ETC/MY.CNF Add a parameter under the [Mysqld] field skip-grant
[Email protected] ~]# vim/etc/my.cnf[mysqld]skip-grant                              datadir=/var/lib/mysqlsocket=/var/lib/mysql/ mysql.sockuser=mysql# disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld _safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
    • Restart MySQL
[[Email protected] ~]# service mysqld restartstopping mysqld:                                           [  OK  ]starting mysqld:                                           [  OK  ]
    • Log in to MySQL
[Email protected] ~]# mysql-u rootwelcome to the MySQL monitor.  Commands End With; or \g.your MySQL connection ID is 2Server version:5.1.73 Source distributioncopyright (c), +, Oracle and/or its a Ffiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names trademarks of their respectiveowners. Type ' help ', ' or ' \h ' for help. Type ' \c ' to clear the current input statement.mysql>
    • Update Root password
mysql> use MySQL;                         Enter MySQL database reading table information for completion of table and column namesyou can turn off this feature to get a quick ER startup with-adatabase changedmysql> update user set Password=password (' 123456 ') where user= ' root ';           Update root password is 123456Query OK, 3 Rows Affected (0.00 sec) rows Matched:3  changed:3  warnings:0 mysql> flush Privi Leges;                 Re-swipe the permissions table to memory query OK, 0 rows Affected (0.00 sec)
    • Edit/etc/my.cnf Remove the parameter skip-grant in the [mysqld] field and restart the MySQL service
[Email protected] ~]# vim/etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/ Log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
[[Email protected] ~]# service mysqld restartstopping mysqld:                                           [  OK  ]starting mysqld:                                           [  OK  ]
Workaround Two
    • Stop MySQL Service
[[Email protected] ~]# service mysqld stopstopping mysqld:                                           [  OK  ]
    • Input command: Mysqld_safe--skip-grant-tables &
[Email protected] ~]# Mysqld_safe--skip-grant-tables &
    • Log in to MySQL
[Email protected] ~]# mysql-u rootwelcome to the MySQL monitor.  
    • Update Root password
mysql> use MySQL;                         Enter MySQL database reading table information for completion of table and column namesyou can turn off this feature to get a quick ER startup with-adatabase changedmysql> update user set Password=password (' 123456 ') where user= ' root ';           Update root password is 123456Query OK, 3 Rows Affected (0.00 sec) rows Matched:3  changed:3  warnings:0 mysql> flush Privi Leges;                 Re-swipe the permissions table to memory query OK, 0 rows Affected (0.00 sec)
    • Restart MySQL
[[Email protected] ~]# service mysqld restart150906 10:21:51 mysqld_safe mysqld from PID file/var/run/mysqld/mysqld.pid E Ndedstopping mysqld:                                           [  OK  ]starting mysqld:                                           [  OK  ]

All right, it's done! The two solutions above are more convenient, but the principle is the same.

Again, there are some things you should be aware of when doing this to restore your password:

  1. First verify that the server is in a secure state, that is, no one can connect to the MySQL database arbitrarily, because during the reset of the MySQL root password, the MySQL database is completely out of password-protected state, other users can also arbitrarily login and modify the MySQL information
  2. You can implement the server's quasi-security status by enclosing MySQL ports externally and stopping Apache and all user processes! The safest state is to operate on the console of the server, and unplug the network cable for operation
  3. After updating the password, be sure to delete the parameter under the [Mysqld] field skip-grant

Forget the root password of Mysql

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.