How can one effectively debug and troubleshoot issues related to MySQL server connectivity in a PHP application?

To effectively debug and troubleshoot issues related to MySQL server connectivity in a PHP application, you can start by checking the database connection credentials, ensuring the MySQL server is running, and verifying that the necessary PHP extensions are enabled. You can also use error handling techniques such as try-catch blocks to catch and display any connection errors that may occur.

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}