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.
- Follow the steps of the documentation mentioned above.
- 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 );
- 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.
4 thoughts to “Moodle 3.3 with MySQL SSL Connection in the Azure Cloud”
Hello… Iam trying to make function MySQL SSL with moodle, where Secure Connection and Moodle Database User require SSL.
I need this very urgent. I trying to make this with Moodle 4.1. Can you help me specially with function connect()? I dont find function connect(), only real_connect(). I comment a few lines of code but iam not sure what code i need change and if this code that you share its of for moodle 4.1 and php 8.1.
Hello,
The code that I shared was for Moodle 3.3+ and PHP 7.
I checked my Moodle 4.0+ and yes, the code has changed a lot.
In the file
path/to/moodle/lib/dml/mysqli_native_moodle_database.php
there is a function raw_connect() // for me it is in line 537
which contains a real_connect function in line 575
You need to follow the guide and change add the cert there.
Cheers,
Moodle is working to resolve that issue in 2023… https://tracker.moodle.org/browse/MDL-54704
Thanks for this.
Moodle developers verified the code against automated checks yesterday (2023-02-28).
So maybe, this improvement will be released soon.
Cheers,