dougang1965 2014-01-27 22:47
浏览 54
已采纳

如何在PHP中使用Doctrine2设置SSL加密的MySQL连接(不是Symfony,而不是Doctrine1)

I am having a hard time finding documentation / examples of how to setup an SSL encrypted connection with Doctrine2 to MySQL. I'm not using Symfony, so looking for the pure PHP path.

What I'm stuck on is basically how to convey the MYSQL_CLIENT_SSL (or MYSQLI_CLIENT_SSL) flag, and the path to the ca certificate. I can live with not verifying the certificate, but I can't live with not encrypting the connection for this task.

On the command line this would be done similar to this:

mysql --ssl-verify-server-cert --ssl-ca=/mysql-ssl-certs/ca-cert.pem --ssl -h host [etc]

In pure php using the mysql extension I think it would look something like:

$conn = mysql_connect($host, $user, $pass, false, MYSQL_CLIENT_SSL);

With mysqli (i think) it would be something like this:

$db = mysqli_init(); 
$db->ssl_set(null, null, $cert, null, null); 
$db->real_connect($host, $user, $pass, $dbname);

The question is, how do I do this with Doctrine2? Is it even possible? How do I modify the initialization for Doctrine2 to do this?

$DOCTRINE2_DB = array(
  'driver'      => 'pdo_mysql',
  'host'        => $host,
  'user'        => $user,
  'password'    => $pass,
  'dbname'      => $dbname,
  'unix_socket' => $sockpath,
);
$DOCTRINE2_EM = \Doctrine\ORM\EntityManager::create($DOCTRINE2_DB, $DOCTRINE2_CONFIG);
$EM =& $DOCTRINE2_EM; // for brevity & sanity
  • 写回答

1条回答 默认 最新

  • dougui2254 2014-01-27 23:09
    关注

    You should be able to add an additional parameter driverOptions and set the appropiate SSL configuration for PDO

    http://es1.php.net/manual/es/ref.pdo-mysql.php#pdo-mysql.constants

    $DOCTRINE2_DB = array(
        'driver'      => 'pdo_mysql',
        'host'        => $host,
        'user'        => $user,
        'password'    => $pass,
        'dbname'      => $dbname,
        'unix_socket' => $sockpath,
        'driverOptions' => array(
            PDO::MYSQL_ATTR_SSL_CA => '...',
            PDO::MYSQL_ATTR_SSL_CERT => '...',
            PDO::MYSQL_ATTR_SSL_KEY => '...'
        )
    );
    

    I can't test it but looking at the code here I think it should work

    [EDIT BY ASKER:] Here is how it worked for me:

    $DOCTRINE2_DB = array(
        'driver'      => 'pdo_mysql',
        'host'        => $host,
        'user'        => $user,
        'password'    => $pass,
        'dbname'      => $dbname,
        'unix_socket' => $sockpath,
        'driverOptions' => array(
            PDO::MYSQL_ATTR_SSL_CA => '/file/path/to/ca_cert.pem',
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题