Connect to remote MySQL server with SSL from PHP

Connect to remote MySQL server with SSL from PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Connect to Remote MySQL Server with SSL from PHP

Attempting to establish an SSL connection to a remote MySQL server using the legacy mysql_connect function often leads to immediate failures, regardless of whether your client certificate configuration is perfect in the terminal environment. The error message "SSL connection error" is notoriously unhelpful, but it usually points to a specific mismatch between how the PHP extension handles TLS parameters and what the MySQL server expects.

In the scenario where mysql_connect fails despite valid certificates in /etc/mysql/ssl, the issue rarely lies with the certificate files themselves. Instead, it stems from the deprecated nature of the mysql_* functions and their inability to handle modern SSL negotiation contexts properly. Even if you have successfully authenticated via command-line tools like mysql -h ip -u user -p, the PHP environment requires explicit configuration that these legacy functions do not expose cleanly.

The most immediate pitfall in your current approach is the reliance on mysql_connect. This function belongs to the deprecated MySQL extension, which has been removed from many modern PHP distributions precisely because of security flaws and limited SSL support. While you might be constrained by existing PDO adapters or legacy codebases preventing a switch to mysqli or PDO immediately, continuing to use this API for secure connections is a technical debt that will eventually block migration paths.

If you are forced to stick with the mysql_* functions, you must ensure that the SSL context is initialized correctly before attempting the connection. However, in many PHP versions (such as 5.3.6), the MYSQL_CLIENT_SSL flag alone is insufficient if the client does not know where to find the CA certificate to verify the server's identity. Unlike the command-line client which reads from my.cnf automatically, the PHP mysql_connect function does not inherit these settings by default unless explicitly passed or configured via openssl directives in php.ini.

To resolve this without switching libraries immediately, you must configure the OpenSSL context within your application. You can do this by creating a new SSL context and attaching it to the connection, but this is only possible if you are using the mysqli extension. Since you mentioned an inability to use mysqli due to existing PDO adapters, the real solution is to recognize that the mysql_* functions cannot be made to work securely with SSL in modern environments without significant hacks that compromise stability.

The most pragmatic path forward is to migrate the connection logic to PDO or mysqli. These interfaces allow you to pass the SSL certificate paths directly into the DSN (Data Source Name) or via attributes, bypassing the need for global php.ini changes that might not take effect in CLI vs Web contexts. For example, with PDO, you can construct a DSN string like:

$dsn = 'mysql:host=ip;dbname=db_name;port=3306';
$options = [
    PDO::MYSQL_ATTR_SSL_CA => '/etc/mysql/ssl/ca-cert.pem',
    PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql/ssl/client-cert.pem',
    PDO::MYSQL_ATTR_SSL_KEY => '/etc/mysql/ssl/client-key.pem'
];

$pdo = new PDO("mysql:host=ip;dbname=db_name", $user, $pass, $options);

This approach ensures that the server and client certificates are explicitly validated during the handshake. The mssql.secure_connection directive in php.ini is irrelevant to MySQL connections and does not influence SSL behavior for this protocol. Relying on it adds noise without solving the underlying TLS negotiation failure.

If you cannot upgrade your PHP version or switch extensions immediately, consider using the mysqlnd driver with explicit context manipulation, though support for this in PHP 5.3 is severely limited. The core issue remains that the legacy extension lacks the robustness to handle mutual authentication (client certificates) reliably without direct access to the underlying OpenSSL context.

The trade-off here is between immediate code changes and long-term security hygiene. Keeping mysql_connect active prevents you from leveraging modern SSL features like cipher suite selection or certificate revocation checking. Given that your terminal connection works, the infrastructure is sound; the bottleneck is purely the PHP interface's inability to expose these settings. Upgrading to a supported PHP version and adopting PDO or mysqli remains the only sustainable solution for maintaining secure remote database connections.

Our network