MySQL版本 8.0.34:
-
使用vi或vim修改MySql的配置文件
在root用户下进行或是使用sudo执行:
vim /etc/my.cnf
在[mysqld]下加入以下语句:
skip-grant-tables
-
重启数据库服务
service mysqld restart
-
进入到MySQL,清空密码
此时无需密码即可进入:
mysql -u root
MySQL 5.7.9后取消了password函数,使用authentication_string=password("new_password")会报错:
在MySQL的安全模式即下无法使用 “ALTER USER 'root'@'localhost' IDENTIFIED BY '123abc';”会报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
清空密码:
use mysql
update user set authentication_string='' where user='root';
-
删除先前添加的配置,重启数据库服务
修改my.cnf文件,将skip-grant-tables删除或者注释掉:
vim /etc/my.cnf
重启MySQL服务:
service mysqld restart
-
进入MySQL并修改密码
此时密码为空,依然不需要密码就可以进入:
mysql -u root
可以直接使用以下语句进行修改,密码策略可以进行更改,此处不作更多讲解:
alter user 'root'@'localhost' identified by '@Abc123#';
相关文章:
MySQL8.0正确修改密码的姿势[通俗易懂]-腾讯云开发者社区-腾讯云
linux mysql忘记root密码的解决方法(直接修改root密码方法)_linux mysql root密码忘记-CSDN博客
Mysql修改密码安全策略方法,临时修改和永久修改方法_mysql修改密码策略-CSDN博客