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 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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