3 namespace Drupal\Core\Database\Driver\mysql\Install;
5 use Drupal\Core\Database\Install\Tasks as InstallTasks;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\Driver\mysql\Connection;
8 use Drupal\Core\Database\DatabaseNotFoundException;
11 * Specifies installation tasks for MySQL and equivalent databases.
13 class Tasks extends InstallTasks {
16 * Minimum required MySQLnd version.
18 const MYSQLND_MINIMUM_VERSION = '5.0.9';
21 * Minimum required libmysqlclient version.
23 const LIBMYSQLCLIENT_MINIMUM_VERSION = '5.5.3';
26 * The PDO driver name for MySQL and equivalent databases.
30 protected $pdoDriver = 'mysql';
33 * Constructs a \Drupal\Core\Database\Driver\mysql\Install\Tasks object.
35 public function __construct() {
38 'function' => 'ensureInnoDbAvailable',
45 public function name() {
46 return t('MySQL, MariaDB, Percona Server, or equivalent');
52 public function minimumVersion() {
59 protected function connect() {
61 // This doesn't actually test the connection.
63 // Now actually do a check.
65 Database::getConnection();
67 catch (\Exception $e) {
68 // Detect utf8mb4 incompability.
69 if ($e->getCode() == Connection::UNSUPPORTED_CHARSET || ($e->getCode() == Connection::SQLSTATE_SYNTAX_ERROR && $e->errorInfo[1] == Connection::UNKNOWN_CHARSET)) {
70 $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.', [':documentation' => 'https://dev.mysql.com/doc/refman/5.0/en/cannot-initialize-character-set.html']));
71 $info = Database::getConnectionInfo();
73 // Set a flag to fall back to utf8. Note: this flag should only be
74 // used here and is for internal use only.
75 $info_copy['default']['_dsn_utf8_fallback'] = TRUE;
76 // In order to change the Database::$databaseInfo array, we need to
77 // remove the active connection, then re-add it with the new info.
78 Database::removeConnection('default');
79 Database::addConnectionInfo('default', 'default', $info_copy['default']);
80 // Connect with the new database info, using the utf8 character set so
81 // that we can run the checkEngineVersion test.
82 Database::getConnection();
83 // Revert to the old settings.
84 Database::removeConnection('default');
85 Database::addConnectionInfo('default', 'default', $info['default']);
88 // Rethrow the exception.
92 $this->pass('Drupal can CONNECT to the database ok.');
94 catch (\Exception $e) {
95 // Attempt to create the database if it is not found.
96 if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
97 // Remove the database string from connection info.
98 $connection_info = Database::getConnectionInfo();
99 $database = $connection_info['default']['database'];
100 unset($connection_info['default']['database']);
102 // In order to change the Database::$databaseInfo array, need to remove
103 // the active connection, then re-add it with the new info.
104 Database::removeConnection('default');
105 Database::addConnectionInfo('default', 'default', $connection_info['default']);
108 // Now, attempt the connection again; if it's successful, attempt to
109 // create the database.
110 Database::getConnection()->createDatabase($database);
111 Database::closeConnection();
113 // Now, restore the database config.
114 Database::removeConnection('default');
115 $connection_info['default']['database'] = $database;
116 Database::addConnectionInfo('default', 'default', $connection_info['default']);
118 // Check the database connection.
119 Database::getConnection();
120 $this->pass('Drupal can CONNECT to the database ok.');
122 catch (DatabaseNotFoundException $e) {
123 // Still no dice; probably a permission issue. Raise the error to the
125 $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', ['%database' => $database, '%error' => $e->getMessage()]));
129 // Database connection failed for some other reason than the database
131 $this->fail(t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist or does the database user have sufficient privileges to create the database?</li><li>Have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', ['%error' => $e->getMessage()]));
141 public function getFormOptions(array $database) {
142 $form = parent::getFormOptions($database);
143 if (empty($form['advanced_options']['port']['#default_value'])) {
144 $form['advanced_options']['port']['#default_value'] = '3306';
151 * Ensure that InnoDB is available.
153 public function ensureInnoDbAvailable() {
154 $engines = Database::getConnection()->query('SHOW ENGINES')->fetchAllKeyed();
155 if (isset($engines['MyISAM']) && $engines['MyISAM'] == 'DEFAULT' && !isset($engines['InnoDB'])) {
156 $this->fail(t('The MyISAM storage engine is not supported.'));
163 protected function checkEngineVersion() {
164 parent::checkEngineVersion();
166 // Ensure that the MySQL driver supports utf8mb4 encoding.
167 $version = Database::getConnection()->clientVersion();
168 if (FALSE !== strpos($version, 'mysqlnd')) {
169 // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
170 $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
171 if (version_compare($version, self::MYSQLND_MINIMUM_VERSION, '<')) {
172 $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.", ['%version' => $version, '%mysqlnd_minimum_version' => self::MYSQLND_MINIMUM_VERSION, '%libmysqlclient_minimum_version' => self::LIBMYSQLCLIENT_MINIMUM_VERSION]));
176 // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
177 if (version_compare($version, self::LIBMYSQLCLIENT_MINIMUM_VERSION, '<')) {
178 $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.", ['%version' => $version, '%libmysqlclient_minimum_version' => self::LIBMYSQLCLIENT_MINIMUM_VERSION, '%mysqlnd_minimum_version' => self::MYSQLND_MINIMUM_VERSION]));