MySQL修改密码的多种方式和忘记密码时该如何操作

来源:互联网 发布:高颜值笔记本 知乎 编辑:程序博客网 时间:2024/06/15 22:47

可以登录MySQL的情况下

方法1: 

语句: set password for 用户名@localhost = password('新密码');
例子:set password for root@localhost = password('666');

方法2:
语句:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p888 password 666

方法3:  
先键入语句: use mysql;

再输入修改语句: update user set password=password('666') where user='root' and host='localhost';

或者:update user set authentication_string=password('666') where user='root' and Host = 'localhost';(新版本)
最后输入: flush privileges;  进行刷新。 

忘记密码的情况

1. 关闭正在运行的MySQL服务。
2. 运行cmd,键入mysqld --skip-grant-tables 回车,启动MySQL服务的时候跳过权限表认证。或者在配置文件my.ini中的[mysqld]条目下添加skip-grant-tables,重启服务。
3. 再重新运行一个cmd,键入mysql,回车,若成功将出现MySQL提示符 >。 
4.  键入use mysql,连接权限数据库。

5. 输入修改密码语句

    update user set password=password('666') where user='root';

    或者update user set authentication_string=password('666') where user='root' and Host = 'localhost';(新版本)

6.  最后输入: flush privileges;  进行权限刷新。

7.  输入:quit,退出数据库 

     若修改了配置文件最后需删除原先添加的语句。

8. 重新运行cmd用新密码登录 (最好重启服务)

9.输入:alter user 'root'@'localhost' identified by '666';

10.输入quit退出再重新登录