(MySQL) 进入库中,忘记了root 密码

以前的一个mysql 库,今天要进去提取数据,但是root的密码给忘记了,试用了很多在我记忆当中的和工作密码记录薄中的所有密码都不行!估计在工作当中,有很多的DBA 都会忘记个别的密码这个错误。今天我就总结一下如果解决这个问题。
首先,在解决这个问题之前,提醒你的是,在你解开库的密码时到你再次为库设置密码的这个时间段,所有人都可以通过root 用户没有密码进入库中,所以此时数据库的安全是最低的。所以,在利用一下的方法解决忘记root用户密码的时候,一定要慎重,千万别造成事故(或者封闭端口不让外访问)。
1:利用root 用户进入 mysql 数据库
[root@mysql5 bin]# ./mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mysql5 bin]# 

2:停掉mysql 实例(这里要注意:要确定你的库上面是否有业务,在没有业务的情况下在停实例)
[root@mysql5 bin]# /etc/init.d/mysql.server stop
Shutting down MySQL.                                       [  OK  ]

3:修改参数配置文件(my.conf)
[root@mysql5 bin]# vi /etc/my.cnf 
# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-grant-tables
(参数介绍:此参数的意思就是启动mysql 服务的时候不启动授权表(mysql 库中的user 表)
添加红色字体参数。
保存退出。
4:启动mysql 服务
[root@mysql5 bin]# /etc/init.d/mysql.server start
Starting MySQL.                                            [  OK  ]

5:进入mysql
[root@mysql5 bin]# ./mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.51-log Source distribution


Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 

此时是没有密码的直接回车下一步。

6: 进入到mysql 库中,修改root 密码,
mysql> use mysql
Database changed
mysql> update user set password='root' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3  Changed: 0  Warnings: 0

7:退出,重启mysql 服务
[root@mysql5 bin]# /etc/init.d/mysql.server restart
Shutting down MySQL.                                       [  OK  ]
Starting MySQL.                                            [  OK  ]

8:利用新密码进入数据库
[root@mysql5 bin]# ./mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.51-log Source distribution


Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 

成功进入


(因为启动mysql 服务有多种方式,所以我们下面介绍第二种方式)  
 1:杀掉原来进行着的mysql
       rcmysqld stop
       或者:
       service mysqld stop
       或者:
       kill -TERM mysqld
 2:以命令行参数启动mysql
       /usr/bin/mysqld_safe --skip-grant-tables &
 3:修改管理员密码
       use mysql;
       update user set password=password('yournewpasswordhere') where user='root';
       flush privileges;
       exit;
 4:杀死mysql,重启mysql



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30176559/viewspace-1794148/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30176559/viewspace-1794148/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值