protected function MySQLiSource::_getConnection in Backup and Migrate 8.4
Get the db connection for the specified db.
Return value
\mysqli Connection object.
Throws
\Exception
5 calls to MySQLiSource::_getConnection()
- DrupalMySQLiSource::importFromFile in src/
Source/ DrupalMySQLiSource.php - Import to this source from the given backup file. This is the main restore function for this source.
- MySQLiSource::exportToFile in lib/
backup_migrate_core/ src/ Source/ MySQLiSource.php - Export this source to the given temp file. This should be the main back up function for this source.
- MySQLiSource::importFromFile in lib/
backup_migrate_core/ src/ Source/ MySQLiSource.php - Import to this source from the given backup file. This is the main restore function for this source.
- MySQLiSource::query in lib/
backup_migrate_core/ src/ Source/ MySQLiSource.php - Run a db query on this destination's db.
- MySQLiSource::_dbInfo in lib/
backup_migrate_core/ src/ Source/ MySQLiSource.php - Get the version info for the given DB.
File
- lib/
backup_migrate_core/ src/ Source/ MySQLiSource.php, line 120
Class
- MySQLiSource
- Class MySQLiSource.
Namespace
BackupMigrate\Core\SourceCode
protected function _getConnection() {
if (!$this->connection) {
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
throw new BackupMigrateException('Cannot connect to the database because the MySQLi extension is missing.');
}
$pdo_config = $this
->confGet('pdo');
$ssl_config = [
'key' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_KEY]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_KEY] : NULL,
'cert' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CERT]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CERT] : NULL,
'ca' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CA]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CA] : NULL,
'capath' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CAPATH]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CAPATH] : NULL,
'cypher' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CIPHER]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CIPHER] : NULL,
];
if (defined('PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')) {
$ssl_config['verify_server_cert'] = isset($pdo_config[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] : TRUE;
}
else {
$ssl_config['verify_server_cert'] = TRUE;
}
if ($ssl_config['key'] || $ssl_config['cert'] || $ssl_config['ca'] || $ssl_config['capath'] || $ssl_config['cypher']) {
// Provide a workaround for PHP7 peer certificate verification issues:
// - https://bugs.php.net/bug.php?id=68344
// - https://bugs.php.net/bug.php?id=71003
if ($ssl_config['verify_server_cert']) {
$flags = MYSQLI_CLIENT_SSL;
}
else {
$flags = MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
}
// Connect using PDO SSL config.
$this->connection = new \mysqli();
$this->connection
->ssl_set($ssl_config['key'], $ssl_config['cert'], $ssl_config['ca'], $ssl_config['capath'], $ssl_config['cypher']);
$this->connection
->real_connect($this
->confGet('host'), $this
->confGet('username'), $this
->confGet('password'), $this
->confGet('database'), $this
->confGet('port'), $this
->confGet('socket'), $flags);
}
else {
$this->connection = new \mysqli($this
->confGet('host'), $this
->confGet('username'), $this
->confGet('password'), $this
->confGet('database'), $this
->confGet('port'), $this
->confGet('socket'));
}
// Throw an error on fail.
if ($this->connection->connect_errno || !$this->connection
->ping()) {
throw new BackupMigrateException("Failed to connect to MySQL server.");
}
// Ensure, that the character set is utf8mb4.
if (!$this->connection
->set_charset('utf8mb4')) {
throw new BackupMigrateException('UTF8 is not supported by the MySQL server.');
}
}
return $this->connection;
}