What are some common reasons for the error message "Can't connect to local MySQL server through socket '/tmp/mysql.sock'" when trying to establish a connection to a MySQL database using PHP?

The error message "Can't connect to local MySQL server through socket '/tmp/mysql.sock'" typically occurs when the MySQL server is not running or when the socket path specified in the PHP code does not match the actual socket path. To solve this issue, you can check if the MySQL server is running, verify the correct socket path, or update the socket path in your PHP code to match the actual socket path.

// Update the socket path in the PHP code to match the actual socket path
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
$socket = "/var/run/mysqld/mysqld.sock";

// Create connection
$conn = new mysqli($servername, $username, $password, $database, null, $socket);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";