MySQL 8.0 重置 root 密码

场景

mysql 8.0 安装之后,忘记了 root 密码,需要重置.

方案

编辑 my.cnf

$ vi /etc/my.cnf

[mysqld] 下添加 skip-grant-tables

[mysqld]
skip-grant-tables

重启 mysql

$ systemctl restart mysqld.service

重新登录 mysql,无需密码,直接 enter 进入

$ mysql -uroot -p

进入 mysql 之后,要先 刷新权限,否则无法修改密码

mysql> flush privileges;

修改密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

mysql 8.0 默认是安装了 validate_password 插件的
密码至少包含一个大写字母,一个小写字母,一位数字和一个特殊字符,并且密码总长度至少为8个字符
这里使用 mysql_native_password 来修改,如果不是 php 连接可能会报 The server requested authentication method unknown to the client 错误

设置完成之后,编辑 my.cnf 去除 skip-grant-tables 指令.

之后重启 mysql 即可

$ systemctl restart mysqld.service

更多

设置 root 远程登录

登录 mysql 之后,查看权限

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host, user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+
4 rows in set (0.00 sec)

修改权限,允许远程访问

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

yum 安装 mysql

参考官方的 A Quick Guide to Using the MySQL Yum Repository

日志查看

mysql 错误日志 /var/log/mysqld.log

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!