Moodle 3.3 with MySQL SSL Connection in the Azure Cloud

Update: Microsoft Azure Database for MySQL: Use PHP (not) to Connect and Query Data article has now some new updates.

Microsoft listened to my words, so I have found a documentation how to connect to Azure Database for MySQL with PHP via SSL connection. This can be found here:
https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl

Of course this is not a complete solution because it did not work for me. So here is my solution.

  1. Follow the steps of the documentation mentioned above.
  2. Modify config.php by adding dbssl and dbcertificate options to the dboptions array :
$CFG->dboptions = array (
 'dbpersist' => 0,
 'dbport' => '',
 'dbsocket' => '',
 'dbcollation' => 'utf8_general_ci',
 'dbssl' => true,
 'dbcertificate' => '/path/to/cert/BaltimoreCyberTrustRoot.crt.pem', //modify path
);
  1. Modify /path/to/moodle/lib/dml/mysqli_native_moodle_database.php by changing the following code in the function connect().
if ($dbhost and !empty($this->dboptions['dbpersist'])) {
    $dbhost = "p:$dbhost";
}

$ssl = false;

if (empty($this->dboptions['dbssl'])) {
    $ssl = false;
} else {
    $ssl = (bool)$this->dboptions['dbssl'];
    if (!empty($this->dboptions['dbcertificate'])) {
        $dbcertificate = (string)$this->dboptions['dbcertificate'];
    }
}

if ($ssl){
    $this->mysqli = mysqli_init();
    $this->mysqli->ssl_set(NULL,NULL, $dbcertificate, NULL, NULL) ;
    $this->mysqli->real_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, NULL, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);
    } else {
    $this->mysqli = @new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
}

The magic here is that for the function real_connect() you have to add MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT attribute to the end.

Then test your connection.

Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.