Mysql(Linux)root用户密码重置方法

一、前言

关于mysql在Linux系统密码忘记需要重置,之前也写过一篇文章;http://blog.csdn.net/roy_70/article/details/53006405,但是那次不是使用的markdown,所以图片似乎都已丢失,那个方法是通过修改配置文件使启动跳过密码,然后登陆成功修改密码后再改回来。这次新装的mysql又出现了root密码不对的情况,找了一种更简单的方法,原理都是一样,只是操作起来简单得多

二、重置root密码

首先知道你的mysqld_safe的安装路径,默认是/usr/bin/mysqld_safe,如果不知道的话,也可以使用命令ps -ef | grep mysql查看。
然后关闭mysql,再使用–skip-grant-tables的方式启动:

[root@localhost ~]# service mysqld stop
Stopping mysqld:  [  OK  ]
[root@localhost ~]# ps -ef |grep mysql
root       7809   7785  0 16:18 pts/0    00:00:00 su - mysql
mysql      7810   7809  0 16:18 pts/0    00:00:00 -bash
root       8635   8160  0 16:58 pts/1    00:00:00 su - mysql
mysql      8636   8635  0 16:58 pts/1    00:00:00 -bash
root       8993   8917  0 17:02 pts/1    00:00:00 grep mysql
[root@localhost ~]# /usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
[1] 8994

这时mysql就会以–skip-grant-tables的方式启动,可以跳过密码输入,所以接下来我们需要登陆进去,修改root用户的密码:

[root@localhost ~]# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

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>  update user set password = Password('root') where User = 'root';
Query OK, 3 rows affected (0.04 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

可以看到使用修改密码的sql语句改完密码后退出,这时候再使用mysql -u root -p 使用新密码登录就可以了。
当然记得,改完后关闭mysql,再用正常方式启动起来

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值