Home > database >  How to connect Mysql database over SSL in php?
How to connect Mysql database over SSL in php?

Time:05-16

I have a YII 1 application. And I try to connect with the Azure database.

I can connect with the azure database if I disable the option: Enforce SSL connection

But of course I want to enable this option.

But if I enable this option. I get this error:

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))

And my main.php in looks like this:

'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',
        ],

So my question is: what is missing in this part of configuration?

Thank you

if I try it like this:

'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'
            ]

I get this error:

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

and In mysql workbrench if I test the ssl certificat I get a successful response.

CodePudding user response:

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

As suggested by toffler, adding comment as a community wiki answer to help community members who might face a similar issue.

The error got resolved after adding PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false as per How to use SSL in PHP Data Objects (PDO) mysql

  • Related