用root登入MySQL 显示 Access denied for user ‘root’@‘localhost’ (using password: **)

这个问题搞了我好几天才摸出来 真的难受。提示这样的错误是因为你输入的密码时错误的,所以拒绝你的访问。

如果你时刚装上mysql(在Linux上) 使用下面的命令:

grep 'temporary password' /var/log/mysqld.log

查看你的临时密码,然后进mysql后自己再去用下面 第4 步的命令去修改密码

使用无密码认证的办法去修改密码

  1. 进入 编辑vim /etc/my.cnf文件
vim /etc/my.cnf
  1. 在[mysqld]下面添加一条命令:skip-grant-tables 不用验证密码进入MySQL
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
skip-grant-tables
# Remove leading # and set to the amount of RAM for the most important 

3.重启MySQL

 systemctl restart mysqld.service 

进入mysql

[root@VM-20-5-centos ~]# mysql -u root -p
Enter password: 

直接按回车
4. 将root 密码设置为空 authentication_string是MySQL8.0 的usr里的密码字段

use mysql; (使用mysql数据表)
update user set authentication_string='' where user='root';(将密码置为空)
exit (退出mysql)
  1. 编辑 vim /etc/my.cnf文件,将skip-grant-tables注释掉
 vim /etc/my.cnf 
[mysqld]
#skip-grant-tables
# Remove leading # and set to the amount of RAM for the most important 

6.重新开启MySQL

systemctl restart mysqld.service 

7.进入mysql

mysql -u root -p
(此处会显示输入密码,直接回车就好了)

修改root 为可以远程登陆 同时重新设置密码

ALTER USER 'root'@'%' IDENTIFIED BY 'password';(password是你要更改后的密码)

这样就可以在所有电脑远程登入了

另一种方法

思路:创建一个和root一样权限的账号
. 创建一个用户
– username:用户名称
– %:是通配符指的是任意IP,这个位置也可以指定特定的ip,或者localhost代表本机才可以登录

create user 'username'@'localhost' identified by 'password';
create user 'username'@'%' identified by 'password'; 

查看新创建的用户

select * from user where user = 'username';

查看权限 localhost这个地方的字段根据你创建的时候的字段填写(localhost、%)

show grants for 'username'@'localhost';

.然后给最高权限

grant all privileges on *.* to 'username'@'localhost' with grant option;

刷新权限

flush privileges;

. 用这个用户登录就行

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐