1

尝试通过 SSL 连接到我的 MySQL 数据库,我已经通过 ssh 使用以下命令行成功地与我的网络服务器建立了连接:

mysql -h my.host.here --port=5454  -v --ssl-ca=/etc/apache2/ssl/mysql/ca-cert.pem --ssl-cert=/etc/apache2/ssl/mysql/client-cert.pem --ssl-key=/etc/apache2/ssl/mysql/client-key.pem -u user -p

但是,尝试在 symfony2 和教义中建立相同的连接,我得到的只是“SSL 错误”

    $params = array(
        'driver'   => 'pdo_mysql',
        'user'     => 'user',
        'password' => 'pass',
        'host'     => 'my.host.here',
        'dbname'   => 'media',
        'port'     => '5454',
    );

    if($this->container->hasParameter('media_ca') && $this->container->hasParameter('media_cert') && $this->container->hasParameter('media_key')) {
        $params['driverOptions'] = array(
            PDO::MYSQL_ATTR_SSL_CA => $this->container->hasParameter('media_ca'),
            PDO::MYSQL_ATTR_SSL_CERT => $this->container->hasParameter('media_cert'),
            PDO::MYSQL_ATTR_SSL_KEY => $this->container->hasParameter('media_key'),
        );
    }

/* Using this instead with only the ca_cert gives me the same error
    if($this->container->hasParameter('media_ca')) {
        $params['driverOptions'] = array(
            PDO::MYSQL_ATTR_SSL_CA => $this->container->hasParameter('media_ca'),
        );
    }
*/
    $connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
    $conn = $connectionFactory->createConnection($params);
    return $conn;

在我的日志中:

[2013-10-01 15:23:30] request.CRITICAL:未捕获的 PHP 异常 PDOException:“SQLSTATE [HY000] [2026] SSL 连接错误”在 /var/www/mysite/vendor/doctrine/dbal/lib/Doctrine /DBAL/Driver/PDOConnection.php 第 36 行 {"exception":"[object] (PDOException: SQLSTATE[HY000] [2026] SSL 连接错误在 /var/www/mysite/vendor/doctrine/dbal/lib/Doctrine/ DBAL/驱动程序/PDOConnection.php:36)"} []

我已经仔细检查了网络服务器用户(www-data)是否可以访问证书文件,并且这些证书文件的路径是否正确(在 symfony2 参数中定义)。

我想不出我的命令行连接和我用教义/symfony2 指定的连接之间有什么不同。

4

2 回答 2

1

检索参数是错误的。您需要getParameter($param)方法而不是hasParameter($param). 这些行是正确的。

PDO::MYSQL_ATTR_SSL_CA => $this->container->getParameter('media_ca'),
PDO::MYSQL_ATTR_SSL_CERT => $this->container->getParameter('media_cert'),
PDO::MYSQL_ATTR_SSL_KEY => $this->container->getParameter('media_key'),
于 2013-10-01T14:43:39.333 回答
1

只是为了记录我最终如何解决问题的完整示例:

//Create a connection to another public database.
 private function videoDatabase() {

    //Create a connection to pub-DB.

    $params = array(
        'driver'   => $this->container->getParameter('media_database_driver'),
        'user'     => $this->container->getParameter('media_database_user'),
        'password' => $this->container->getParameter('media_database_password'),
        'host'     => $this->container->getParameter('media_database_host'),
        'dbname'   => $this->container->getParameter('media_database_name'),
        'port'     => $this->container->getParameter('media_database_port')
    );

    if($this->container->hasParameter('media_ca') && $this->container->hasParameter('media_cert') && $this->container->hasParameter('media_key')) {
       $params['driverOptions'] = array(
                PDO::MYSQL_ATTR_SSL_CA => $this->container->getParameter('media_ca'),
                PDO::MYSQL_ATTR_SSL_CERT => $this->container->getParameter('media_cert'),
                PDO::MYSQL_ATTR_SSL_KEY => $this->container->getParameter('media_key'),
        );
   }

    $connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
    $conn = $connectionFactory->createConnection($params);
    return $conn;

 }
于 2013-10-21T11:22:56.960 回答