标签:
阅读目录
当忘记Mysql的密码时,我们尝试去登录会报以下错误
[root@test ~]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[root@test ~]# service mysqld stop Stopping mysqld: [ OK ]
[root@test ~]# vim /etc/my.cnf [mysqld] skip-grant datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[root@test ~]# service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ]
[root@test ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql>
mysql> use mysql; //进入mysql数据库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set password=password(‘123456‘)where user=‘root‘; //更新root密码为 123456 Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> flush privileges; //重新刷权限表到内存 Query OK, 0 rows affected (0.00 sec)
[root@test ~]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[root@test ~]# service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ]
[root@test ~]# service mysqld stop Stopping mysqld: [ OK ]
[root@test ~]# mysqld_safe --skip-grant-tables &
[root@test ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql>
mysql> use mysql; //进入mysql数据库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set password=password(‘123456‘)where user=‘root‘; //更新root密码为 123456 Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> flush privileges; //重新刷权限表到内存 Query OK, 0 rows affected (0.00 sec)
[root@test ~]# service mysqld restart 150906 10:21:51 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended Stopping mysqld: [ OK ] Starting mysqld: [ OK ]
好了,大功告成!以上的两种解决方法第二种比较方便但是原理都是一样的
再来说说当这样来进行恢复密码的时候应该注意哪些问题:
- 首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库,因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的状态下,其他的用户也可以任意地登录和修改MySQL的信息
- 可以采用将MySQL对外的端口封闭,并且停止Apache以及所有的用户进程的方法实现服务器的准安全状态!最安全的状态是到服务器的Console上面操作,并且拔掉网线进行操作
- 更新密码过后一定要去删除在[mysqld]字段下的参数 skip-grant
标签:
原文地址:http://www.cnblogs.com/vforbox/p/4807989.html