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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler