Mysql—修改用户密码(重置密码)

一、登陆mysqlmysql

[root@localhost ~]# mysql -uroot -p123456
[root@localhost ~]# mysql -hlocalhost -uroot -p123456

若是忘记密码,则跳过MySQL的密码认证过程。步骤以下:sql

  • 修改Mysql配置文件:vi /etc/my.cnf(注:windows下修改的是my.ini)。在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程。
  • 重启Mysql:
  • 进入Mysql:[root@localhost ~]# mysql -uroot -p

二、使用mysql数据库,从user表中查看主机,用户名,密码数据库

-- 使用mysql数据库
mysql> use mysql;
-- 查询主机用户名密码:5.7版本以前的
mysql> select host,user,plugin,password from user;
-- 查询主机用户名密码:5.7版本以后的,包括5.7
mysql> select host,user,plugin,authentication_string from user;
mysql> select host,user,plugin,authentication_string from user\G;
mysql> select host,user,plugin,authentication_string from mysql.user;

三、修改密码,刷新一下权限windows

mysql> update user set password=password("新密码") where user="root";
mysql> flush privileges;
mysql> quit

上面修改密码是在5.7版本以前的。如果5.7版本以后的(包括5.7),没有password这个字段了,则修改方法以下:bash

mysql> alter user "root"@"localhost" identified by "新密码";  --方法1
mysql> update user set authentication_string=password("新密码") where user="root";  -- 方法2
mysql> flush privileges;
mysql> quit

四、若是以上不能解决密码修改,则使用下面方法ide

mysql> use mysql;
mysql> alter user "root"@"localhost" identified with mysql_native_password by "新密码";
mysql> flush privileges;

修改加密规则:mysql> alter user "root"@"localhost" identified by 'password' PASSWORD EXPIRE NEVER; ui

若是执行以上的操做并无解决,请再把default_authentication_plugin=mysql_native_password添加到配置中。加密

五、再去编辑一下my.cnf配置文件,去掉skip-grant-tables。spa

六、重启Mysql,用你修改后的密码登陆Mysql。code

相关文章
相关标签/搜索