我需要更改一个旧的 Symfony 1.4 应用程序,以便它能够通过 ssl 连接连接到 mysql。
我为 Symfony >= 2 找到了很多关于这个的信息。但不幸的是,对于这个尘土飞扬的版本没有。
出于验证目的,我已经通过编辑使其工作
./apps/frontend/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Connection.php
$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']), array(PDO::ATTR_PERSISTENT => true));
到
$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']),
array(PDO::ATTR_PERSISTENT => true,
PDO::MYSQL_ATTR_SSL_KEY => '/etc/my.cnf.d/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/etc/my.cnf.d/ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/etc/my.cnf.d/ssl/ca-cert.pem'));
但我想知道这个丑陋的 hack 是否真的是唯一的解决方案?
最佳答案
我花了一段时间才看到这个连接类已经被覆盖了(apps/frontend/lib...)。
所以我只需要让这些变量可配置。 databases.yml 配置中有一个名为 attributes 的选项(doctrine::param::attributes)。如果您传递非字符串键,您可以使用 getAttribute 获取它们。
所以至少它可以工作(它在connect 方法的try 区域内)。
$sslOptionKeys = array(PDO::MYSQL_ATTR_SSL_KEY, PDO::MYSQL_ATTR_SSL_CERT, PDO::MYSQL_ATTR_SSL_CA);
foreach($sslOptionKeys as $sslOptionKey) {
if(array_key_exists($sslOptionKey, $this->pendingAttributes)) {
$pdoOptions[$sslOptionKey] = $this->getAttribute($sslOptionKey);
}
}
$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']),
$pdoOptions);
在 databases.yml 中,您必须键入以下内容(注释有助于理解这些数字)
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=localhost;dbname=db
username: user
password: pass
encoding: utf8
attributes:
#PDO::MYSQL_ATTR_SSL_KEY
1010: /etc/my.cnf.d/ssl/client-key.pem
#PDO::MYSQL_ATTR_SSL_CERT
1011: /etc/my.cnf.d/ssl/client-cert.pem
#PDO::MYSQL_ATTR_SSL_CA
1012: /etc/my.cnf.d/ssl/ca-cert.pem
关于php - Symfony 1.4 通过 SSL 连接到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44502901/