How to Solve MySQL Socket Error mysqld.sock

Issue:

Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’

Error deleting data: Access denied for user ”@’localhost’ (using password: NO)

Environment:

Docker, Apache/2.4.10 (Debian), PHP Version 5.3.29, MySQL Version 5.7

Resolution:

Below is the sample code that produce an error. What happened was on mysql_query($query) statement, it doesn’t has connection defined and rely on $conection to be open before this statement can be executed.

    $query = sprintf( "DELETE FROM tablename where ID = %ld;", $this->ID );
    $result = mysql_query( $query );
    if( !$result ) {
      die('Error deleting data: '.mysql_error());
    }

 

So, to solve this problem, make sure you have $connection open before executing mysql_query() as shown below:

    $connection = database_connect( $database_name );
    $query = sprintf( "DELETE FROM tablename where ID = %ld;", $this->ID );
    $result = mysql_query( $query );
    if( !$result ) {
      die('Error deleting data: '.mysql_error());
    }

 

Troubleshoot:

Look for your connection to MySQL in your code!!