mysqli sll连接失败,并显示“读取通信数据包时出错”

时间:2019-03-26 14:22:35

标签: php mysql ssl mysqli

当尝试使用ssl连接到php-7.2中的mysql数据库时,会导致错误Got an error reading communication packets

使用以下命令在服务器上设置了SSL:

sudo mysql_ssl_rsa_setup --uid=mysql

到目前为止,我已经尝试了以下方法:

<?php
$conn=mysqli_init();
if (!$conn){
  die("mysqli_init failed");
}

$conn->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
mysqli_ssl_set($conn,NULL,NULL,"scripts/ssl/ca.pem",NULL,NULL);

if (!mysqli_real_connect($conn,"<ip-address>","<user>","<passsword>","<database>")){
  die("Connect Error: " . mysqli_connect_error());
}

$sql = "SELECT * FROM accounts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

和...

<?php
$conn=mysqli_init();
if (!$conn){
  die("mysqli_init failed");
}

mysqli_ssl_set($conn,"scripts/ssl/client-key.pem","scripts/ssl/client-cert.pem","scripts/ssl/ca.pem",NULL,NULL);

if (!mysqli_real_connect($conn,"<ip-address>","<user>","<passsword>","<database>")){
  die("Connect Error: " . mysqli_connect_error());
}

$sql = "SELECT * FROM accounts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?> 

我暂时添加:

$homepage = file_get_contents("scripts/ssl/client-key.pem");
echo $homepage;

将证书打印到屏幕上的代码,所以我知道文件路径是正确的。

所有SSL文件都是直接从运行mysql的服务器中提取的,因此那里也应该没有问题。

mysqli_connect_error()不返回任何内容。唯一的错误是Got an error reading communication packets中的/var/log/mysql/error.log

我从this堆栈溢出问题中找到了上面的方法。

链接的帖子引用了this文档,该文档仅比official docs有用。似乎缺少有关mysqli的SSL文档。

关于我在做什么错的任何建议

0 个答案:

没有答案