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

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!

Tags:

Mysql

Php