MySQL5.7修改默认密码随机密码

Posted 水木山川

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL5.7修改默认密码随机密码相关的知识,希望对你有一定的参考价值。

mysql5.7以后默认生成随机密码,修改root用户密码

  1、修改配置参数
Linux:     /etc/my.cnf
Windows:    C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
[mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证
或者启动服务时添加命令: service mysqld start --skip-grant-tables

  2、重启 mysql 服务
Linux:      service mysqld restart
Windows:     net start/stop MySQL57

  3、使用 root 用户登录到 mysql:
Linux:      mysql -u root
Windows:  必须用管理员权限启动cmd,切换到C:\Program Files\MySQL\MySQL Server 5.7\bin,mysql -u root -p

  4、切换到mysql数据库,更新 user 表,user表里面保存了全部用户信息
update user set password=password(‘root‘) where user="new_password";
alter user ‘root‘@‘localhost‘ identified by ‘new_password‘;
update user set authentication_string = password(‘new_password‘), password_expired = ‘n‘, password_last_changed = now() where user = ‘root‘;

  注:在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

  5、退出 mysql,编辑 /etc/my.cnf 或 C:\ProgramData\MySQL\MySQL Server 5.7\my.ini文件,删除 skip-grant-tables=1 行
MySQL三个退出命令:    exit;    quit;    \q;

  6、重启 mysqld 服务,再用新密码登录即可

注:mysql 5.7 在初始安装后(centos7 操作系统)会生成随机初始密码,并在 /var/log/mysqld.log 中有记录,可以通过 cat 命令查看,找 password 关键字找到密码

 













以上是关于MySQL5.7修改默认密码随机密码的主要内容,如果未能解决你的问题,请参考以下文章

自动修改mysql5.7初始化密码

CentOS安装MySql5.7后修改密码

mysql5.7.11修改root默认密码

修改wampsever中MySql5.7.14默认为空的密码

Mysql5.7修改密码

Ubuntu18.04 修改Mysql5.7默认root密码