1

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 Jul 17 at 16:36
  • Please share the output of drush status. Jul 17 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.
    – apaderno
    Jul 18 at 12:37
0

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.

2
  • 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.
    – apaderno
    Jul 18 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
    Jul 26 at 12:06
1

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

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.