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

Nextcloud version (eg, 27.1.6): replace me
Operating system and version (eg, Ubuntu 20.04): docker
PHP version (eg, 7.4): 8.2.16

The issue you are facing:
I updated my docker images and restarted my containers but now the app won’t connect to the database as it normally does. I’m not sure why and the logs don’t really tell me what changed that I can tell.

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

Steps to replicate it:

  1. Updated my docker images and restarted

The output of your Nextcloud log in Admin > Logging:
I’m not sure where to get the logs besides what docker gives me. occ gives me some errors:

$ sudo docker compose exec --user www-data app php occ help                                                                                                       
An unhandled exception has been thrown:                                                                                                                                                       
Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: Name or service n
ot known 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(1029): Doctrine\DBAL\Connection->getWrappedConnection()                                                                            
#2 /var/www/html/lib/private/DB/Connection.php(262): Doctrine\DBAL\Connection->executeQuery('SELECT * FROM `...', Array, Array, NULL)                                                         
#3 /var/www/html/3rdparty/doctrine/dbal/src/Query/QueryBuilder.php(345): OC\DB\Connection->executeQuery('SELECT * FROM `...', Array, Array)                                                   
#4 /var/www/html/lib/private/DB/QueryBuilder/QueryBuilder.php(280): Doctrine\DBAL\Query\QueryBuilder->execute()                                                                               
#5 /var/www/html/lib/private/AppConfig.php(421): OC\DB\QueryBuilder\QueryBuilder->execute()                                                                                                   
#6 /var/www/html/lib/private/AppConfig.php(187): OC\AppConfig->loadConfigValues()
#7 /var/www/html/lib/private/AppConfig.php(377): OC\AppConfig->getApps()
#8 /var/www/html/lib/private/legacy/OC_App.php(803): OC\AppConfig->getVa

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

<?php                                                                                                                                                                                         
$CONFIG = array (                                                                                                                                                                             
  'datadirectory' => '/var/www/html/data',                                                                                                                                                    
  'memcache.local' => '\\OC\\Memcache\\APCu',                                                                                                                                                 
  'instanceid' => 'xxxx',                                                                                                                                                             
  'passwordsalt' => 'xxxx',                                                                                                                                         
  'secret' => 'xxxxx',                                                                                                                             
  'overwriteprotocol' => 'https',                                                                                                                                                             
  'trusted_domains' =>                                                                                                                                                                        
  array (                                                                                                                                                                                     
    0 => 'xxxx',                                                                                                                                                         
  ),                                                                                                                                                                                          
  'trusted_proxies' =>                                                                                                                                                                        
  array (                                                                                                                                                                                     
    0 => 'xxxx',                                                                                                                                                         
  ),                                                                                                                                                                                          
  'apps_paths' =>                                                                                                                                                                             
  array (                                                                                                                                                                                     
    0 =>                                                                                                                                                                                      
    array (                                                                                                                                                                                   
      'path' => '/var/www/html/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 => 
    array (
      'path' => '/var/www/html/custom_apps',
      'url' => '/custom_apps',
      'writable' => true,
    ),
  ),
  'overwrite.cli.url' => 'https://xxxxx',
  'check_for_working_wellknown_setup' => false, 
  'dbtype' => 'mysql',
  'version' => '27.1.6.2',
  'dbname' => 'nextcloud',
  'dbhost' => 'db',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => 'xxxxx',
  'installed' => true,
  'mail_from_address' => 'postmaster',
  'logtimezone' => 'Etc/UTC',
  'logdateformat' => 'Y-m-d H:i:s',
  'mail_smtpmode' => 'smtp',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtpsecure' => 'tls',
  'mail_domain' => 'xxxx', 
  'mail_smtpauth' => 1,
  'mail_smtphost' => 'smtp.mailgun.org',
  'mail_smtpname' => 'postmaster@xxxx',
  'mail_smtppassword' => 'xxxxxx',
  'default_phone_region' => 'US',
  'loglevel' => 0,
  'maintenance' => false,
  'theme' => '',
  'updater.release.channel' => 'stable',
  'app_install_overwrite' => 
  array (
    0 => 'spreed',
    1 => 'twofactor_totp',
  ),
);

Output errors in nextcloud.log in /var/www/ or as admin user in top right menu, filtering for errors. Use a pastebin service if necessary.

I’m not sure where the logs are on docker setups.

php_network_getaddresses: getaddrinfo for db failed: Name or service not known

Seems to be that the database “db” is not reachable. Have you changed something?

I have not. The database is setup in a docker compose file.

$ cat docker-compose.yml                                                                                                                                          
version: '3.7'                                                                                                                                                                                
x-volumes:                                                                                                                                                                                    
  &nextcloud-volumes                                                                                                                                                                          
      - /mnt/data/ansible/nextcloud/custom_apps:/var/www/html/custom_apps                                                                                                                     
      - /mnt/data/ansible/nextcloud/config:/var/www/html/config                                                                                                                               
      - /mnt/data/ansible/nextcloud/data:/var/www/html/data                                                                                                                                   
      - /mnt/data/ansible/nextcloud/themes:/var/www/html/themes                                                                                                                               
      - /mnt/data/ansible/nextcloud/root:/var/www/html                                                                                                                                        
x-nextcloud-image: &nextcloud-image                                                                                                                                                           
  "nextcloud:stable"                                                                                                                                                                          
                                                                                                                                                                                              
services:                                                                                                                                                                                     
  db:                                                                                                                                                                                         
    image: mariadb:10.6                                                                                                                                                                       
    environment:                                                                                                                                                                              
      MYSQL_ROOT_PASSWORD: "xxxx"                                                                                                                                                     
      MYSQL_DATABASE: nextcloud                                                                                                                                                               
      MYSQL_USER: nextcloud                                                                                                                                                                   
      MYSQL_PASSWORD: "xxxx"                                                                                                                                                          
    volumes:                                                                                                                                                                                  
      - /mnt/data/ansible/nextcloud/mariadb_data:/var/lib/mysql                                                                                                                               
    networks:                                                                                                                                                                                 
      - default
    restart: always
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --skip-innodb-read-only-compressed
  app:
    image: *nextcloud-image
    links:
      - db
    environment:
      VIRTUAL_HOST: xxxx
           VIRTUAL_PORT: 80
      LETSENCRYPT_HOST: nxx
      LETSENCRYPT_EMAIL: xxxx
      NEXTCLOUD_ADMIN_USER: axxxx
      NEXTCLOUD_ADMIN_PASSWORD: "xxxx"
      MYSQL_DATABASE: nextcloud
      MYSQL_USER: nextcloud
      MYSQL_PASSWORD: "xxxx"
      MYSQL_HOST: db
    volumes:
      *nextcloud-volumes
    networks:
      - nginx-proxy
      - default
    restart: always
  cron:
    image: *nextcloud-image
    restart: always
    volumes:
      *nextcloud-volumes
    depends_on:
      - app
    entrypoint: |
      bash -c 'bash -s <<EOF
      trap "break;exit" SIGHUP SIGINT SIGTERM
      while /bin/true; do
        su -s "/bin/bash" -c "/usr/local/bin/php /var/www/html/cron.php" www-data
        echo $$(date) - Running cron finished
        sleep 900
      done
      EOF'
networks:
  nginx-proxy:
    external: true
  default:
volumes:
  nextcloud-root:

Maybe something changed in docker but I’m not sure what it would be.

Is the database up and running? Can you test it e.g. with netcat or netstat -tulpen. Check listen port for database.

The docker logs indicate it is running. The containers have limited commands available to them so I don’t have netcat.

I usually do sudo docker compose exec --user www-data app occ but occ is broken right now because it cannot connect to the db.

$ sudo docker compose ps
NAME               IMAGE              COMMAND                  SERVICE   CREATED        STATUS          PORTS
nextcloud-app-1    nextcloud:stable   "/entrypoint.sh apac…"   app       12 hours ago   Up 40 minutes   80/tcp
nextcloud-cron-1   nextcloud:stable   "bash -c 'bash -s <<…"   cron      12 hours ago   Up 9 seconds    80/tcp
nextcloud-db-1     mariadb:10.6       "docker-entrypoint.s…"   db        12 hours ago   Up 41 minutes   3306/tcp

I suspect this is a docker issue due to docker not creating virtual networks as it should. Some of the docker stack did recently change in an update on Arch OS.

I rebooted today after updating the kernel and a few other packages and Nextcloud came back alive. I think it might be a race condition issue causing Docker to not work sometimes. For now I do not have a issue with Nextcloud.

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