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

后端 未结 7 1590
终归单人心
终归单人心 2020-12-16 01:45

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 ran

相关标签:
7条回答
  • 2020-12-16 01:55

    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 讨论(0)
  • 2020-12-16 01:57

    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!

    0 讨论(0)
  • 2020-12-16 02:01

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

    0 讨论(0)
  • 2020-12-16 02:05

    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()); }
    
    0 讨论(0)
  • 2020-12-16 02:06

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

    0 讨论(0)
  • 2020-12-16 02:08

    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!

    0 讨论(0)
提交回复
热议问题