php MySQL - 拒绝用户访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5875831/
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-25 22:45:34  来源:igfitidea点击:

MySQL - Access denied for user

phpmysqlpermissions

提问by Arnthor

So, I create a new user

所以,我创建了一个新用户

CREATE USER servname_shb IDENTIFIED BY 'password';

Grant him all the privileges:

授予他所有特权:

GRANT ALL ON *.* TO servname_shb;

No errors so far. Then I try to connect to database:

到目前为止没有错误。然后我尝试连接到数据库:

$dbhost = "localhost";
$dbname = "servname_shbusers";
$dbuser = "servname_shb";
$dbpass = "password";

$c = mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:".mysql_error());
mysql_select_db($dbname) or die ("Error connecting to databse:".mysql_error());

And get an error:

并得到一个错误:

Access denied for user 'servname_shb'@'localhost' (using password: YES) in <path>

I tried FLUSH PRIVILEGES, dropping and recreating user - no effect. What am I doing wrong?

我尝试了 FLUSH PRIVILEGES,删除并重新创建用户 - 没有效果。我究竟做错了什么?

回答by Thilo

The error messages gives you a hint already. Use this grant statement:

错误消息已经给你一个提示。使用此授权声明:

GRANT ALL ON *.* TO 'servname_shb'@'localhost';

Or better, as @grantk points out in the comments, refine the access scope to only what is actually needed.

或者更好,正如@grantk 在评论中指出的那样,将访问范围细化为仅实际需要的范围。

回答by isra17

You must use 'servname_shb'@'localhost'as the username. Your request become:

您必须使用'servname_shb'@'localhost'作为用户名。您的请求变为:

CREATE USER 'servname_shb'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'servname_shb'@'localhost';

Note: 'servname_shb'@'localhost'only allow connection from localhost. You can use 'servname_shb'@'%'to allow connection from everywhere.

注意:'servname_shb'@'localhost'只允许从本地主机连接。您可以使用'servname_shb'@'%'允许来自任何地方的连接。