7

We run Wordpress in a sub folder of our main .NET solution on a cloud service. We have moved our MySQL from CloudDB to Azure MySQL, however it will only connect if we set the "Enforce SSL Connection" to disabled.

The Wordpress wp-config.php has the following

define('DB_SSL', true);

I presume the issue is we need to pass a certificate, but it is not clear to me how we can set this in Wordpress so it is passed when connecting over SSL.

4 Answers 4

3

Here is what I did:

  1. Obtain SSL certificate and save the certificate file to the root of my Wordpress project.

  2. Add the following into wp-config.php:

    define('DB_SSL', true);
    
  3. Add this to the function db_connect() in my wp-includes/wp-db.php. It must be called before mysqli_real_connect():

    // Just add this line
    mysqli_ssl_set($this->dbh, NULL, NULL, ABSPATH . 'BaltimoreCyberTrustRoot.crt.pem', NULL, NULL); 
    
    if ( WP_DEBUG ) {
        mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    } else {
        @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    }
    

The solution seems a bit dirty but it works for me.

3
  • 2
    Modifying the core files of WP is not advised. What happens when WP is updated? Do we need to do this every time we upgrade WP?
    – Nitesh
    Dec 27, 2017 at 20:14
  • Yes, we do need. But do you know any other solution without changing wp-db.php file to make this work?
    – Aaron Chen
    Dec 28, 2017 at 1:30
  • 1
    I agree @Nitesh but don't see any alternative. As Aaron said, the solution is a bit dirty as it could be overwritten but it does at least solve the question asked.
    – Jezbers
    Dec 31, 2017 at 7:36
3

This will solve the issue by adding these lines to wp_config.php

define(‘MYSQL_CLIENT_FLAGS’, MYSQLI_CLIENT_SSL);
define(‘MYSQL_SSL_CA’, getenv(‘MYSQL_SSL_CA’));
define(‘MYSQL_CLIENT_FLAGS’, MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT );
1
  • Thanks Anurag, I will try this, although it's now quite a long way down on my to do list, hence the slow reply (apologies). Appreciate your input and will give this a try.
    – Jezbers
    Jun 27, 2018 at 19:53
2

I found the solution to be much simpler when using an Azure mySQL database server in conjunction with a Wordpress container image. If you're using a VPS this may not apply.

1
  • in my case it worked without adding define('MYSQL_SSL_CA_PATH','/'); May 15, 2018 at 13:27
1

The solutions that work for me, add to wp-config.php:

PHP7.x

define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL | 
MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT ); 
define( 'MYSQL_SSL_CA', getenv('MYSQL_SSL_CA'));

PHP5.x

define('MYSQL_CLIENT_FLAGS', MYSQL_CLIENT_SSL);
define( 'MYSQL_SSL_CA', getenv('MYSQL_SSL_CA'));

Source: https://jkudo.medium.com/how-to-connection-from-wordpress-installed-on-azure-app-service-to-azure-database-for-mysql-via-2b2c37c4a7de

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.