An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused

Nextcloud version (eg, 29.0.5): 27.02
Operating system and version (eg, Ubuntu 24.04): Linux 5.15.0-117-generic x86_64
Apache or nginx version (eg, Apache 2.4.25): Apache/2.4.52
PHP version (eg, 8.3): 8.1.2-1ubuntu2.18

The issue you are facing:
Everything seems to be working fine but the admin log is full of this error.

Is this the first time you’ve seen this error? X(Y/N)_:

Steps to replicate it:

  1. Rebooted server
  2. Verified MariaDB is running

The output of your Nextcloud log in Admin > Logging:

Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused

0. /var/www/html/3rdparty/doctrine/dbal/src/Connection.php - line 1531:OC\DB\Connection->connect()
1. /var/www/html/3rdparty/doctrine/dbal/src/Connection.php - line 1029:Doctrine\DBAL\Connection->getWrappedConnection()
2. /var/www/html/lib/private/DB/Connection.php - line 262:Doctrine\DBAL\Connection->executeQuery()
3. /var/www/html/3rdparty/doctrine/dbal/src/Query/QueryBuilder.php - line 345:OC\DB\Connection->executeQuery()
4. /var/www/html/lib/private/DB/QueryBuilder/QueryBuilder.php - line 280:Doctrine\DBAL\Query\QueryBuilder->execute()
5. /var/www/html/lib/private/Files/Cache/Cache.php - line 168:OC\DB\QueryBuilder\QueryBuilder->execute()
6. /var/www/html/lib/private/Files/View.php - line 1306:OC\Files\Cache\Cache->get()
7. /var/www/html/lib/private/Files/View.php - line 1356:OC\Files\View->getCacheEntry()
8. /var/www/html/lib/private/Files/Node/Root.php - line 202:OC\Files\View->getFileInfo()
9. <<closure>>OC\Files\Node\Root->get()
10. /var/www/html/lib/private/Files/Node/LazyFolder.php - line 74:call_user_func_array()
11. /var/www/html/lib/private/Files/Node/LazyFolder.php - line 151:OC\Files\Node\LazyFolder->__call()
12. /var/www/html/lib/private/Files/AppData/AppData.php - line 132:OC\Files\Node\LazyFolder->get()
13. /var/www/html/lib/private/Template/JSCombiner.php - line 87:OC\Files\AppData\AppData->getFolder()
14. /var/www/html/lib/private/Template/JSResourceLocator.php - line 145:OC\Template\JSCombiner->process()
15. /var/www/html/lib/private/Template/JSResourceLocator.php - line 75:OC\Template\JSResourceLocator->cacheAndAppendCombineJsonIfExist()
16. /var/www/html/lib/private/Template/ResourceLocator.php - line 73:OC\Template\JSResourceLocator->doFind()
17. /var/www/html/lib/private/TemplateLayout.php - line 377:OC\Template\ResourceLocator->find()
18. /var/www/html/lib/private/TemplateLayout.php - line 222:OC\TemplateLayout::findJavascriptFiles()
19. /var/www/html/lib/private/legacy/OC_Template.php - line 182:OC\TemplateLayout->__construct()
20. /var/www/html/lib/private/Template/Base.php - line 132:OC_Template->fetchPage()
21. /var/www/html/lib/private/legacy/OC_Template.php - line 331:OC\Template\Base->printPage()
22. /var/www/html/index.php - line 74:OC_Template::printExceptionErrorPage()

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => 'REMOVED',
  'passwordsalt' => 'REMOVED',
  'secret' => 'REMOVED',
  'trusted_domains' =>
  array (
    0 => 'REMOVED',
    1 => '172.30.1.110',
  ),
  'trusted_proxies' =>
  array (
    0 => '172.30.1.114',
  ),
  'overwrite.cli.url' => '',
  'overwritehost' => '',
  'overwriteprotocol' => 'https',
  'htaccess.RewriteBase' => '/',
  'datadirectory' => '/mnt/nasdata',
  'dbtype' => 'mysql',
  'version' => '27.0.2.1',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'ncadmin',
  'dbpassword' => '',
  'installed' => true,
  'maintenance' => false,
  'twofactor_enforced' => 'true',
  'twofactor_enforced_groups' =>
  array (
    0 => 'admin',
    1 => 'Users',
  ),
  'twofactor_enforced_excluded_groups' =>
  array (
  ),
  'mail_smtpmode' => 'smtp',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'REMOVED',
  'mail_domain' => 'gmail.com',
  'mail_smtphost' => 'smtp.gmail.com',
  'mail_smtpport' => '465',
  'mail_smtpauth' => 1,
  'mail_smtpname' => 'REMOVED',
  'mail_smtppassword' => 'REMOVED',
  'mail_smtpsecure' => 'ssl',
  'updater.secret' => 'REMOVED',
);

The output of your Apache/nginx/system log in /var/log/____:

[Thu Aug 01 08:36:37.100181 2024] [php:error] [pid 131552] [client 172.30.1.114:54634] PHP Fatal error:  Uncaught Doctrine\\DBAL\\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/lib/private/DB/Connection.php:140
Stack trace:
#0 /var/www/html/3rdparty/doctrine/dbal/src/Connection.php(1531): OC\\DB\\Connection->connect()
#1 /var/www/html/3rdparty/doctrine/dbal/src/Connection.php(813): Doctrine\\DBAL\\Connection->getWrappedConnection()
#2 /var/www/html/3rdparty/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php(323): Doctrine\\DBAL\\Connection->quote()
#3 /var/www/html/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php(422): Doctrine\\DBAL\\Query\\Expression\\ExpressionBuilder->literal()
#4 /var/www/html/lib/private/Lock/DBLockingProvider.php(258): OC\\DB\\QueryBuilder\\ExpressionBuilder\\ExpressionBuilder->literal()
#5 [internal function]: OC\\Lock\\DBLockingProvider->releaseAll()
#6 {main}
  thrown in /var/www/html/lib/private/DB/Connection.php on line 140

You are aware that a server usually contains to different PHP components, one which is executed via the web server, and one which is executed from the command line?

It might be possible that you can access the server via the web interface without an issue, because the database driver is correctly installed, but it fails if the cron.php e.g. calls php from the command line, because the module hasn’t been installed.

On the command line you can check which modules are being loade by executing the following command:

php -m | grep sql

If everything is working, it might be a mall configuration of your database. Maybe not enough resources to open enough database connections.


The password was only removed to avoid compromising it here, or is your database password not in the config.php?


There are so many log files that you can still check - even in real time. For example, those of your database. Find out why it refuses to connect every now and then.


Much and good luck,
ernolf

hi

change
‘dbhost’ => ‘localhost’,
to
‘dbhost’ => ‘127.0.0.1’,

for me it have worked. Spent 2 full days.

2 Likes

mysqli
mysqlnd
pdo_mysql

I removed the password before posting. It is in the config file.

Not work for me. Still this error:
Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused /var/www/html/lib/private/DB/Connection.php at 155

Hi there! Did you fix the error?
I have this one after change/move data directory.

Everything works ok! But it’s bothering me!