1. 检查 mysql 认证方法
- 在 mysql 5.7 及以上版本中,
root
用户默认可能使用auth_socket
插件而不是密码进行认证。 - 要查看认证方法,可以通过以下命令登录 mysql:
sudo mysql -uroot -p
然后运行以下查询来查看 root
用户的认证方式:
select user, host, plugin from mysql.user where user='root';
如果你看到 auth_socket
插件与 root
用户关联,说明 root
用户使用的是 socket 认证方式。
可以修改认证方式和密码解决,注意,修改了认证方式后密码也要修改
alter user 'root'@'localhost' identified with mysql_native_password by 'new_password';
2. 重置密码(如果使用密码认证)
- 如果认证方式是基于密码的,可以通过以下步骤重置
root
用户的密码: - 停止 mysql 服务:
sudo systemctl stop mysql
- 使用跳过授权表模式启动 mysql(不进行权限检查):
sudo mysqld_safe --skip-grant-tables &
- 登录到 mysql:
mysql -u root
- 更新
root
用户的密码:
use mysql; update user set authentication_string=password('new_password') where user='root'; flush privileges;
- 重新启动 mysql 服务:
sudo systemctl start mysql
- 尝试使用新密码登录:
mysql -u root -p
3. 检查用户权限
- 如果
root
用户的权限被限制,可以使用以下命令检查用户权限并进行适当调整:
show grants for 'root'@'localhost';
如果需要,可以调整权限,例如:
grant all privileges on *.* to 'root'@'localhost' with grant option; flush privileges;
按照上述步骤检查和修复后,应该能够解决 access denied 错误。
以上就是mysql遇到“ access denied for user ”问题的解决办法的详细内容,更多关于mysql access denied for user的资料请关注代码网其它相关文章!
发表评论