Symfony2 doctrine通过SSL连接到数据库

时间:2013-09-26 22:46:20

标签: mysql symfony ssl doctrine-orm dbal

尝试通过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和doctrine中设置相同的连接,我不断得到的是“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] / var / www / mysite / vendor / doctrine / dbal / lib中的SSL连接错误/Doctrine/DBAL/Driver/PDOConnection.php:36)“} []

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

我无法想到我的命令行连接和我用doctrine / symfony2指定的连接之间的其他任何不同。

2 个答案:

答案 0 :(得分: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'),

答案 1 :(得分: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;

 }