如何在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

我很难找到如何使用Doctrine2到MySQL建立SSL加密连接的文档/示例。 我没有使用Symfony,所以寻找纯PHP路径。 p>

我所坚持的基本上是如何传达MYSQL_CLIENT_SSL(或MYSQLI_CLIENT_SSL)标志,以及路径 ca证书。 我可以忍受不验证证书,但我不能忍受不加密此任务的连接。 p>

在命令行上,这将完成类似于此: p >

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

在使用mysql扩展的纯php中,我认为它看起来像: p>

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

使用mysqli(我认为)它会是这样的: p> \ n

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

问题是,如何使用Doctrine2执行此操作 ? 它甚至可能吗? 如何修改Doctrine2的初始化来执行此操作? p>

  $ DOCTRINE2_DB = array(
'driver'=>'pdo_mysql',
'host'=&gt  ; $ host,
'user'=> $ user,
'password'=> $ pass,
'dbname'=> $ dbname,
'unix_socket'=> $ sockpath,
  ); 
 $ DOCTRINE2_EM = \ Doctrine \ ORM \ EntityManager :: create($ DOCTRINE2_DB,$ DOCTRINE2_CONFIG); 
 $ EM =&  $ DOCTRINE2_EM;  //为了简洁& 理智
  code>  pre> 
  div>

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',
    )
);