开发者

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. 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 a开发者_JAVA百科gain. Username which that page use to connect to mysql server is not www-data.

Does anyone has faced similar problem ?


www-data is 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>@localhost with no password. This is where www-data@localhost (using password:NO) is coming from.

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_connect will 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.

I suggest enabling error reporting, and error display (as suggested by @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; 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()); at the end of all your query variables to debug them.

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

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.

Thanks!


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


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜