Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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 ?

like image 627
jingo Avatar asked Oct 06 '11 07:10

jingo


People also ask

How do I fix localhost Access Denied?

Use the ALTER USER command and change the authentication method to log into MySQL as root: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password'; This command changes the password for the user root and sets the authentication method to mysql_native_password.

How do I fix MySQL access denied error?

You will get this error when the user user_name does not have the right to access your MySQL database. To resolve the error, you must create a user with the following command: mysql> GRANT ALL ON *. * to user_name@localhost IDENTIFIED BY 'password';

How do I fix Access denied for user root localhost in xampp?

Solution: Actually the problem is not in code, the issue is that by-default mysql in xampp allowed passwordless authentication in mysql, so we have to disable that and it will work as normal. Uncomment Line 19, save file and restart mysql service.

How do I fix error 1045 28000 Access denied?

How to fix “Error 1045 (28000) access denied for user 'root'@'localhost' (using password: yes)”? It arises when you perform a fresh installation of MySQL and try to login with a password. The default password of MySQL is blank () (i.e. empty string). So, you can login to the MySQL server using the same password.


2 Answers

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

like image 68
Skippy le Grand Gourou Avatar answered Oct 12 '22 23:10

Skippy le Grand Gourou


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()); }
like image 21
Lee Avatar answered Oct 12 '22 22:10

Lee