一、前言
关于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,再用正常方式启动起来