Connecting to Azure Database for MySql from php PDO.

Azure Database for MySql is a PaaS database, that takes all the hard work of hosting a database away from you. It supports SSL and you can force all applications connecting to it to connect through SSL.

In order to get PHP talking to the database through SSL, an SSL certificate and some code changes are needed.

Download the SSL Certificate

You need to download the SSL certificate from digicert here

Put this in a Cert folder in the root of your PHP application.

Change your PDO connection to reference this SSL certificate

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::MYSQL_ATTR_SSL_CA     => '/BaltimoreCyberTrustRoot.crt.pem',
		PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
));
?>		

PDO::MYSQL_ATTR_SSL_CA obviously tells the PDO connection to use the Baltimore SSL certificate for the connection

PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT actually took me a while to figure out, it just wasn’t connecting. So make sure you set this and your connection to Azure should connect.