MYSQL遗忘密码解决方案备忘

 介绍一个非常有用的mysql启动参数—— --skip-grant-tables。 顾名思义,就是在启动mysql时不启动grant-tables,授权表。有什么用呢?当然是忘记管理员密码后有用。
    操作方法:
    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

另外:

首次安装mysql后,使用root登录mysql。终端会提示需要输入密码。可是安装的过程并没有提示输入root的密码。怎么办呢?通过修改my。conf配置绕开mysql的授权验证,重置root用户的密码。操作过程如下 
1)先停止当前正在运行的mysql服务

/etc/init.d/mysql stop

2)修改my.conf 
将下面的语句加入到 my.conf 文件中

#skip-grant-tables

保存退出 
3)重新启动mysql服务 
4) mysql -hlocalhost -root 直接进入到mysql的管理终端

use mysql ;//使用mysql数据库
   update user set password=password('newpassword') where user=root ;//修改root用户的密码
   flush privileges  ;//刷新
   quit ;//退出

5)还原my.conf 文件 
在skip-grant-table前加#号

skip-grant-table

6)重启mysql服务 
/etc/init.d/mysql restart 
7)mysql -hlocalhost -uroot -p //此时输入正确的root密码可以正常登录

猜你喜欢

转载自yjandx.iteye.com/blog/2263390