How to connect to mysql server in verify-identity mode with php?

Member

by shyann , in category: PHP General , 2 months ago

How to connect to mysql server in verify-identity mode with php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by darion , 2 months ago

@shyann 

To connect to a MySQL server in verify-identity mode with PHP, you can use the following steps:

  1. Install the MySQL server certificate on your server. This certificate is used to verify the identity of the MySQL server during the connection process.
  2. Set up your PHP script to use SSL for the MySQL connection by adding the 'MYSQLI_CLIENT_SSL' option in the mysqli_connect function. This option tells PHP to use SSL for the connection.
  3. Provide the path to the SSL key, SSL certificate, and SSL CA file in the mysqli_connect function. These files are used to establish a secure connection to the MySQL server.


Here is an example PHP script to connect to a MySQL server in verify-identity mode:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
$port = 3306;
$ssl_key = 'path/to/ssl-key.pem';
$ssl_cert = 'path/to/ssl-cert.pem';
$ssl_ca = 'path/to/ssl-ca.pem';

$conn = mysqli_init();
mysqli_ssl_set($conn, $ssl_key, $ssl_cert, $ssl_ca, null, null);
mysqli_real_connect($conn, $host, $username, $password, $database, $port, null, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno()) {
    die('Failed to connect to MySQL: ' . mysqli_connect_error());
}

echo 'Connected successfully';
mysqli_close($conn);
?>


Replace 'localhost', 'username', 'password', 'database', and the paths to the SSL key, SSL certificate, and SSL CA file with your own values. Make sure that the MySQL server is configured to use SSL and verify-identity mode.


By following these steps, you should be able to connect to a MySQL server in verify-identity mode with PHP.