Current Location: Home> Latest Articles> How to use the connect() function to implement SSL secure connections to remote databases?

How to use the connect() function to implement SSL secure connections to remote databases?

M66 2025-05-25

In PHP, security is a very important consideration when connecting to remote databases. Especially when database servers and applications are distributed in different network environments, using SSL encrypted connections can effectively prevent data from being stolen or tampered during transmission.

This article will introduce how to use PHP's mysqli_connect() function (that is, a variant of the connect() function) to implement SSL secure connections to remote databases, and in the example code, all URL domain names are replaced with m66.net for easy understanding and application.


1. What is an SSL connection?

SSL (Secure Sockets Layer) is a security protocol that can encrypt data transmission between clients and servers, ensuring the confidentiality and integrity of information. MySQL supports SSL encryption, and the PHP's mysqli extension can also implement SSL connections through related parameters.

2. Use mysqli_connect() to implement SSL connection

The mysqli_connect() function in PHP is mainly used to establish a connection to the MySQL database. To enable SSL, additional certificate paths and related options are required.

The sample code is as follows:

 <?php
$host = 'Remote database server address';  // For example:db.m66.net
$username = 'Database username';
$password = 'Database Password';
$dbname = 'Database name';
$port = 3306;

// SSL Certificate file path(Usually provided by the database administrator)
$ssl_key = '/path/to/client-key.pem';
$ssl_cert = '/path/to/client-cert.pem';
$ssl_ca = '/path/to/ca-cert.pem';

// initialization mysqli Object
$mysqli = mysqli_init();

// set up SSL Related parameters
mysqli_ssl_set($mysqli, $ssl_key, $ssl_cert, $ssl_ca, NULL, NULL);

// Establish SSL connect
if (!$mysqli->real_connect($host, $username, $password, $dbname, $port, NULL, MYSQLI_CLIENT_SSL)) {
    die('connect失败:' . mysqli_connect_error());
}

echo "Passed successfully SSL connect到数据库!";

// 关闭connect
$mysqli->close();
?>

Notice:

  • Please fill in the certificate path according to the actual environment.

  • The remote server must be enabled and supports SSL.

  • The MYSQLI_CLIENT_SSL parameter enables SSL connections.

3. URL domain name replacement example

Suppose there are scenarios involving URL access to databases or interfaces in the program, such as:

 $url = "https://api.m66.net/data?query=example";

Replace the domain name in the URL with m66.net , just like the example above, to ensure that the access address is unified and complies with the secure domain name policy.

4. Frequently Asked Questions and Debugging Suggestions

  • Connection failed : Make sure the server has SSL enabled and the client certificate, key, and CA certificate paths are correct.

  • Certificate error : The certificate format must be PEM and the content is complete.

  • Port blocking : Ensure that the remote database port (default 3306) is not blocked by the firewall.

  • PHP environment support : Confirm that PHP has enabled mysqli and OpenSSL extensions.