尽管设置了密码,但 MySQL 允许无密码连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15866237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:14:44  来源:igfitidea点击:

MySQL allows connection without password although password is set

mysqlpasswords

提问by Marcelo Diniz

I'm setting a MySQL server (actually a Percona server, but that shouldn't matter) and I'm setting a password to the root user. At the end, I have this:

我正在设置一个 MySQL 服务器(实际上是一个 Percona 服务器,但这应该无关紧要)并且我正在为 root 用户设置一个密码。最后,我有这个:

mysql> select host, user, password from user;
+-----------+------------------+-------------------------------------------+
| host      | user             | password                                  |
+-----------+------------------+-------------------------------------------+
| localhost | root             | *huge string here, no kidding             |
| localhost | debian-sys-maint | *another huge string here                 |
+-----------+------------------+-------------------------------------------+
2 rows in set (0.00 sec)

I thought this should not allow the root user to connect without a password. However, if I go to the command line, I can connect with mysql -u rootor just mysql. If I do mysql -u root -pand hit enter for the password, then I get ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO).

我认为这不应该允许 root 用户在没有密码的情况下进行连接。但是,如果我转到命令行,则可以连接mysql -u root或仅连接mysql. 如果我这样做mysql -u root -p并按回车键输入密码,那么我得到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO).

Could anyone explain to me how to make sure a user can only connect with a password?

谁能向我解释如何确保用户只能使用密码进行连接?

Edit: if relevant, I set the password with SET PASSWORD FOR 'root'@'localhost' = PASSWORD('somethinghere');

编辑:如果相关,我设置密码 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('somethinghere');

Edit: output of show grants, it indicates I used a password to login but I did not.

编辑: 的输出show grants,它表明我使用密码登录但我没有。

mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*huge string here, no kidding' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

回答by Marcelo Diniz

Facepalm. It turned out there was a .my.cnf on /root with username and password, and so it was possible to login only with mysqlwhen using the root account (that's what I was using). It was created by a Chef recipe (percona was installed via Chef) and I wasn't aware of it.

面掌。结果发现 /root 上有一个 .my.cnf 带有用户名和密码,因此只有mysql在使用 root 帐户时才可以登录(这就是我使用的)。它是由 Chef 配方创建的(percona 是通过 Chef 安装的),我并不知道。

The hint was to look at the output of show grants. Even though I entered no password it still said I entered one, so there must be one somewhere!

提示是查看show grants. 即使我没有输入密码,它仍然说我输入了一个,所以某处肯定有一个!