php MySQL 错误 1045 访问被拒绝

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

MySQL Error 1045 Access Denied

phpmysqlmysql-error-1045

提问by SidC

Good Morning,

早上好,

I wrote the code block below on my local Windows 7 PC and tried to run it. Unfortunately, I received:

我在本地 Windows 7 PC 上编写了下面的代码块并尝试运行它。不幸的是,我收到了:

Connect Error (1045) Access denied for user 'dbuser'@'myhost(using password: YES)

I have granted dbuser Insert, Select, Update, and Execute using both localhost and % for this database schema. I am able to mysql -u dbuser -p from command line on server as well.

我已使用 localhost 和 % 为该数据库架构授予 dbuser 插入、选择、更新和执行权限。我也可以从服务器上的命令行 mysql -u dbuser -p 。

Here's the code block:

这是代码块:

<?php
/* Set Variables */
$host="serveripaddress";
$db="dbname"; 
$username="dbuser";
$pass="pass";

/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();

}
?>

I'm having difficulty understanding whether the above code block is causing my error, or whether there's something required to be done on the server. Can anyone suggest some areas of investigation?

我很难理解上述代码块是否导致了我的错误,或者是否需要在服务器上执行某些操作。谁能建议一些调查领域?

Thanks, Sid

谢谢,希德

采纳答案by Marc B

Make sure that if you're using a hostname for the GRANTin MySQL, that MySQL can properly resolve that hostname to the IP you're connecting from.

确保如果您GRANT在 MySQL 中使用主机名,则MySQL 可以将该主机名正确解析为您正在连接的 IP。

For instance, if you do

例如,如果你这样做

GRANT blah ON *.* to user@somehost

you have to remember that MySQL won't see 'somehost', it'll see an IP address. It'll have to do a reverse lookup to get a hostname, and if the IP either doesn't have a reverse mapping, or maps to something completely different, MySQL won't give access.

你必须记住,MySQL 不会看到“somehost”,它会看到一个 IP 地址。它必须进行反向查找才能获得主机名,如果 IP 没有反向映射,或者映射到完全不同的内容,MySQL 将不会提供访问权限。

Unless you can guarantee that the reverse mapping is stable, it's best to use IP addresses for remote access accounts in MySQL.

除非你能保证反向映射稳定,否则最好在 MySQL 中使用 IP 地址作为远程访问帐户。

回答by Elxx

<?php
/* Set Variables */
$host="127.0.0.1:3306";
$db="dbname"; 
$username="dbuser";
$pass="pass";

/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());    
}
else
{
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
}

?>

First of all, add these braces in for your if/else statement. Second, try hardcoding the IP. I just ran this with an IP set to a variable (didnt work) and then I hardcoded it, worked just fine.

首先,为 if/else 语句添加这些大括号。其次,尝试硬编码 IP。我只是在将 IP 设置为变量的情况下运行它(没有工作),然后我对其进行了硬编码,工作得很好。