Score:3

How can I fix the "The specified database connection is not defined: default" error?

cn flag

I installed PHP 7.4.21, then Drupal 9.2.1, and Drush 10.5.0 with Composer as well.

Running Drush without arguments, I get the list of all the available commands.
Running drush cr gives me the following error.

The specified database connection is not defined: default

drush cr --root=/home/ip/d916/web gives the same error.

The output of drush cr -vvv is the following.

Exception trace

at /home/ip/d916/web/core/lib/Drupal/Core/Database/Database.php:371
Drupal\Core\Database\Database::openConnection() at /home/ip/d916/web/core/lib/Drupal/Core/Database/Database.php:169
Drupal\Core\Database\Database::getConnection() at n/a:n/a
call_user_func_array() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:258
Drupal\Component\DependencyInjection\Container->createService() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:176
Drupal\Component\DependencyInjection\Container->get() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:437
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:240
Drupal\Component\DependencyInjection\Container->createService() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:176
Drupal\Component\DependencyInjection\Container->get() at /home/ip/d916/web/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php:89
Drupal\Core\Cache\ChainedFastBackendFactory->get() at /home/ip/d916/web/core/lib/Drupal/Core/Cache/CacheFactory.php:83
Drupal\Core\Cache\CacheFactory->get() at n/a:n/a
call_user_func_array() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:258
Drupal\Component\DependencyInjection\Container->createService() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:176
Drupal\Component\DependencyInjection\Container->get() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:437 Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:240
Drupal\Component\DependencyInjection\Container->createService() at /home/ip/d916/web/core/lib/Drupal/Component/DependencyInjection/Container.php:176
Drupal\Component\DependencyInjection\Container->get() at /home/ip/d916/web/core/lib/Drupal/Core/DrupalKernel.php:586
Drupal\Core\DrupalKernel->preHandle() at /home/ip/d916/web/core/includes/utility.inc:35
drupal_rebuild() at /home/ip/d916/vendor/drush/drush/src/Commands/core/CacheCommands.php:234
Drush\Commands\core\CacheCommands->rebuild() at n/a:n/a
call_user_func_array() at /home/ip/d916/vendor/consolidation/annotated-command/src/CommandProcessor.php:257
Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback() at /home/ip/d916/vendor/consolidation/annotated-command/src/CommandProcessor.php:212
Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter() at /home/ip/d916/vendor/consolidation/annotated-command/src/CommandProcessor.php:176
Consolidation\AnnotatedCommand\CommandProcessor->process() at /home/ip/d916/vendor/consolidation/annotated-command/src/AnnotatedCommand.php:311
Consolidation\AnnotatedCommand\AnnotatedCommand->execute() at /home/ip/d916/vendor/symfony/console/Command/Command.php:255
Symfony\Component\Console\Command\Command->run() at /home/ip/d916/vendor/symfony/console/Application.php:1027
Symfony\Component\Console\Application->doRunCommand() at /home/ip/d916/vendor/symfony/console/Application.php:273
Symfony\Component\Console\Application->doRun() at /home/ip/d916/vendor/symfony/console/Application.php:149
Symfony\Component\Console\Application->run() at /home/ip/d916/vendor/drush/drush/src/Runtime/Runtime.php:118
Drush\Runtime\Runtime->doRun() at /home/ip/d916/vendor/drush/drush/src/Runtime/Runtime.php:48
Drush\Runtime\Runtime->run() at /home/ip/d916/vendor/drush/drush/drush.php:72
require() at /home/ip/d916/vendor/drush/drush/drush:4

Composer works without any mistake. Drupal work fine too. Unfortunately I can't work without Drush 10.

How can I fix the The specified database connection is not defined: default error?

miststudent2011 avatar
fr flag
Can you share database details from your settings.php. It seems you haven't configured it properly
hotwebmatter avatar
nr flag
Please share the output of `drush status`.
apaderno avatar
us flag
As side note: The previous comments are asking to give that information in the question, not in answers. Answers aren't used to give more information to answer the question. Stack Exchange sites aren't forums, but Q&A sites: A user asks a question that other users answer.
Score:2
nr flag

You say that Drupal works fine. Do you really mean that your Drupal 9.2.1 website is fully functional without a database connection? That is surprising!

You can start by connecting to this environment via ssh and testing a connection to the database using the command-line mysql utility and the database authentication credentials stored in your settings.php file.

If you get a successful connection, run some diagnostic commands like USE `drupal`; (replacing drupal with the DB name specified in your settings) and SHOW TABLES; and SELECT COUNT(*) FROM `node`; (just be very careful not to issue any commands that insert, update, or delete any data!)

If Drupal really can connect to the database, and you can connect in this way from the command line, then you will have narrowed the problem down to Drush.

You can make sure that you are really running the correct project-local version of Drush 10 that you installed with Composer by executing which drush at the command prompt to see if you are instead using a gloabally installed Drush, perhaps even a different version.

If that is the case, please specify the full path to your local Drush to be sure you are executing the correct Drush PHAR:

$ cd /home/ip/d916/
$ ./vendor/bin/drush status
$ ./vendor/bin/drush cr

If these commands work for you, then you have different options depending on your hosting. The Drush docs are excellent, and will help you to decide the correct path forward.

apaderno avatar
us flag
It could also be that Drush is executed from the wrong directory and it doesn't find the settings.php file, or it finds a settings.php file that doesn't contain the database connection information, or the correct database connection information. This would explain why the Drupal site works but Drush reports an error.
cn flag
Drush was the problem. The downloaded 10.5.0 called by an alias didn't work for me. After runnning `composer require drush/drush` and `echo export PATH="$HOME/.composer/vendor/bin:$PATH` I had the correct version and both Drush and Drupal work fine.
LeraA avatar
cz flag
I want to add my experience today to this question/answer. I had the exact same problem as listed in the question, though the op's final answer didn't solve it for me. The steps inside this upvoted answer DID help. I could see that no matter what I did, I could not get a connection to the DB. None of my colleagues had the error I had. I ended up chucking the entire repo and reinstalling it. Same code, same steps, same files, no manual changes, but this time the DB connection formed. Drush commands worked and the site came up first try. Go figure. Hope this helps someone else.
Score:1
in flag

If you have an extra file with your local settings, like for example settings.local.php, check if it is placed in the correct directory.

In this case it must be in the sites/default/ directory, not in the sites/ root folder. Most likely the error is caused by a misplaced configuration file.

If that's not the case, or you have only one settings.php, check if you have a valid configuration for your database in the $databases[][] array in it, for the "default" site it must be named "$databases['default']['default']" and must contain all your database information.

Example:

## database settings
$databases['default']['default'] = [
  'database' => 'drupal9',
  'username' => 'drupal9',
  'password' => 'drupal9',
  'prefix' => '',
  'host' => 'database',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
];
Score:0
br flag

If you're using local development, such as with Vagrant, Lando, or DDEV, be sure you've established a shell connection to your environment using a command such as vagrant ssh, lando ssh, or ddev ssh, then run your commands from within your SSH session. Otherwise, the Drupal Console command can't connect to the database.

This is true for any other commands like drush that require database access, although many local development environments have shortcuts like ddev drush that handle that connection for you. Drupal console does not have such a shortcut, so an SSH connection to your local environment is essential.

Score:0
th flag

I ran into this recently. If this is a shared host, or a multisite setup, you will need to include the --uri=https://[example-domain.com] parameter with your drush command. That's how it is going to find your particular site's db connection info.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.