ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
时间: 2023-11-22 20:50:55 浏览: 38
当我们在使用MySQL时,有时会遇到“ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)”这个错误,这是因为我们使用的root用户没有权限访问MySQL。这个问题可以通过以下两种方法解决:
1.重置root用户密码
我们可以通过以下步骤重置root用户密码:
```shell
sudo /etc/init.d/mysql stop # 停止MySQL服务
sudo mysqld_safe --skip-grant-tables & # 跳过权限表启动MySQL
mysql -u root # 以root用户登录MySQL
use mysql; # 切换到mysql数据库
update user set authentication_string=password('new_password') where user='root'; # 更新root用户密码
flush privileges; # 刷新权限表
quit; # 退出MySQL
sudo /etc/init.d/mysql stop # 停止MySQL服务
sudo /etc/init.d/mysql start # 启动MySQL服务
```
2.使用授权用户登录MySQL
我们可以通过以下步骤创建一个新用户并授权:
```shell
sudo /etc/init.d/mysql stop # 停止MySQL服务
sudo mysqld_safe --skip-grant-tables & # 跳过权限表启动MySQL
mysql -u root # 以root用户登录MySQL
use mysql; # 切换到mysql数据库
create user 'new_user'@'localhost' identified by 'password'; # 创建新用户
grant all privileges on *.* to 'new_user'@'localhost' with grant option; # 授权新用户
flush privileges; # 刷新权限表
quit; # 退出MySQL
sudo /etc/init.d/mysql stop # 停止MySQL服务
sudo /etc/init.d/mysql start # 启动MySQL服务
```