MySQL Error: : 'Access denied for user 'root'@'localhost' 错误解决

来源:
https://stackoverflow.com/questions/41645309/mysql-error-access-denied-for-user-rootlocalhost

用:
sudo mysql

ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’;
解决了

如安装服务中,未提示设置密码等相关操作则密码错误。解决办法如下

sudo vim /etc/mysql/debian.cnf 进入mysql文件查看原始密码

[client]
host     = localhost
user     = debian-sys-maint  用户名
password = 1v5Fuo0zjy6GkSCz 这是默认随机密码
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = 1v5Fuo0zjy6GkSCz
socket   = /var/run/mysqld/mysqld.sock

用默认的用户名密码登陆:
mysql -u debian-sys-maint -p

原文摘录:

Asked 3 years, 2 months ago

$ ./mysqladmin -u root -p ‘redacted’
Enter password:

mysqladmin: connect to server at 'localhost' failed error:
'Access denied for user 'root'@'localhost' (using password: YES)'

How can I fix this?


Answers

  • Open & Edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distro.
  • Add skip-grant-tables under [mysqld]
  • Restart Mysql
  • You should be able to login to mysql now using the below command mysql -u root -p
  • Run mysql> flush privileges;
  • Set new password by ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
  • Go back to /etc/my.cnf and remove/comment skip-grant-tables
  • Restart Mysql
  • Now you will be able to login with the new passwordmysql -u root -p

All solutions I found were much more complex than necessary and none worked for me. Here is the solution that solved my problem. No need to restart mysqld or start it with special privileges.

sudo mysql

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root (feel free to change it in the query)

Now you should be able to login with root. More information can be found in mysql documentation

(exit mysql console with Ctrl + D or by typing exit)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值