环境:CentOS7 MySQL5.7
mysql -uroot -p
,后输入密码后提示
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 停止mysql服务
sudo systemctl mysqld stop
跳过验证
编辑mysql配置文件
sudo vim /etc/my.cnf
在最后添加一行,使其登录时跳过权限检查
skip_grant_tables
修改密码
启动MySQL服务,
sudo systemctl mysqld start
登录MySQL
mysql -uroot -p
此时提示输入密码,输入任意密码回车即可进入MySQL。
将数据库切换至mysql库
mysql>USE mysql;
修改密码
mysql>update user set authentication_string = password(‘newpasswd’) where user='root';
刷新MySQL权限相关的表
mysql>flush privileges;
退出
mysql>exit;
重启服务
将my.cnf文件中加入的跳过权限语句删除或加#号注释重启服务,使用修改后的密码登录即可。
sudo systemctl mysqld restart