The mysqli::ssl_set() function must be called before mysqli::real_connect(), or the settings will not take effect. Incorrect calling order is one of the most common oversights.
Incorrect example:
$conn = new mysqli();
$conn->real_connect('db.m66.net', 'user', 'pass', 'database');
$conn->ssl_set('/path/client-key.pem', '/path/client-cert.pem', '/path/ca.pem', NULL, NULL);
Correct example:
$conn = new mysqli();
$conn->ssl_set('/path/client-key.pem', '/path/client-cert.pem', '/path/ca.pem', NULL, NULL);
$conn->real_connect('db.m66.net', 'user', 'pass', 'database');
When connecting with real_connect(), if the MYSQLI_CLIENT_SSL flag is not passed, SSL may not be enabled for the connection, even if ssl_set() is called.
$conn->real_connect('db.m66.net', 'user', 'pass', 'database', null, null, MYSQLI_CLIENT_SSL);
It is crucial to check whether PHP has the OpenSSL extension enabled. You can confirm this by running phpinfo() or executing php -m | grep openssl in the command line. If it's not enabled, PHP will not be able to establish an SSL encrypted connection.
The server side must also enable SSL and provide valid certificates and CA files. You can confirm this with the following SQL query:
SHOW VARIABLES LIKE '%ssl%';
If the result shows have_ssl as DISABLED, SSL will not work regardless of the client's settings.
Some older versions of PHP or the libmysqlclient library may not support SSL or may require specific compile options. It is recommended to use the mysqlnd driver, as it natively supports SSL and is more modern.
Even if everything is configured correctly, you should verify if the connection has SSL encryption enabled. You can check the actual encryption status with the following query:
$result = $conn->query("SHOW STATUS LIKE 'Ssl_cipher'");
$row = $result->fetch_assoc();
if (!empty($row['Value'])) {
echo "SSL is enabled, the encryption algorithm is: " . $row['Value'];
} else {
echo "SSL is not enabled";
}
If the result is empty, it means SSL is not actually enabled.
In some environments, if localhost is used, the connection may fall back to Unix socket, bypassing the network-layer SSL. It is recommended to explicitly use the hostname or IP address, for example:
$conn->real_connect('db.m66.net', 'user', 'pass', 'database');