简体   繁体   English

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

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

I face with a strange problem yesterday. 我昨天遇到了一个奇怪的问题。 I have server running Debian with installed PHP 4.4.4-8 and mysql 5.5.9. 我有运行Debian的服务器,安装了PHP 4.4.4-8和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. 出于某种原因,当我尝试加载网页时,我会随机收到“用户拒绝访问'www-data'@'localhost'(使用密码:NO)”错误。 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. 该页面用于连接mysql服务器的用户名不是www-data。

Does anyone has faced similar problem ? 有没有人遇到过类似的问题?

www-data is the Debian user that runs apache and php. www-data是运行apache和php的Debian用户。 If you attempt a query when you don't have a valid connection, php/mysql will attempt to create a connection using <unix-user>@localhost with no password. 如果在没有有效连接的情况下尝试查询,php / mysql将尝试使用<unix-user>@localhost创建一个没有密码的连接。 This is where www-data@localhost (using password:NO) is coming from. 这是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). 现在已经开始发生这种情况的最可能的原因(尽管它已经运行了2年之前)是你的数据库负载增加到某些连接无法成功的程度(可能是由于max_connections或max_user_connections;尽管这也可能来自其他限制,如内存,线程等)。 When this happens, your call to mysql_connect will emit an error message, and return FALSE . 发生这种情况时,调用mysql_connect将发出错误消息,并返回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. 如果您未能检测到此故障,那么您的下一个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 not preceded by a @ sign; 另外,请确保您对mysql_connect的调用前面没有 @符号; and make sure to check the return value. 并确保检查返回值。 It should look something like this: 它应该看起来像这样:

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

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()); 尽量确保你是绝对连接,使用or die(mysql_error()); at the end of all your query variables to debug them. 在所有查询变量的末尾调试它们。

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. 这将显示您的类/文件中可能出现的任何小错误,您可能以前没有找到它们。

If your still having a problem after this, please post your PHP code and I will take a look at it directly. 如果您在此之后仍有问题,请发布您的PHP代码,我会直接看一下。

Thanks! 谢谢!

i faced the same problem. 我遇到了同样的问题。 The problem was in my config.php! 问题出在我的config.php中! I simply changed the $dbserver from "127.0.0.1" -> "localhost". 我只是将$ dbserver“127.0.0.1” - >“localhost”更改为 Now the connection works again! 现在连接再次起作用!

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"; ?> 

取消激活安全模式为我修复了它:

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

对于心不在焉的人,当在mysqli_connect()之后调用mysql_query()时,可能会发生此错误,而应该是mysqli_query()

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"; ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户&#39;www-data&#39;@&#39;localhost&#39;的访问被拒绝 - Access denied for user 'www-data'@'localhost' 错误:用户&#39;www-data&#39;@&#39;localhost&#39;拒绝访问 - Error: Access denied for user 'www-data'@'localhost' WordPress-mysql_query():生产服务器上用户&#39;www-data&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:NO)错误 - Wordpress - mysql_query(): Access denied for user 'www-data'@'localhost' (using password: NO) error on production server PHP mysql_real_escape_string():用户&#39;www-data&#39;@&#39;localhost&#39;的访问被拒绝 - PHP mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' 用户&#39;www-data @&#39;localhost的访问被拒绝 - Access denied for user 'www-data@'localhost 如何授予www-data用户访问python salt模块的权限? - how to grant www-data user access to python salt module? 用户ubuntu拒绝权限,但.csv文件上的用户www-data拒绝该权限 - Permission denied with user ubuntu but not with user www-data on .csv file 如何为 www-data 用户设置 umask? - How to set umask for www-data user? Www-data用户访问/ var / www /之外的文件/文件夹 - Www-data User to Access files/folder outside of /var/www/ 如何在Debian Linux中允许用户www-data进行i2c访问? - How to allow i2c access by user www-data in Debian Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM