Drupal are working fine and no issue connect to the database. Default sources are there, however, it always "Failed to connect to MySQL server." when we perform a backup with database.

Here is the part of the error log:
Warning: mysqli::__construct(): (HY000/9002): SSL connection is required. Please specify SSL options and retry. in BackupMigrate\Core\Source\MySQLiSource->_getConnection() (line 136 of /modules/contrib/backup_migrate/lib/backup_migrate_core/src/Source/MySQLiSource.php) #0 /core/includes/bootstrap.inc(582): _drupal_error_handler_real(2, 'mysqli::__const..

Database using Azure MySQL, and due to policy, it been enforced only SSL connection. Hence, suspecting module unable establish the Database connection due to not including SSL connection.
https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl

Additional testing had been conducted which migrate database to without enforced SSL connection, and it is working fine.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cl.choong created an issue. See original summary.

maki3000’s picture

I think you have to setup your "Sources" first before you can backup your database. Go to the configuration of the Backup and Migrate module, then to Settings and then to Sources (I guess this is the right naming. I'm not 100% sure because my Drupal is in German).

What I wonder however is, why the default sources aren't there anymore. In an older version of this module, the main backup sources were predefined in place, like default_db, entire_site, private_files and public_files. What do I have to do that these default sources are there again?

cl.choong’s picture

Issue summary: View changes
cl.choong’s picture

Issue summary: View changes
TedWS’s picture

I also experiencing the same issue backup and migrate with SSL.
@maki3000
I tried setup the configuration in "Sources", but i get the error as below:
Warning: mysqli::__construct(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in BackupMigrate\Core\Source\MySQLiSource->_getConnection()
I also tried using the ipaddress instead of the domain name, but i still get the same error.

Alex Andrascu’s picture

Category: Support request » Feature request

Ok. That's a good point. Let's look into getting those default sources back in.

jacklee0410’s picture

Hi, I have face the same problem as well.
I have found something below.

$db = mysqli_init();
mysqli_options ($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);

$db->ssl_set('/etc/mysql/ssl/client-key.pem', '/etc/mysql/ssl/client-cert.pem', '/etc/mysql/ssl/ca-cert.pem', NULL, NULL);
$link = mysqli_real_connect ($db, 'ip', 'user', 'pass', 'db', 3306, NULL, MYSQLI_CLIENT_SSL);
if (!$link)
{
    die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
} else {
    $res = $db->query('SHOW TABLES;');
    print_r ($res);
    $db->close();
}

but i'm not sure how to fit this code into the backup and migrate module.

Can anyone help?

jacklee0410’s picture

Hi, I have found a solution for ssl method. however it is a hardcode currently. After modified this code below in "backup_migrate/lib/backup_migrate_core/src/Source/MySQLiSource.php" it should work with ssl connection.

line 127
       if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
         throw new BackupMigrateException('Cannot connect to the database becuase the MySQLi extension is missing.');
      }
/* start from here*/
 $connection_ssl = new \mysqli;
       $connection_ssl->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
       $connection_ssl->ssl_set(NULL, NULL, "/etc/ssl/certs/ca.pem", NULL, NULL);
       $connection_ssl->real_connect(
         $this->confGet('host'),
         $this->confGet('username'),
         $this->confGet('password'),
         $this->confGet('database'),
         $this->confGet('port'),
         $this->confGet('socket'),
         MYSQLI_CLIENT_SSL
       );
       $this->connection = $connection_ssl;
/* end*/

/* remove the code below*/
      //     $this->connection = new \mysqli(
      //     $this->confGet('host'),
      //     $this->confGet('username'),
      //     $this->confGet('password'),
      //     $this->confGet('database'),
      //     $this->confGet('port'),
      //     $this->confGet('socket')
      // );
ashlewis’s picture

FileSize
2.65 KB

Here's a patch to use the PDO SSL config from settings.php if present, e.g.

$databases['default']['default'] = array (
  'database' => '<DATABASE>',
  'username' => '<USER>',
  'password' => '<PASSWORD>',
  'prefix' => '',
  'host' => '<HOST>',
  'port' => '',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
  'pdo' => array(
    PDO::MYSQL_ATTR_SSL_KEY => '/path/to/client-key.pem',
    PDO::MYSQL_ATTR_SSL_CERT => '/path/to/client-cert.pem',
    PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca-cert.pem',
  ),
);
ashlewis’s picture

FileSize
2.97 KB

Sorry, the first patch was using SSL connection even when not configured - updated patch attached.

ashlewis’s picture

FileSize
3 KB

and here's another one that composer is able to apply

DamienMcKenna’s picture

Status: Active » Needs work

@ashlewis: That's a great idea, thanks.

Could you please update the patch to just store $this->confGet('pdo') in an array? That'd greatly simplify the rest of the code.

ashlewis’s picture

Agreed it looks a bit messy. However, $this->confGet('pdo') returns a populated array even when ssl values are not set in settings.php and i don't know enough about this to know which of the pdo config parameters may or may not be populated in settings.php, i used what drush does (https://github.com/drush-ops/drush/pull/2131/commits/3d4fab4051d22e9fbea...) as example.

ashlewis’s picture

FileSize
2.74 KB

Here you go, managed to tidy it up a bit.

DamienMcKenna’s picture

Status: Needs work » Needs review

That's awesome, thank you!

Lets see what the testbot says.

ashlewis’s picture

FileSize
3.17 KB

Here's another patch which provides a workaround for PHP7's peer certificate verification issues (see: https://bugs.php.net/bug.php?id=68344 and https://bugs.php.net/bug.php?id=71003)

ashlewis’s picture

And another one that doesn't break on older versions of php where PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT hasn't been introduced

jacklee0410’s picture

#17 patches it works. Thanks @ashlewis

DamienMcKenna’s picture

Presuming the tests still work fine, this should be good to go. Thank you.

DamienMcKenna’s picture

DamienMcKenna’s picture

Status: Needs review » Fixed

Committed. Thank you.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.