新版MySQL忘记密码、只能超级用户sudo登录解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenbetter1996/article/details/83187705

根本方法:

修改MySQL配置文件,添加一句skip-grant-tables,重启即可无需密码登录

旧版MySQL

在Ubuntu,默认安装配置文件全路径 /etc/mysql/my.cnf

新版MySQL

对于5.7.23等高版本,配置文件路径和命令已经改变
在这里插入图片描述
此时的 /etc/mysql/my.cnf 文件已经是一个软链接,里面只有指向链接的语句。
真正的配置文件是 /etc/mysql/mysql.conf.d/mysqld.cnf

具体操作

在这里插入图片描述
添加完 skip-grant-tables 后, 重启MySQL

可用的重启指令
sudo/etc/init.d/mysql restart
sudo service mysql restart

之后可用直接输入 mysql, 或者mysql -uroot -p 谁便输入都可以进去
进入后,

  1. 先选择mysql数据库
USE mysql;
  1. 给root用户设置新密码
update mysql.user set authentication_string=password('新密码') where user='root' and Host ='localhost';
  1. 退出删掉skip-grant-tables重启即可

  2. 若打开还错误,请使用root用户/sudo mysql -uroot -p 打开 ,然后输入以下即可解决

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

猜你喜欢

转载自blog.csdn.net/chenbetter1996/article/details/83187705
今日推荐