---
title: "Mysql账号用户名正确但无法登录问题解决"
author: "equalxx"
date: "2017-09-27T15:54:24Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2021-01-03T11:52:02Z"
id: 2394709
url: "https://equalxx.iteye.com/blog/2394709"
---

我本地的mysql版本是5.7.19，设置的用户名和密码是root/root，前几天还用得好好的，突然有一天输入正确的用户名和密码登不上去了，报错像下面这样：

 

```
XUEXUEdeMacBook-Pro:MyShells xuexue$ mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
```

 好多人说用跳过认证的方式直接改密码，我也试了，步骤就是找到mysql安装路径下的bin下，运行

```
./mysqld_safe  --skip-grant-tables &
```

 然后直接就可以免密登陆了，查找user表，用以下方式把密码改成123456

```
update user set authentication_string='123456' where user='root';
```

 发现并不管用，你会发现这一列的密码样式很不合群，按说不应该是明文，应该是加密的啊，看看结果

mysql&gt; select user,host,authentication\_string from user;

+---------------+-----------+-------------------------------------------+

| user          | host      | authentication\_string                     |

+---------------+-----------+-------------------------------------------+

| root          | localhost | 123456                                    |

| mysql.session | localhost | \*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

| mysql.sys     | localhost | \*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

+---------------+-----------+-------------------------------------------+

 

3 rows in set (0.00 sec)

 

而且通过查看user表，发现root用户的password\_expired是Y，意思也就是过期了，这并不是上面update语句能改变的。

最后解决这个问题是要通过

```
GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY '123456'; 
```

 执行完后，再执行

```
flush privileges;
```

 然后就可以通过mysql -uroot -p123456顺利登录了

 

这篇文章是17年写的，回看一下，发现这个东西应该从根上去解决，也就是说，既然知道是密码过期，就应该去设置密码过期时间。我当时估计也就是求先能登陆了：）转发别人的链接：[https://www.cnblogs.com/root-admin/p/9369622.html](https://www.cnblogs.com/root-admin/p/9369622.html)

 

如果觉得文章好的话，鼓励一下吧：D

   
![](http://dl2.iteye.com/upload/attachment/0127/8157/0e525af3-2c79-3dbd-b256-5625aabae878.jpg)