码迷,mamicode.com
首页 > 数据库 > 详细

忘记 Mysql 的 root 密码

时间:2015-09-14 21:15:57      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

阅读目录

 

当忘记Mysql的密码时,我们尝试去登录会报以下错误

 

[root@test ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)

解决方法一 

  • 停止mysql服务
[root@test ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]
  • 编辑/etc/my.cnf  在[mysqld]字段下添加参数  skip-grant 
[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
  • 重启Mysql
[root@test ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
  • 登入Mysql
[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>
  • 更新root密码
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)
  • 编辑/etc/my.cnf 在[mysqld]字段下去掉参数 skip-grant 并重启mysql服务 
[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  ]

解决方法二

  • 停止mysql服务
[root@test ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]
  • 输入命令:mysqld_safe --skip-grant-tables &
[root@test ~]# mysqld_safe --skip-grant-tables &
  • 登入Mysql
[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> 
  • 更新root密码
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)
  • 重启mysql
[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  ]

 


 

好了,大功告成!以上的两种解决方法第二种比较方便但是原理都是一样的

再来说说当这样来进行恢复密码的时候应该注意哪些问题:

  1. 首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库,因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的状态下,其他的用户也可以任意地登录和修改MySQL的信息
  2. 可以采用将MySQL对外的端口封闭,并且停止Apache以及所有的用户进程的方法实现服务器的准安全状态!最安全的状态是到服务器的Console上面操作,并且拔掉网线进行操作
  3. 更新密码过后一定要去删除在[mysqld]字段下的参数 skip-grant 

 

忘记 Mysql 的 root 密码

标签:

原文地址:http://www.cnblogs.com/vforbox/p/4807989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!