7

I am trying to connect to MySQL that has SSL enabled. I am using Symfony2 framework with Doctrine.

In plain PHP, I can achieve this with

$link = mysql_connect("127.0.0.1:3306","test","testpass",true,MYSQL_CLIENT_SSL);

Does anyone know how I can do this in symfony/doctrine? What is the correct doctrine configuration in config.yml?

UPDATE:

Maybe my question "What is the correct doctrine configuration in config.yml?" is wrong. So, how do I go about doing this? Where should I start?

Thanks

gnowlak
  • 486
  • 1
  • 6
  • 12

2 Answers2

15

I have found the answer after a long search and with the help from people from doctrine chat room.

This is the dbal configuration that works on PHP > 5.3.7 It uses three PDO Constants which are not available to PHP prior to 5.3.7

In standard PDO connection:

$conn = new PDO("mysql:host=localhost;port=3307;database=dbname", "user1", "password1",
    array(
        1010 => '/path/to/certs/priv_key.pem',
        1011 => '/path/to/certs/pub_cert.pem',
        1012 => '/path/to/certs/ca_cert.pem',
    )
);

If trying the above code gives you an error, it is possible that your PHP version is < 5.3.7 then you are unlikely to be able to use PDO with SSL.

Now the solution to the DBAL configuration in config.yml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                password: %database_password%
                charset:  UTF8
                mapping_types:
                    enum: string
                options:
                    1010 : %priv_key% 
                    1011 : %pub_cert% 
                    1012 : %ca_cert%

            default2: # second connection ...

    orm:
        # orm configuration here ....

Hope this helps anyone who are trying to connect using SSL. As a matter of fact, it is recommended to connect using SSL for all your database connection, if it is possible to do so.

gnowlak
  • 486
  • 1
  • 6
  • 12
  • Right answer ! But, don't know where I should place all these *.pem and to what paths should look like.. – Delphine Jun 09 '16 at 12:09
8

Just wanted to point out that the integer values of the following SSL attribute constants are as follows in PHP 5.4.16:

PDO:MYSQL_ATTR_SSL_KEY: 1007
PDO:MYSQL_ATTR_SSL_CERT: 1008
PDO:MYSQL_ATTR_SSL_CA: 1009

They may vary from one version to another, so best to check these values before plugging them into the DBAL confiugration.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
samuraii
  • 81
  • 1
  • 2
  • More info on constants is described in the docs http://php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants – Dimitry K Jan 27 '15 at 17:56