1.root权限下可以直接修改配置文件,默认配置文件目录:/etc/my.cng
不同操作系统配置文件的默认路径。
vim /etc/my.cnf //编辑配置文件
在mysqld下添加skip-grant-tables=1
qw保存
systemctl restart mysqld //重启mysql服务
重启后直接mysql -u root -p 回车进入,不需要密码,以跳过验证。
2.修改密码
show databases; //查看数据库
use mysql //选择mysql数据库
select user,host from uesr; //查看user表的user,host字段
select * from user \G
update user set authentication_string='123456' where user='root';
//通过authentication_string字段修改密码
grant all privileges on *.* to 'root'@'localhost' identified by '123456';
flush privileges; //刷新权限
quit退出,退出后修改配置文件,注释或者删除skip-grant-tables=1。
重启mysql服务
systectl restart mysqld
mysql -u root -p 输入密码进入MySQL即可。