mysql-client-configuration-on-the-php-server

Now we will configure the PHP web application to connect to the MySQL database on the separate server.

1 Installing MySQL Client and Testing the Connection

Install the MySQL client on the web server:

sudo apt update
sudo apt install mysql-client -y

Next, test the connection to the MySQL server:

mysql -h 192.168.85.139 -u todo_user -p

Enter the password for todo_user when prompted. If the connection is successful, you’ll see the MySQL prompt:

2 Configuring the Database Connection in the PHP Application

In the PHP application’s db.php file, update the connection details to reflect the IP address of the MySQL server and the credentials for the newly created todo_user.

Edit the db.php file:

<?php
session_start();

$host = "192.168.85.139"; // Remote MySQL server IP
$user = "todo_user"; // Remote MySQL user
$password = "password"; // User password
$dbname = "todo_db"; // Database name

try {
    $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
    $pdo = new PDO($dsn, $user, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    die("Database Connection Failed: " . $e->getMessage());
}
?>