【问题标题】:Cakephp 3 - MySQL connection over SSLCakephp 3 - 通过 SSL 的 MySQL 连接
【发布时间】:2018-06-08 06:27:40
【问题描述】:

我有一个关于使用 CakePHP 3 通过 SSL 连接到 mySQL-Server 的问题。我知道这可能更像是一个 PHP 问题,但我只是在这里写下我使用的框架。

所以我设置了一个远程 mysql 服务器并想将 CakePHP 与它连接起来。不幸的是,我得到了 MySQL 错误:

SQLSTATE[HY000] [3159] Connections using insecure transport are prohibited while --require_secure_transport=ON. 

因为我配置的服务器只允许安全连接。之后,我搜索了有关安全连接的 Cakephp 文档并找到了 ssl 证书。这是我的设置:

config.php

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'remote-ip',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'non_standard_port_number',
        'username' => 'my_user',
        'password' => 'my_password',
        'database' => 'my_database',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'ssl_key' => '/home/my-user/client-ssl/client-key.pem',
        'ssl_cert' => '/home/my-user/client-ssl/client-cert.pem',
        'ssl_ca' => '/home/my-user/client-ssl/ca.pem',
        'log' => false,

不幸的是,我刚刚收到以下错误:

SQLSTATE[HY000] [2002]

据我所知,应该使用证书正确设置所有内容,因为我可以使用终端和后续使用证书登录,如下所示:

mysql -u my_user -h remote_ip -p --ssl-ca=~/client-ssl/ca.pem --ssl-cert=~/client-ssl/client-cert.pem --ssl-key=~/client-ssl/client-key.pem

如果我尝试一些像这样的原始 php(当然还有我的信息):

<?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('/etc/mysql/ssl/client-key.pem', '/etc/mysql/ssl/client-cert.pem', '/etc/mysql/ssl/ca-cert.pem', NULL, NULL);
$link = mysqli_real_connect ($db, 'ip', 'user', 'pass', 'db', 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();
}
?>

我明白了:

PHP 警告:mysqli_real_connect():对等证书 CN=MySQL_Server_5.7.20_Auto_Generated_Server_Certificate' did not match expected CN=remote_ip'

所以我的问题是现在。有人有类似的问题或可以帮助我获得证书吗? (我使用 ubuntu 16,php 7)或者有没有其他方法可以解决“使用不安全传输的连接......”-错误?

【问题讨论】:

标签: php mysql ssl cakephp


【解决方案1】:

该错误 (Peer certificate CN=...) 告诉您的是,自动生成的证书是为您所连接的 IP 或域名(可能是 127.0.0.1?)创建的。确保您拥有任何 'remote-ip' 的证书。

很可能,config.php 中的host 条目不正确。尝试将其设置为您的域名、服务器 IP 甚至“localhost”。

'host' => 'remote-ip',

生成证书。

也有可能您遇到了另一个已经解决的问题:

PHP MySQL over SSL. Peer certificate did not match

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 2019-04-03
    • 1970-01-01
    • 2012-12-25
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多