-1

I am trying to install a Drupal site locally on a new laptop that was working fine on the old one. I get the following errors.

Notice: Undefined offset: 2 in Drupal\Core\Database\Database::parseConnectionInfo() (line 215 of /var/www/html/docroot/core/lib/Drupal/Core/Database/Database.php)

Notice: Undefined index: driver in Drupal\Core\Database\Database::openConnection() (line 367 of /var/www/html/docroot/core/lib/Drupal/Core/Database/Database.php).

Drupal\Core\Database\DriverNotSpecifiedException: Driver not specified for this database connection: default in Drupal\Core\Database\Database::openConnection() (line 368 of /var/www/html/docroot/core/lib/Drupal/Core/Database/Database.php).

It's running inside Docker, PHP version is 7.3.27 and Drupal version 8.9.

3
  • Can somebody explain why this question is getting downvoted? I'm not a backend or Drupal dev, apologies if the question is stupid, but I've been wrestling with this install for 3 weeks now and need help. – Derek Johnson Apr 29 at 9:13
  • Haven't voted so just guessing - there's no information on how to reproduce the issue, and the errors are pretty generic. Abstractly it might be suggesting the database connection array is missing or incomplete, but it's hard to say – Clive Apr 29 at 9:20
  • 1
    I haven't voted either, but I guess the down-vote comes from a user who doesn't see any effort in looking for a solution, or doesn't think the question gives enough details. I answered the question because I think the causes can just be two: The settings.php file contains the wrong information, or a module tries to add a connection information passing a wrong array. – apaderno Apr 29 at 11:32
2

The notices and the exception are caused from the connection array not setting the driver property. In fact, Database::parseConnectionInfo() tries to access a numeric index of the array it gets with the following code.

// If there is no "driver" property, then we assume it's an array of
// possible connections for this target. Pick one at random. That allows
// us to have, for example, multiple replica servers.
if (empty($info['driver'])) {
  $info = $info[mt_rand(0, count($info) - 1)];
}

That code assumes that the array defining available connections is similar to the following one, in the case there are more connections for that target.

$databases['default']['replica'][] = [
  'database' => 'databasename1',
  'username' => 'sqlusername1',
  'password' => 'sqlpassword1',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
];
$databases['default']['replica'][] = [
  'database' => 'databasename2',
  'username' => 'sqlusername2',
  'password' => 'sqlpassword2',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
];

It cannot be the following one because the code is expecting the indexes for $databases['default']['replica'] to be between 0 and 1.

$databases['default']['replica'][3] = [
  'database' => 'databasename1',
  'username' => 'sqlusername1',
  'password' => 'sqlpassword1',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
];
$databases['default']['replica'][4] = [
  'database' => 'databasename2',
  'username' => 'sqlusername2',
  'password' => 'sqlpassword2',
  'host' => 'localhost',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
];

Basing on the first notice message, this seems the case: The array containing the connection information, which is contained in the settings.php file, or which is passed to Database::addConnectionInfo() from a module, doesn't contain all the necessary information, starting from the driver property, and contains the wrong information.
In the case the notices and the exception are caused from Drupal when it reads the connection information from the settings.php file, that file could be corrupted, which would explain why it works in a case, but not in the other one.

As for why the connection settings work in one case but not in the other one, assuming that the part of the settings.php file defining the available connections is the same in both the case, or that the settings.php file isn't corrupted, that could happen because:

  • The settings.php file used is different
  • A module is adding information for a new connection in one case, and it's using a malformed array

Where Drupal looks for the settings.php file is explained in default.settings.php, which explains that, for a https://www.drupal.org:8080/mysite/test/ site, Drupal will look into the following directories.

  • sites/8080.www.drupal.org.mysite.test
  • sites/www.drupal.org.mysite.test
  • sites/drupal.org.mysite.test
  • sites/org.mysite.test
  • sites/8080.www.drupal.org.mysite
  • sites/www.drupal.org.mysite
  • sites/drupal.org.mysite
  • sites/org.mysite
  • sites/8080.www.drupal.org
  • sites/www.drupal.org
  • sites/drupal.org
  • sites/org
  • sites/default

The default directory, sites/default is the last directory where Drupal searches the settings.php file. It's sufficient that another directory matching the domain name / port number is found that Drupal will use a different settings.php.

In the case the connection is added by a module, it could be that module is installed in one case, but not in the other one. Drupal looks for third-party modules in different directories, as described in Directory Structure. ([domain] means the domain name used to access the site.)

  • /modules
  • sites/[domain]/modules
  • sites/default/modules
2
  • Thanks for such a detailed answer. I'll work through it today and see how I get on. Appreciate you taking the time – Derek Johnson Apr 30 at 8:13
  • This got me in a new and hopefully correct direction. I can now see that Docker isn't reading the env variables, of which driver is one. Thanks again. – Derek Johnson May 4 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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