[英]PHP to MySQL using SSL
I have a remote MySQL Server that requires me to use SSL for connections. 我有一个远程MySQL Server,要求我使用SSL进行连接。 I can connect to it using my terminal.
我可以使用终端连接到它。 But when I try to connect to it using PHP, I get the following error:
但是,当我尝试使用PHP连接到它时,出现以下错误:
SSL3_GET_RECORD:wrong version number
It seems like the OPENSSL Handshake fails and the reason could be that my PHP is trying to connect to it using SSL3. 似乎OPENSSL握手失败,原因可能是我的PHP试图使用SSL3连接到它。 The MySQL Server supports only TLSv1.2.
MySQL服务器仅支持TLSv1.2。 Is there a way to force PHP to connect using TLSv1.2 ?
有没有一种方法可以强制PHP使用TLSv1.2连接?
Here is my code used to connect: 这是我用于连接的代码:
<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);
$db = mysqli_init();
mysqli_options ($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set(NULL, NULL, '/path/to/ca-cert.pem' , NULL, NULL);
$link = mysqli_real_connect ($db, 'hostname', 'user', 'password', 'dbname', 3306, NULL, MYSQLI_CLIENT_SSL);
if (!$link)
{
die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
} else {
$res = $db->query('SHOW TABLES;');
print_r ($res);
$db->close();
}
?>
Things I have tried and possible problems: 我尝试过的事情和可能出现的问题:
So, in short, Help! 因此,简而言之,求助!
Environment: 环境:
PHP 7.0.18 PHP 7.0.18
MySQL Server Enterprise version 5.7.18 MySQL服务器企业版5.7.18
OpenSSL 1.0.2g OpenSSL 1.0.2克
OS: Windows 7, Ubuntu 16.04. 操作系统:Windows 7,Ubuntu 16.04。 Tried on both
都尝试过
The problem was the driver that PHP used to connect to MySQL. 问题出在PHP用于连接MySQL的驱动程序。 By default, it is mysqlnd.
默认情况下,它是mysqlnd。 And I don't think it was being able to connect using a SHA256 or better cipher to my MySQL Server, which is an enterprise version.
而且我认为它无法使用SHA256或更好的密码连接到我的MySQL Server(企业版)。
The other option for the driver is a libmysql library which Oracle does provide as part of the MySQL Enterprise Server (.deb files). 驱动程序的另一个选项是libmysql库,Oracle确实将它作为MySQL Enterprise Server的一部分提供(.deb文件)。 I started with a clean OS(Ubuntu), and compiled PHP with the libmysql provided by Oracle, and then I was able to establish the connection successfully!
我从一个干净的OS(Ubuntu)开始,然后使用Oracle提供的libmysql编译PHP,然后我成功建立了连接!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.