Home  >  Article  >  Database  >  Why is my PHP SSL connection to a remote MySQL server failing, and how can I fix it using MySQLi or PDO?

Why is my PHP SSL connection to a remote MySQL server failing, and how can I fix it using MySQLi or PDO?

Susan Sarandon
Susan SarandonOriginal
2024-11-21 06:10:10466browse

Why is my PHP SSL connection to a remote MySQL server failing, and how can I fix it using MySQLi or PDO?

Connect to Remote MySQL Server with SSL from PHP

Unable to Connect via mysql_connect

You are encountering a "SSL connection error" while trying to connect to a remote MySQL server with SSL using mysql_connect. This error arises after adding the necessary parameters to my.cnf and successfully connecting to the server with -h ip -u user -p from the terminal.

Solution: Switch to MySQLi or PDO_MYSQL

The outdated mysql_connect has limited support for SSL. Consider switching to mysqli_connect or PDO_MYSQL.

For mysqli_connect, specify SSL options as follows:

mysqli_options($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set('/path/to/client-key.pem', '/path/to/client-cert.pem', '/path/to/ca-cert.pem');

For PDO_MYSQL, include SSL options in the connection array:

PDO::MYSQL_ATTR_SSL_KEY => '/path/to/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/path/to/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca-cert.pem'

Additional Notes

  • Ensure you have a recent version of PHP, as SSL support for PDO is limited in older versions.
  • Refer to the PHP development manual for further details on MySQLi and PDO_MYSQL with SSL.

The above is the detailed content of Why is my PHP SSL connection to a remote MySQL server failing, and how can I fix it using MySQLi or PDO?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn