php 用户 'www-data'@'localhost 的访问被拒绝 - 如何处理?

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

Access denied for user 'www-data'@'localhost - how to deal with that?

phpmysql

提问by jingo

I face with a strange problem yesterday. I have server running Debian with installed PHP 4.4.4-8 and mysql 5.5.9. That server serves a several websites. For some reason randomly I get that error "Access denied for user 'www-data'@'localhost' (using password: NO)" when I try to load the webpage. If I hit refresh the page loads normally, but afer several clicks that message appears again. Username which that page use to connect to mysql server is not www-data.

我昨天遇到了一个奇怪的问题。我有运行 Debian 的服务器,安装了 PHP 4.4.4-8 和 mysql 5.5.9。该服务器为多个网站提供服务。由于某种原因,当我尝试加载网页时,我随机收到错误“用户'www-data'@'localhost'的访问被拒绝(使用密码:NO)”。如果我点击刷新页面正常加载,但点击几次后该消息再次出现。该页面用于连接到 mysql 服务器的用户名不是 www-data。

Does anyone has faced similar problem ?

有没有人遇到过类似的问题?

回答by Lee

www-datais the Debian user that runs apache and php. If you attempt a query when you don't have a valid connection, php/mysql will attempt to create a connection using <unix-user>@localhostwith no password. This is where www-data@localhost (using password:NO)is coming from.

www-data是运行 apache 和 php 的 Debian 用户。如果您在没有有效连接时尝试查询,php/mysql 将尝试使用<unix-user>@localhost无密码创建连接。这是从哪里来的www-data@localhost (using password:NO)

The most likely reason that this has started happening now (though it has been running fine for 2-years prior) is that your db load has increased to the point where some connections are unable to succeed (probably due to max_connections, or max_user_connections; though this can also result from other limits like memory, threads, etc). When this happens, your call to mysql_connectwill emit an error message, and return FALSE. If you fail to detect this failure, then your next mysql call (probably mysql_query, or mysql_select_db) will attempt the connection to www-data@localhost -- thus causing the problem you're seeing.

现在开始发生这种情况的最可能原因(尽管它在 2 年前一直运行良好)是您的数据库负载增加到某些连接无法成功的程度(可能是由于 max_connections 或 max_user_connections;虽然这也可能是由于其他限制(如内存、线程等)造成的)。发生这种情况时,您对 的调用mysql_connect将发出错误消息,并返回FALSE。如果您未能检测到此故障,那么您的下一个 mysql 调用(可能是 mysql_query 或 mysql_select_db)将尝试连接到 www-data@localhost —— 从而导致您看到的问题。

I suggest enabling error reporting, and error display (as suggested by @DarkMantis) :

我建议启用错误报告和错误显示(如@DarkMantis 所建议):

ini_set('error_reporting', E_ALL|E_STRICT);
ini_set('display_errors', 1);

Also, be sure that your call to mysql_connect is notpreceded by a @sign; and make sure to check the return value. It should look something like this:

另外,请确保您对 mysql_connect 的调用前面没有@符号;并确保检查返回值。它应该是这样的:

$cxn = mysql_connect('localhost','yourusername','yourpassword');
if( $cxn === FALSE ) {  die('mysql connection error: '.mysql_error()); }

回答by DarkMantis

It sounds like the query that is causing the error happens when something specific is called. This could mean that when the query is called, you aren't connected to the database with the correct username/password.

听起来像调用特定内容时会发生导致错误的查询。这可能意味着当调用查询时,您没有使用正确的用户名/密码连接到数据库。

Try to ensure that you are definatly connected, use or die(mysql_error());at the end of all your query variables to debug them.

尝试确保您已明确连接,or die(mysql_error());在所有查询变量的末尾使用以调试它们。

Also, use the following two lines at the top of your php file:

另外,在 php 文件的顶部使用以下两行:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

That will show you any little php errors that may occur within your class/file which you may not have picked up before.

这将显示您的类/文件中可能发生的任何小 php 错误,而您以前可能没有发现这些错误。

If your still having a problem after this, please post your PHP code and I will take a look at it directly.

如果您在此之后仍然遇到问题,请发布您的PHP代码,我会直接查看它。

Thanks!

谢谢!

回答by Franziska

i faced the same problem. The problem was in my config.php! I simply changed the $dbserverfrom "127.0.0.1" -> "localhost".Now the connection works again!

我遇到了同样的问题。问题出在我的 config.php 中!我只是将$dbserver“127.0.0.1”更改为“localhost”。现在连接再次工作!

回答by Hansaraj Wakudkar

Use password 'NO'

使用密码“否”

(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?> 

回答by lama12345

Deactivating safe mode fixed it for me:

停用安全模式为我修复了它:

Notice: mysql_connect(): SQL safe mode in effect - ignoring host/user/password information in?/var/www/html/test.php?on line?4

回答by Skippy le Grand Gourou

For absent-minded people, this error may happen when mysql_query()is called after mysqli_connect(), when it should be mysqli_query().

对于心不在焉的人,这个错误可能会在mysql_query()被调用后发生mysqli_connect(),应该是mysqli_query()

回答by Ambiorix Rodriguez Placencio

For solve this problem. I had to change the connection script Using

为了解决这个问题。我不得不更改连接脚本使用

(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>