Nextcloud unable to connect to database after reboot. (can't connect to database)

[details=“Can’t connect to database”]

The Basics

  • Nextcloud Server version (e.g., 29.x.x):
    • ``31.0.6.2"
  • Operating system and version (e.g., Ubuntu 24.04):
    • Debian 12 - 6.8.12-11-pve
  • Web server and version (e.g, Apache 2.4.25):
    • Apache/2.4.62
  • Reverse proxy and version _(e.g. nginx 1.27.2)
    • none (LAN only)
  • PHP version (e.g, 8.3):
    • 8.2
  • Is this the first time you’ve seen this error? (Yes / No):
    • yes
  • When did this problem seem to first start?
    • last night while making several tweaks to config.php and php.ini
  • Installation method (e.g. AlO, NCP, Bare Metal/Archive, etc.)
    • Archive in an LXC (Turnkey Linux)
  • Are you using CloudfIare, mod_security, or similar? (Yes / No)
    • no

Summary of the issue you are facing:

Last night trying to fix several errors in the logs related to opcache and php.ini I made config edits and this morning, after a restart; I can’t seem to get nextcloud to start. I’ve grepped /var/www//nextclouddata/nextcloud.log for access errors from my endpoint and the only relevant thing I can find is this

{"reqId":"aF1gGlIOFFJrK1E6iWBIbgAAAAA","level":3,"time":"2025-06-26T14:58:35+00:00","remoteAddr":"10.203.1.4","user":"--","app":"core","method":"GET","url":"/index.php/204","message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory","userAgent":"Mozilla/5.0 (Windows) mirall/3.16.5 (build 20250602) (Nextcloud, windows-10.0.26100 ClientArchitecture: x86_64 OsArchitecture: x86_64)","version":"31.0.6.2","clientReqId":"dd950122-b46a-4ffc-8276-2aa88402c4b7","exception":{"Exception":"Doctrine\\DBAL\\Exception","Message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory","Code":2002,"Trace":[{"file":"/var/www/nextcloud/3rdparty/doctrine/dbal/src/Connection.php","line":458,"function":"connect","class":"OC\\DB\\Connection","type":"->"},{"file":"/var/www/nextcloud/3rdparty/doctrine/dbal/src/Connection.php","line":416,"function":"getDatabasePlatformVersion","class":"Doctrine\\DBAL\\Connection","type":"->"},{"file":"/var/www/nextcloud/3rdparty/doctrine/dbal/src/Connection.php","line":323,"function":"detectDatabasePlatform","class":"Doctrine\\DBAL\\Connection","type":"->"},{"file":"/var/www/nextcloud/lib/private/DB/Connection.php","line":903,"function":"getDatabasePlatform","class":"Doctrine\\DBAL\\Connection","type":"->"},{"file":"/var/www/nextcloud/lib/private/DB/ConnectionAdapter.php","line":235,"function":"getDatabaseProvider","class":"OC\\DB\\Connection","type":"->"},{"file":"/var/www/nextcloud/lib/private/DB/QueryBuilder/QueryBuilder.php","line":96,"function":"getDatabaseProvider","class":"OC\\DB\\ConnectionAdapter","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppConfig.php","line":1226,"function":"expr","class":"OC\\DB\\QueryBuilder\\QueryBuilder","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppConfig.php","line":243,"function":"loadConfig","class":"OC\\AppConfig","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppConfig.php","line":1366,"function":"searchValues","class":"OC\\AppConfig","type":"->"},{"file":"/var/www/nextcloud/lib/private/App/AppManager.php","line":136,"function":"getValues","class":"OC\\AppConfig","type":"->"},{"file":"/var/www/nextcloud/lib/private/App/AppManager.php","line":157,"function":"getInstalledAppsValues","class":"OC\\App\\AppManager","type":"->"},{"file":"/var/www/nextcloud/lib/private/legacy/OC_App.php","line":188,"function":"getInstalledApps","class":"OC\\App\\AppManager","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppFramework/Bootstrap/Coordinator.php","line":48,"function":"getEnabledApps","class":"OC_App","type":"::"},{"file":"/var/www/nextcloud/lib/base.php","line":675,"function":"runInitialRegistration","class":"OC\\AppFramework\\Bootstrap\\Coordinator","type":"->"},{"file":"/var/www/nextcloud/lib/base.php","line":1171,"function":"init","class":"OC","type":"::"},{"file":"/var/www/nextcloud/index.php","line":22,"args":["/var/www/nextcloud/lib/base.php"],"function":"require_once"}],"File":"/var/www/nextcloud/lib/private/DB/Connection.php","Line":237,"message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory","exception":{},"CustomMessage":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory"}}

Steps to replicate it (hint: details matter!):

Unsure on what exactly I changed; but I suspect syntax.

Nextcloud.log

{"reqId":"Cm3SWu64jhf6mAnLPnWy","level":3,"time":"2025-06-26T14:55:24+00:00","remoteAddr":"","user":"--","app":"richdocuments","method":"","url":"--","message":"Failed to fetch discovery: cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://nextcloud.bitnet.dev/apps/richdocumentscode/proxy.php?req=/hosting/discovery","userAgent":"--","version":"31.0.6.2","exception":{"Exception":"GuzzleHttp\\Exception\\ConnectException","Message":"cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://nextcloud.bitnet.dev/apps/richdocumentscode/proxy.php?req=/hosting/discovery","Code":0,"Trace":[{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Handler/CurlFactory.php","line":205,"function":"createRejection","class":"GuzzleHttp\\Handler\\CurlFactory","type":"::","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Handler/CurlFactory.php","line":157,"function":"finishError","class":"GuzzleHttp\\Handler\\CurlFactory","type":"::"},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Handler/CurlHandler.php","line":47,"function":"finish","class":"GuzzleHttp\\Handler\\CurlFactory","type":"::"},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Middleware.php","line":142,"function":"__invoke","class":"GuzzleHttp\\Handler\\CurlHandler","type":"->"},{"file":"/var/www/nextcloud/lib/private/Http/Client/DnsPinMiddleware.php","line":109,"function":"GuzzleHttp\\{closure}","class":"GuzzleHttp\\Middleware","type":"::","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php","line":35,"function":"OC\\Http\\Client\\{closure}","class":"OC\\Http\\Client\\DnsPinMiddleware","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Middleware.php","line":31,"function":"__invoke","class":"GuzzleHttp\\PrepareBodyMiddleware","type":"->"},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/RedirectMiddleware.php","line":71,"function":"GuzzleHttp\\{closure}","class":"GuzzleHttp\\Middleware","type":"::","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Middleware.php","line":66,"function":"__invoke","class":"GuzzleHttp\\RedirectMiddleware","type":"->"},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/HandlerStack.php","line":75,"function":"GuzzleHttp\\{closure}","class":"GuzzleHttp\\Middleware","type":"::","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Client.php","line":333,"function":"__invoke","class":"GuzzleHttp\\HandlerStack","type":"->"},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Client.php","line":169,"function":"transfer","class":"GuzzleHttp\\Client","type":"->"},{"file":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Client.php","line":189,"function":"requestAsync","class":"GuzzleHttp\\Client","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/lib/private/Http/Client/Client.php","line":206,"function":"request","class":"GuzzleHttp\\Client","type":"->"},{"file":"/var/www/nextcloud/apps/richdocuments/lib/Service/DiscoveryService.php","line":59,"function":"get","class":"OC\\Http\\Client\\Client","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/apps/richdocuments/lib/Service/CachedRequestService.php","line":74,"function":"sendRequest","class":"OCA\\Richdocuments\\Service\\DiscoveryService","type":"->"},{"file":"/var/www/nextcloud/apps/richdocuments/lib/Backgroundjobs/ObtainCapabilities.php","line":41,"function":"fetch","class":"OCA\\Richdocuments\\Service\\CachedRequestService","type":"->"},{"file":"/var/www/nextcloud/lib/public/BackgroundJob/Job.php","line":61,"function":"run","class":"OCA\\Richdocuments\\Backgroundjobs\\ObtainCapabilities","type":"->"},{"file":"/var/www/nextcloud/lib/public/BackgroundJob/TimedJob.php","line":97,"function":"start","class":"OCP\\BackgroundJob\\Job","type":"->"},{"file":"/var/www/nextcloud/lib/public/BackgroundJob/TimedJob.php","line":84,"function":"start","class":"OCP\\BackgroundJob\\TimedJob","type":"->"},{"file":"/var/www/nextcloud/cron.php","line":170,"function":"execute","class":"OCP\\BackgroundJob\\TimedJob","type":"->"}],"File":"/var/www/nextcloud/3rdparty/guzzlehttp/guzzle/src/Handler/CurlFactory.php","Line":275,"message":"Failed to fetch discovery: cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://nextcloud.bitnet.dev/apps/richdocumentscode/proxy.php?req=/hosting/discovery","exception":{},"CustomMessage":"Failed to fetch discovery: cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://nextcloud.bitnet.dev/apps/richdocumentscode/proxy.php?req=/hosting/discovery"}}

My config.php

<?php
$CONFIG = array (
  'passwordsalt' => 'REDACTED',
  'secret' => 'REDACTED',
  'trusted_domains' => 
  array (
    0 => 'localhost',
    1 => 'REDCTED',
    2 => '10.203.1.106',
    3 => 'REDACTED',
  ),
  'maintenance_window_start' => 1,
  'datadirectory' => '/var/www/nextcloud-data',
  'dbtype' => 'mysql',
  'version' => '31.0.6.2',
  'overwrite.cli.url' => 'http://localhost',
  'dbname' => 'nextcloud',
  'dbhost' => '10.203.1.106',
  'dbport' => '3306',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => 'REDACTED',
  'installed' => true,
  'instanceid' => '3ef4525597ff2',
  'memcache.local' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => '/var/run/redis/redis.sock',
    'port' => 0,
    'timeout' => 0.0,
  ),
  'filelocking.enabled' => true,
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'log_type' => 'file',
  'logfile' => '/var/www/nextcloud-data/nextcloud.log',
  'loglevel' => 1,
  'maintenance' => false,
  'theme' => '',
  'forbidden_filename_characters' => 
  array (
    0 => '<',
    1 => '>',
    2 => ':',
    3 => '"',
    4 => '|',
    5 => '?',
    6 => '*',
    7 => '\\',
    8 => '/',
  ),
  'forbidden_filename_extensions' => 
  array (
    0 => ' ',
    1 => '.',
    2 => '.filepart',
    3 => '.part',
  ),
  'mail_smtpmode' => 'smtp',
  'mail_sendmailmode' => 'smtp',
  'mail_smtphost' => 'smtp.gmail.com',
  'mail_smtpport' => '465',
  'mail_smtpauth' => true,
  'mail_smtpname' => 'REDACTED',
  'mail_smtppassword' => 'REDACTED',
  'mail_from_address' => 'NextCloud',
  'mail_domain' => 'REDACTED',
  'app_install_overwrite' => 
  array (
    0 => 'maps',
  ),
  'forbidden_filename_basenames' => 
  array (
    0 => 'con',
    1 => 'prn',
    2 => 'aux',
    3 => 'nul',
    4 => 'com0',
    5 => 'com1',
    6 => 'com2',
    7 => 'com3',
    8 => 'com4',
    9 => 'com5',
    10 => 'com6',
    11 => 'com7',
    12 => 'com8',
    13 => 'com9',
    14 => 'com¹',
    15 => 'com²',
    16 => 'com³',
    17 => 'lpt0',
    18 => 'lpt1',
    19 => 'lpt2',
    20 => 'lpt3',
    21 => 'lpt4',
    22 => 'lpt5',
    23 => 'lpt6',
    24 => 'lpt7',
    25 => 'lpt8',
    26 => 'lpt9',
    27 => 'lpt¹',
    28 => 'lpt²',
    29 => 'lpt³',
  ),
  'default_phone_region' => 'us',
  'memories.db.triggers.fcu' => true,
  'memories.exiftool' => '/var/www/nextcloud/apps/memories/bin-ext/exiftool-amd64-glibc',
  'memories.vod.path' => '/var/www/nextcloud/apps/memories/bin-ext/go-vod-amd64',
  'memories.vod.ffmpeg' => '/usr/bin/ffmpeg',
  'memories.vod.ffprobe' => '/usr/bin/ffprobe',
  'enabledPreviewProviders' => 
  array (
    0 => 'OC\\Preview\\Image',
    1 => 'OC\\Preview\\HEIC',
    2 => 'OC\\Preview\\TIFF',
  ),

  'preview_max_x' => 1024,
  'preview_max_y' => 1024,
  'memories.gis_type' => 1,
  'memories.index.path' => '/Pictures/USA',
  'preview_max_memory' => 1024,
  'memories.vod.external' => true,

);

Apps

###My php.ini

[PHP]engine = On
short_open_tag = Offprecision = 14output_buffering = 4096zlib.output_compression = Offimplicit_flush = Offunserialize_callback_func =
serialize_precision = -1disable_functions =
disable_classes =
zend.enable_gc = Onzend.exception_ignore_args = Onzend.exception_string_param_max_len = 0
expose_php = Off
max_execution_time =30max_input_time = 60
max_input_vars =1000
memory_limit =512Merror_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Offlog_errors = On
ignore_repeated_errors = Offignore_repeated_source = Offreport_memleaks = Onvariables_order = "GPCS"
request_order = "GP"
register_argc_argv = Offauto_globals_jit = On
post_max_size =16Mauto_prepend_file =auto_append_file =
default_mimetype = "text/html"default_charset = "UTF-8"
doc_root =
user_dir =enable_dl = Offfile_uploads = On
upload_max_filesize =8M
max_file_uploads = 20allow_url_fopen = Onallow_url_include = Off
default_socket_timeout = 60
[CLI Server]cli_server.color = On[Date]
[filter]
[iconv][imap]
[intl][sqlite3][Pcre][Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=[Phar]
[mail function]
SMTP = localhostsmtp_port = 25mail.add_x_header = Offmail.mixed_lf_and_crlf = Off[ODBC]odbc.allow_persistent = Onodbc.check_persistent = Onodbc.max_persistent = -1odbc.max_links = -1
odbc.defaultlrl = 4096odbc.defaultbinmode = 1[MySQLi]mysqli.max_persistent = -1mysqli.allow_persistent = Onmysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =mysqli.default_host =mysqli.default_user =mysqli.default_pw =
[mysqlnd]
mysqlnd.collect_statistics = Onmysqlnd.collect_memory_statistics = Off
[OCI8][PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Offpgsql.max_persistent = -1pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0[bcmath]
bcmath.scale = 0[browscap][Session]
session.save_handler = filessession.use_strict_mode = 0session.use_cookies = 1session.use_only_cookies = 1session.name = PHPSESSIDsession.auto_start = 0session.cookie_lifetime = 0session.cookie_path = /session.cookie_domain =
session.cookie_httponly =session.cookie_samesite =session.serialize_handler = php
session.gc_probability = 0session.gc_divisor = 1000
session.gc_maxlifetime = 1440session.referer_check =
session.cache_limiter = nocachesession.cache_expire = 180session.use_trans_sid = 0
session.sid_length = 26session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]zend.assertions = -1[COM]
[mbstring]
[gd]
[exif][Tidy]
tidy.clean_output = Off[soap]
soap.wsdl_cache_enabled=1soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5[sysvshm][ldap]ldap.max_links = -1[dba]
[opcache]
opcache.interned_strings_buffer=8opcache.revalidate_freq=30[curl]
[openssl][ffi]
[pdlib]
extension="pdlib.so"

Apps

root@NextCloud /v/w/nextcloud# sudo -u www-data php occ app:list
An unhandled exception has been thrown:
Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused in /var/www/nextcloud/lib/private/DB/Connection.php:237
Stack trace:
#0 /var/www/nextcloud/3rdparty/doctrine/dbal/src/Connection.php(458): OC\DB\Connection->connect()
#1 /var/www/nextcloud/3rdparty/doctrine/dbal/src/Connection.php(416): Doctrine\DBAL\Connection->getDatabasePlatformVersion()
#2 /var/www/nextcloud/3rdparty/doctrine/dbal/src/Connection.php(323): Doctrine\DBAL\Connection->detectDatabasePlatform()
#3 /var/www/nextcloud/lib/private/DB/Connection.php(903): Doctrine\DBAL\Connection->getDatabasePlatform()
#4 /var/www/nextcloud/lib/private/DB/ConnectionAdapter.php(235): OC\DB\Connection->getDatabaseProvider()
#5 /var/www/nextcloud/lib/private/DB/QueryBuilder/QueryBuilder.php(96): OC\DB\ConnectionAdapter->getDatabaseProvider()
#6 /var/www/nextcloud/lib/private/AppConfig.php(1226): OC\DB\QueryBuilder\QueryBuilder->expr()
#7 /var/www/nextcloud/lib/private/AppConfig.php(243): OC\AppConfig->loadConfig()
#8 /var/www/nextcloud/lib/private/AppConfig.php(1366): OC\AppConfig->searchValues()
#9 /var/www/nextcloud/lib/private/App/AppManager.php(136): OC\AppConfig->getValues()
#10 /var/www/nextcloud/lib/private/App/AppManager.php(157): OC\App\AppManager->getInstalledAppsValues()
#11 /var/www/nextcloud/lib/private/legacy/OC_App.php(188): OC\App\AppManager->getInstalledApps()
#12 /var/www/nextcloud/lib/private/AppFramework/Bootstrap/Coordinator.php(48): OC_App::getEnabledApps()
#13 /var/www/nextcloud/lib/base.php(675): OC\AppFramework\Bootstrap\Coordinator->runInitialRegistration()
#14 /var/www/nextcloud/lib/base.php(1171): OC::init()
#15 /var/www/nextcloud/console.php(28): require_once('...')
#16 /var/www/nextcloud/occ(33): require_once('...')
#17 {main}⏎

Guess no apps list …

Unfortunatelly, my instance has so much data that I opted to backup weekly in snapshots only … and it’s too large and too old to be usefull so if I can’t get this DB listening again, I have to start from scratch including several 100h of face recognition.

I’m fairly experienced with linux servers but new to nextcloud.

Hello @BitsNBurnouts,

welcome to the Nextcloud community! :handshake:

sounds like connection parameters are wrong. as the problem was introduced by config changes I would recommend to review/compare config backups - likely you unintentionally broke the config - eg. because of a syntax error.. e.g. some lines in your php.ini look suspicious if line brakes are missing:

Thank you everyone. I figured it out after 8 hours of tweaking and tinkering.

First I thought it may be database related since I had also just dropped a row more a message stock in the local email table and now that table showed no days at all. That was normal.

I connected to Maria db on cli which validated the connection credentials, next I tried resetting some of the connection parameters I had changed local db IP from localhost to loopback and local IP. Them tried matching the php.ini from the clin since I could connect to MySQL there.

Eventually I remembered that since this is a pre built LXC it comes with adminer so I logged in to the database there to check. Witch didn’t get me anywhere but reminded me that it also comes with webmin.

Them I logged in to webmin to poke around and diagnose, at this point I suspected something more basic at the os level and started sanity checks.

That’s when I saw the fail2ban button on webmin. There is no other firewall enabled in the system so this called my attention and … What do you know. This sunofagun had jailed the nextcloud process because while messing with the parameters I must have had incorrect credentials a could of times.

I white listed the process in fail2ban and everything began working again.

SOLVED!

thank you all, this has been a stressful but interesting experience.

2 Likes

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.