简体   繁体   中英

How to connect to MySQL using SSL on symfony/doctrine

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM