Issue #2529188 by stefan.r, reevo, vijaycs85, alexpott, Crell: Provide better error handling for MySQL client and server utf8mb4 incompatibility

8.0.x
Alex Pott 2015-08-12 23:58:42 +01:00
parent 3350c0ca6f
commit 70dfc3bef9
4 changed files with 86 additions and 12 deletions

View File

@ -1233,6 +1233,13 @@ abstract class Connection {
return $this->connection->getAttribute(\PDO::ATTR_SERVER_VERSION);
}
/**
* Returns the version of the database client.
*/
public function clientVersion() {
return $this->connection->getAttribute(\PDO::ATTR_CLIENT_VERSION);
}
/**
* Determines if this driver supports transactions.
*

View File

@ -28,6 +28,11 @@ class Connection extends DatabaseConnection {
*/
const DATABASE_NOT_FOUND = 1049;
/**
* Error code for "Can't initialize character set" error.
*/
const UNSUPPORTED_CHARSET = 2019;
/**
* Flag to indicate if the cleanup function in __destruct() should run.
*
@ -82,6 +87,13 @@ class Connection extends DatabaseConnection {
* {@inheritdoc}
*/
public static function open(array &$connection_options = array()) {
if (isset($connection_options['_dsn_utf8_fallback']) && $connection_options['_dsn_utf8_fallback'] === TRUE) {
// Only used during the installer version check, as a fallback from utf8mb4.
$charset = 'utf8';
}
else {
$charset = 'utf8mb4';
}
// The DSN should use either a socket or a host/port.
if (isset($connection_options['unix_socket'])) {
$dsn = 'mysql:unix_socket=' . $connection_options['unix_socket'];
@ -93,7 +105,7 @@ class Connection extends DatabaseConnection {
// Character set is added to dsn to ensure PDO uses the proper character
// set when escaping. This has security implications. See
// https://www.drupal.org/node/1201452 for further discussion.
$dsn .= ';charset=utf8mb4';
$dsn .= ';charset=' . $charset;
if (!empty($connection_options['database'])) {
$dsn .= ';dbname=' . $connection_options['database'];
}
@ -124,10 +136,10 @@ class Connection extends DatabaseConnection {
// certain one has been set; otherwise, MySQL defaults to
// 'utf8mb4_general_ci' for utf8mb4.
if (!empty($connection_options['collation'])) {
$pdo->exec('SET NAMES utf8mb4 COLLATE ' . $connection_options['collation']);
$pdo->exec('SET NAMES ' . $charset . ' COLLATE ' . $connection_options['collation']);
}
else {
$pdo->exec('SET NAMES utf8mb4');
$pdo->exec('SET NAMES ' . $charset);
}
// Set MySQL init_commands if not already defined. Default Drupal's MySQL

View File

@ -16,6 +16,17 @@ use Drupal\Core\Database\DatabaseNotFoundException;
* Specifies installation tasks for MySQL and equivalent databases.
*/
class Tasks extends InstallTasks {
/**
* Minimum required MySQLnd version.
*/
const MYSQLND_MINIMUM_VERSION = '5.0.9';
/**
* Minimum required libmysqlclient version.
*/
const LIBMYSQLCLIENT_MINIMUM_VERSION = '5.5.3';
/**
* The PDO driver name for MySQL and equivalent databases.
*
@ -27,13 +38,6 @@ class Tasks extends InstallTasks {
* Constructs a \Drupal\Core\Database\Driver\mysql\Install\Tasks object.
*/
public function __construct() {
$this->tasks[] = array(
'arguments' => array(
'SET NAMES utf8mb4',
'The %name database server supports utf8mb4 character encoding.',
'The %name database server must support utf8mb4 character encoding to work with Drupal. Make sure to use a database server that supports utf8mb4 character encoding, such as MySQL/MariaDB/Percona versions 5.5.3 and up.',
),
);
$this->tasks[] = array(
'arguments' => array(),
'function' => 'ensureInnoDbAvailable',
@ -62,7 +66,34 @@ class Tasks extends InstallTasks {
// This doesn't actually test the connection.
db_set_active();
// Now actually do a check.
Database::getConnection();
try {
Database::getConnection();
}
catch (\Exception $e) {
// Detect utf8mb4 incompability.
if ($e->getCode() == Connection::UNSUPPORTED_CHARSET) {
$this->fail(t('Your MySQL server and PHP MySQL driver must support utf8mb4 character encoding. Make sure to use a database system that supports this (such as MySQL/MariaDB/Percona 5.5.3 and up), and that the utf8mb4 character set is compiled in. See the <a href="@documentation" target="_blank">MySQL documentation</a> for more information.', array('@documentation' => 'https://dev.mysql.com/doc/refman/5.0/en/cannot-initialize-character-set.html')));
$info = Database::getConnectionInfo();
$info_copy = $info;
// Set a flag to fall back to utf8. Note: this flag should only be
// used here and is for internal use only.
$info_copy['default']['_dsn_utf8_fallback'] = TRUE;
// In order to change the Database::$databaseInfo array, we need to
// remove the active connection, then re-add it with the new info.
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $info_copy['default']);
// Connect with the new database info, using the utf8 character set so
// that we can run the checkEngineVersion test.
Database::getConnection();
// Revert to the old settings.
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $info['default']);
}
else {
// Rethrow the exception.
throw $e;
}
}
$this->pass('Drupal can CONNECT to the database ok.');
}
catch (\Exception $e) {
@ -121,4 +152,27 @@ class Tasks extends InstallTasks {
}
}
/**
* {@inheritdoc}
*/
protected function checkEngineVersion() {
parent::checkEngineVersion();
// Ensure that the MySQL driver supports utf8mb4 encoding.
$version = Database::getConnection()->clientVersion();
if (FALSE !== strpos($version, 'mysqlnd')) {
// The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
$version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
if (version_compare($version, self::MYSQLND_MINIMUM_VERSION, '<')) {
$this->fail(t("The MySQLnd driver version %version is less than the minimum required version. Upgrade to MySQLnd version %mysqlnd_minimum_version or up, or alternatively switch mysql drivers to libmysqlclient version %libmysqlclient_minimum_version or up.", array('%version' => Database::getConnection()->version(), '%mysqlnd_minimum_version' => self::MYSQLND_MINIMUM_VERSION, '%libmysqlclient_minimum_version' => self::LIBMYSQLCLIENT_MINIMUM_VERSION)));
}
}
else {
// The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
if (version_compare($version, self::LIBMYSQLCLIENT_MINIMUM_VERSION, '<')) {
$this->fail(t("The libmysqlclient driver version %version is less than the minimum required version. Upgrade to libmysqlclient version %libmysqlclient_minimum_version or up, or alternatively switch mysql drivers to MySQLnd version %mysqlnd_minimum_version or up.", array('%version' => Database::getConnection()->version(), '%libmysqlclient_minimum_version' => self::LIBMYSQLCLIENT_MINIMUM_VERSION, '%mysqlnd_minimum_version' => self::MYSQLND_MINIMUM_VERSION)));
}
}
}
}

View File

@ -192,8 +192,9 @@ abstract class Tasks {
* Check the engine version.
*/
protected function checkEngineVersion() {
// Ensure that the database server has the right version.
if ($this->minimumVersion() && version_compare(Database::getConnection()->version(), $this->minimumVersion(), '<')) {
$this->fail(t("The database version %version is less than the minimum required version %minimum_version.", array('%version' => Database::getConnection()->version(), '%minimum_version' => $this->minimumVersion())));
$this->fail(t("The database server version %version is less than the minimum required version %minimum_version.", array('%version' => Database::getConnection()->version(), '%minimum_version' => $this->minimumVersion())));
}
}