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.
Does anyone has faced similar problem ?
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 wherewww-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_connectwill emit an error message, and returnFALSE. 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) :
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: