如何在 php 中通过 SSL 连接 Mysql 数据库?

How to connect Mysql database over SSL in php?

我有一个 YII 1 应用程序。我尝试连接 Azure 数据库。

如果禁用选项,我可以连接到 Azure 数据库:强制 SSL 连接

不过我当然想启用这个选项。

但是如果我启用这个选项。我收到此错误:

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [9002] SSL connection is required. Please specify SSL options and retry.

CWebUser.php(146)

134 
135     /**
136      * PHP magic method.
137      * This method is overridden so that persistent states can be accessed like properties.
138      * @param string $name property name
139      * @return mixed property value
140      */
141     public function __get($name)
142     {
143         if($this->hasState($name))
144             return $this->getState($name);
145         else
146             return parent::__get($name);
147     }
148 
149     /**
150      * PHP magic method.
151      * This method is overridden so that persistent states can be set like properties.
152      * @param string $name property name
153      * @param mixed $value property value
154      * @throws CException
155      */
156     public function __set($name,$value)
157     {
158         if($this->hasState($name))

我的 main.php 看起来像这样:

'db'=> [
            'pdoClass' => 'NestedPDO',
            'connectionString' => 'mysql:host=host_name;dbname=db_name',
            'emulatePrepare' => true,
            'username' => 'user',
            'password' => 'password',            
            'charset' => 'utf8',
            'enableProfiling'=>true,
        ],
        'cache'=>[
            'class'=>'system.caching.CDbCache',
        ],

所以我的问题是:这部分配置缺少什么?

谢谢

如果我这样尝试:

'db'=> [
            'pdoClass' => 'NestedPDO',
            'connectionString' => 'mysql:host=host_name;dbname=db_name',
            'emulatePrepare' => true,
            'username' => 'user',
            'password' => 'password',            
            'charset' => 'utf8',
            'enableProfiling'=>true,      
         'attributes' =>[                       
                PDO::MYSQL_ATTR_SSL_CA => 'path ssl'
            ]

我收到这个错误:

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] (trying to connect via (null))

并且在 mysql workbrench 中,如果我测试 ssl 证书,我会得到成功的响应。

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] (trying to connect via (null))

根据 toffler 的建议,将评论添加为社区 Wiki 答案,以帮助可能面临类似问题的社区成员。

根据 How to use SSL in PHP Data Objects (PDO) mysql

添加 PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false 后错误得到解决