3

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?

3
  • 3
    Can you share database details from your settings.php. It seems you haven't configured it properly Commented Jul 17, 2021 at 16:36
  • Please share the output of drush status. Commented Jul 17, 2021 at 21:16
  • 1
    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.
    – avpaderno
    Commented Jul 18, 2021 at 12:37

5 Answers 5

2

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.

3
  • 1
    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.
    – avpaderno
    Commented Jul 18, 2021 at 12:34
  • 1
    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.
    – bolshii
    Commented Jul 26, 2021 at 12:06
  • 1
    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.
    – LeraA
    Commented May 18, 2022 at 13:09
2

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',
];
2

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.

1

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.

0

I had a same issue while executing a drush command. The reason behind my error was a missing directory alias in sites.php file. For ex: I had a site with "example.local.com" and I had created a site folder with the name "example". I had to add a directory alias in sites.php file to fix the issue.

$sites['example.local.com'] = 'example';

Hope this helps someone!!

Refer: https://www.techpond.in/drush-specified-database-connection-not-defined-default

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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