stop掉mysql服务

首先需要拿到操作系统的root权限,stop掉mysql服务。

这里我使用的是stop

systemctl stop mysqld

修改配置

vim /etc/my.cnf     

[mysqld]

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

skip-grant-tables          (跳过授权表)

之所以有用户名有密码就是因为有授权表的存在,它的表里面记录了用户名和密码。有账户去登录mysql数据库的时候它先去查看那张表,现在就直接跳过去,不看了,直接就登录了。

启动mysql服务

systemctl start mysqld

不用输入密码直接回车

修改密码

不要用 set password = password('root'); 修改密码

update user set authentication_string=passworD("test") where user='root';

update user set authentication_string=passworD("root") where user='root';

密码修改成功

删除或禁止· skip-grant-tables 重启mysql服务;

systemctl restart mysqld 重启服务

接下来可以输入密码登陆了。