PHP mysqli无法识别远程数据库的SSL证书

时间:2017-12-18 03:44:33

标签: php mysql ssl

我想通过mysqli函数连接远程mysql数据库。 Connection还需要SSL证书才能访问。所以我的代码看起来像这样:

$db = array(
            "host" => "host",
            "user" => "user",
            "password" => "password",
            "dbName" => "dbName"
        );

        ini_set ('error_reporting', E_ALL);
        ini_set ('display_errors', '1');
        error_reporting (E_ALL|E_STRICT);

        $connection = mysqli_init();
        mysqli_options ($connection, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);

        $connection->ssl_set('/usr/local/certs/client-key.pem',
 '/usr/local/certs/client-cert.pem', '/usr/local/certs/server-ca.pem', NULL, NULL);
        $link = mysqli_real_connect ($connection, $db['host'],
 $db['user'], $db['password'], $db['dbName'], 3306, NULL, MYSQLI_CLIENT_SSL);

 if (!$link)
        {
            die ('Connect error (' . mysqli_connect_errno() . '): '
 . mysqli_connect_error() . "\n");
        } 
        else 
        {
            $response = $connection->query('SHOW TABLES;');
            $this->output->writeln($response);
            $connection->close();
        }

然后我收到了这个错误:

PHP Warning:  mysqli_real_connect(): Peer certificate CN=`[project 
name]' did not match expected CN=`[IP address]`

我在这个错误中挣扎了好几天。我还100%确定我的证书和路径是正确的。我该如何解决它并建立连接?

1 个答案:

答案 0 :(得分:1)

就我而言,我必须先将MYSQLI_OPT_SSL_VERIFY_SERVER_CERT设置为 false

请尝试以下操作:

mysqli_options ($connection, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);

希望它行之有效,也有利于以后的读者。