问题描述
不小心忘记了mysql的密码,重置mysql密码时发现/etc/my.cnf下没有内容,MySQL的配置文件my.cnf默认配置如下
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important
这时在[mysqld]下添加skip-grant-tables
,然后重新启动mysql
systemctl restart mysql
其他问题
发现在mysql8.0中的密码是用cache_sha2_password加密后的密码,而我们使用
UPDATE mysql.user SET authentication_string = '123456' WHERE User = 'root';
加进去的密码是明文
1. 安全模式启动
使用管理员权限打开cmd窗口,输入:mysqld --console --skip-grant-tables --shared-memory
2. 刷新权限及退出:flush privileges;
3. 设置数据库密码为空
先把root的密码先设为空:
UPDATE user SET authentication_string = '', plugin = 'caching_sha2_password' WHERE User = 'root';
然后 flush privileges;
再次查看:SELECT Host, User, authentication_string, plugin from user;
发现密码空了,然后exit,如下图:
接下来按ctlc+c退出安全模式(mysqld --console --skip-grant-tables --shared-memory)
mysql -u root 直接回车即可
然后输入:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '123456';
flush privileges;
exit
重启数据库就可以了
这时来查看数据库的密码
参考链接:https://blog.csdn.net/ligaopan/article/details/135593205