After moving from mariadb to mysql: SQLSTATE[HY000] [2002] Connection refused

The Basics

  • Nextcloud Server version (e.g., 29.x.x):
    • 31.0.2.1
  • Operating system and version (e.g., Ubuntu 24.04):
    • Debian 12
  • Web server and version (e.g, Apache 2.4.25):
    • NGINX
  • 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?
    • After moving to a new host
  • Installation method (e.g. AlO, NCP, Bare Metal/Archive, etc.)
    • Bare Metal
  • Are you using CloudfIare, mod_security, or similar? (Yes / No)
    • No

Summary of the issue you are facing:

We have decided to move our Nextcloud to a new Server and move the database from a single MariaDB server into our MySQL 8 Cluster.

After the import of the SQL was finished (without errors or warnings) i completed the change in the config.php to the correct database settings.

Unfortunately we are receiving the following error:

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

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

  1. export mariadb nextcloud database
  2. import in a mysql 8 cluster
  3. fix the mysql credentials and host in the config.php

Log entries

Nextcloud

Please provide the log entries from your Nextcloud log that are generated during the time of problem (via the Copy raw option from Administration settings->Logging screen or from your nextcloud.log located in your data directory). Feel free to use a pastebin/gist service if necessary.

{
  "reqId":"r62FMHttp4QpTvjyvu5M",
  "level":3,
  "time":"2025-04-04T15:00:23+02:00",
  "remoteAddr":"10.1.200.1",
  "user":false,
  "app":"index",
  "method":"GET",
  "url":"/index.php/apps/files/preview-service-worker.js",
  "message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused",
  "userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
  "version":"31.0.2.1",
  "exception":{
    "Exception":"Doctrine\\DBAL\\Exception",
    "Message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused",
    "Code":2002,
    "Trace":[
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/3rdparty/doctrine/dbal/src/Connection.php",
        "line":458,
        "function":"connect",
        "class":"OC\\DB\\Connection",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/3rdparty/doctrine/dbal/src/Connection.php",
        "line":416,
        "function":"getDatabasePlatformVersion",
        "class":"Doctrine\\DBAL\\Connection",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/3rdparty/doctrine/dbal/src/Connection.php",
        "line":323,
        "function":"detectDatabasePlatform",
        "class":"Doctrine\\DBAL\\Connection",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/DB/Connection.php",
        "line":903,
        "function":"getDatabasePlatform",
        "class":"Doctrine\\DBAL\\Connection",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/DB/ConnectionAdapter.php",
        "line":235,
        "function":"getDatabaseProvider",
        "class":"OC\\DB\\Connection",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/DB/QueryBuilder/QueryBuilder.php",
        "line":96,
        "function":"getDatabaseProvider",
        "class":"OC\\DB\\ConnectionAdapter",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/AppConfig.php",
        "line":1226,
        "function":"expr",
        "class":"OC\\DB\\QueryBuilder\\QueryBuilder",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/AppConfig.php",
        "line":243,
        "function":"loadConfig",
        "class":"OC\\AppConfig",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/AppConfig.php",
        "line":1366,
        "function":"searchValues",
        "class":"OC\\AppConfig",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/App/AppManager.php",
        "line":136,
        "function":"getValues",
        "class":"OC\\AppConfig",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/App/AppManager.php",
        "line":157,
        "function":"getInstalledAppsValues",
        "class":"OC\\App\\AppManager",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/legacy/OC_App.php",
        "line":188,
        "function":"getInstalledApps",
        "class":"OC\\App\\AppManager",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/private/AppFramework/Bootstrap/Coordinator.php",
        "line":48,
        "function":"getEnabledApps",
        "class":"OC_App",
        "type":"::"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/base.php",
        "line":675,
        "function":"runInitialRegistration",
        "class":"OC\\AppFramework\\Bootstrap\\Coordinator",
        "type":"->"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/lib/base.php",
        "line":1156,
        "function":"init",
        "class":"OC",
        "type":"::"
      },
      {
        "file":"/var/www/share/next.[REDACTED]/htdocs/index.php",
        "line":22,
        "args":["/var/www/share/next.[REDACTED]/htdocs/lib/base.php"],
        "function":"require_once"
      }
    ],
    "File":"/var/www/share/next.[REDACTED]/htdocs/lib/private/DB/Connection.php",
    "Line":237,
    "message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused",
    "exception":{},
    "CustomMessage":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused"
  }
}

Web server / Reverse Proxy

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

2025/04/04 09:40:03 [error] 5357#5357: *151942 FastCGI sent in stderr: "PHP message: {"reqId":"vwJusrhNtqRvc6VKfR4K","level":3,"time":"2025-04-04T09:40:02+02:00","remoteAddr":"10.1.200.1","user":false,"app":"index","method":"GET","url":"/index.php/apps/files/preview-service-worker.js","message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user 'nextcloud'@'10.1.200.6' (using password: YES)","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36","version":"31.0.2.1","exception":{"Exception":"Doctrine\\DBAL\\Exception","Message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user 'nextcloud'@'10.1.200.6' (using password: YES)","Code":1045,"Trace":[{"file":"/var/www/share/next.[REDACTED]/htdocs/3rdparty/doctrine/dbal/src/Connection.php","line":458,"function":"connect","class":"OC\\DB\\Connection","type":"->"},{"file":"/var/www/share/next.[REDACTED]/htdocs/3rdparty/d...; PHP message: {"reqId":"vwJusrhNtqRvc6VKfR4K","level":3,"time":"2025-04-04T09:40:03+02:00","remoteAddr":"10.1.200.1","user":false,"app":"core","method":"GET","url":"/index.php/apps/files/preview-service-worker.js","message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user 'nextcloud'@'10.1.200.6' (using password: YES)","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36","version":"31.0.2.1","exception":{"Exception":"Doctrine\\DBAL\\Exception","Message":"Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user 'nextcloud'@'10.1.200.6' (using password: YES)","Code":1045,"Trace":[{"file":"/var/www/share/next.[REDACTED]/htdocs/3rdparty/doctrine/dbal/src/Connection.php","line":458,"function":"connect","class":"OC\\DB\\Connection","type":"->"},{"file":"/

Configuration

Nextcloud

The output of occ config:list system or similar is best, but, if not possible, the contents of your config.php file from /path/to/nextcloud is fine (make sure to remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => '[REDACTED]',
  'passwordsalt' => '[REDACTED]+',
  'secret' => '[REDACTED]',
  'trusted_domains' => 
  array (
    0 => '[REDACTED]',
    1 => '[REDACTED]',
    2 => '[REDACTED]',
  ),
  'datadirectory' => '/var/www/share/next.[REDACTED]/nx-data',
  'dbtype' => 'mysql',
  'version' => '31.0.2.1',
  'overwrite.cli.url' => 'https://next.[REDACTED]',
  'dbname' => 'nextcloud',
  'dbhost' => '10.1.201.4',
  'dbport' => '6033',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => '[REDACTED]',
  'installed' => true,
  'ldapIgnoreNamingRules' => false,
  'ldapProviderFactory' => 'OCA\\User_LDAP\\LDAPProviderFactory',
  'app_install_overwrite' => 
  array (
    0 => 'files_fulltextsearch_tesseract',
    1 => 'previewgenerator',
    2 => 'documentserver_community',
    3 => 'files_texteditor',
    4 => 'twofactor_admin',
    5 => 'integration_whiteboard',
    6 => 'fulltextsearch',
    7 => 'fulltextsearch_elasticsearch',
    8 => 'files_fulltextsearch',
    9 => 'impersonate',
    10 => 'duplicatefinder',
    11 => 'files_markdown',
    12 => 'files_readmemd',
  ),
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => 'localhost',
    'port' => 6379,
    'dbindex' => 0,
    'timeout' => 0.0,
  ),
  'activity_expire_days' => 14,
  'auth.bruteforce.protection.enabled' => true,
  'blacklisted_files' => 
  array (
    0 => '.htaccess',
    1 => 'Thumbs.db',
    2 => 'thumbs.db',
  ),
  'cron_log' => true,
  'enable_previews' => true,
  'enabledPreviewProviders' => 
  array (
    0 => 'OC\\Preview\\PNG',
    1 => 'OC\\Preview\\JPEG',
    2 => 'OC\\Preview\\GIF',
    3 => 'OC\\Preview\\BMP',
    4 => 'OC\\Preview\\XBitmap',
    5 => 'OC\\Preview\\Movie',
    6 => 'OC\\Preview\\PDF',
    7 => 'OC\\Preview\\MP3',
    8 => 'OC\\Preview\\TXT',
    9 => 'OC\\Preview\\MarkDown',
    10 => 'OC\\Preview\\Illustrator',
    11 => 'OC\\Preview\\Photoshop',
    12 => 'OC\\Preview\\TIFF',
    13 => 'OC\\Preview\\SVG',
    14 => 'OC\\Preview\\Movie',
    15 => 'OC\\Preview\\MSOffice2007',
    16 => 'OC\\Preview\\MSOffice',
  ),
  'filesystem_check_changes' => 0,
  'filelocking.enabled' => 'true',
  'htaccess.RewriteBase' => '/',
  'integrity.check.disabled' => false,
  'knowledgebaseenabled' => false,
  'logfile' => '/var/www/share/next.[REDACTED]/nextcloud.log',
  'loglevel' => 0,
  'logtimezone' => 'Europe/Berlin',
  'log_rotate_size' => 104857600,
  'maintenance' => false,
  'maintenance_window_start' => 1,
  'overwriteprotocol' => 'https',
  'preview_max_x' => 1024,
  'preview_max_y' => 768,
  'preview_max_scale_factor' => 1,
  'quota_include_external_storage' => false,
  'share_folder' => '/Shares',
  'skeletondirectory' => '',
  'theme' => '',
  'trashbin_retention_obligation' => 'auto, 7',
  'updater.release.channel' => 'stable',
  'twofactor_enforced' => 'true',
  'twofactor_enforced_groups' => 
  array (
  ),
  'twofactor_enforced_excluded_groups' => 
  array (
    0 => 'servicegruppe',
  ),
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'tls',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'notification',
  'mail_domain' => '[REDACTED]',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtpauth' => 1,
  'mail_smtphost' => 'mail.[REDACTED]',
  'mail_smtpport' => '587',
  'mail_smtpname' => '[REDACTED]',
  'mail_smtppassword' => '[REDACTED]',
  'default_phone_region' => 'DE',
);

Apps

The output of occ app:list (if possible).

cant run

Extended informations

  • using a php test script to connect to the exact same host with user to access the database with the same password works
  • using the mysql cli to connect to the cluster as the nextcloud user with the password copied from config to the nextcloud database works too
  • the mysql cluster is loadbalanced with a proxysql server

It’s basically what it says.

What happens if you try to connect using the exact same info via the command-line mysql client the server running Nextcloud?

As already mentioned a php test script and the mysql CLI was able to connect as that nextcloud user to the database. Surely i tested that from the nextcloud host, everything else would be useless.

Seems like the dbPort Key is legacy. Adding the port with :port to the host value worked. Solved.

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