SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known

I’ve reviewed multiple potential solution to this and none have worked. I think I’m too close to the problem so I can’t seem to debug it, but I feel it’s because Nextcloud is either ignoring the port number or is trying to use the socket. I’d appreciate any thoughts on the issue. There is no firewall in place at this time.

Issue
When starting Nextcloud and creating an admin account, the following error is displayed:

Error

Error while trying to create admin user: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known

Create an admin account

I have successfully run an instance of Nextcloud before, but need to adjust the port number used by MySQL to avoid a collision with an instance of MariaDB on the same server. I am trying to run the containers in Host network mode.

Configuration
Here is my docker-compose.yml file:

version: '3'
services:
  nextcloud:
    image: nextcloud:latest
    container_name: nextcloud
    hostname: id-services
    network_mode: host
    # ports:
    #   - 80:80
    depends_on:
      - mysql
    environment:
      - TZ=${TZ}
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_HOST=mysql
      - MYSQL_TCP_PORT=3307
      - MYSQLX_TCP_PORT=33070
      - TRUSTED_PROXIES=${TRUSTED_PROXIES}
      - NEXTCLOUD_TRUSTED_DOMAINS=${NEXTCLOUD_TRUSTED_DOMAINS}
      - NEXTCLOUD_HOSTNAME=${NEXTCLOUD_HOSTNAME}
      - OVERWRITECLIURL=${OVERWRITECLIURL}
      - OVERWRITEHOST=${OVERWRITEHOST}
      - OVERWRITEPROTOCOL=https
    volumes:
      - ${DIRECTORY_HTML}:/var/www/html
      - ${DIRECTORY_BACKUPS}:/media/backups
      - ${DIRECTORY_DOWNLOADS}:/media/downloads
      - ${DIRECTORY_PICTURES}:/media/pictures
      - ${DIRECTORY_SHARED}:/media/shared
      - ${DIRECTORY_SECURITY}:/media/security
      - /etc/localtime:/etc/localtime:ro
    restart: unless-stopped
  mysql:
    image: mysql:latest
    container_name: mysql
    hostname: id-services
    network_mode: host
    # ports:
    #   - 3307:3306
    #   - 33070:33060
    cap_add:
      - SYS_NICE
    environment:
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_TCP_PORT=${MYSQL_TCP_PORT}
      - MYSQLX_TCP_PORT-${MYSQLX_TCP_PORT}
    volumes:
      - ${DIRECTORY_MYSQL}:/var/lib/mysql
    restart: unless-stopped

I have a .env file containing the various directories, passwords, and configuration values. I bring the service up using docker compose pull;docker compose up --detach. The contains are running with no issues.

Troubleshooting
Here are the logs for each container.

Nextcloud:

$ docker logs nextcloud
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Mon Feb 20 08:03:49.369373 2023] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.54 (Debian) PHP/8.1.16 configured -- resuming normal operations
[Mon Feb 20 08:03:49.369625 2023] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
23.17.32.59 - - [20/Feb/2023:08:04:18 -0700] "GET /data/htaccesstest.txt HTTP/1.1" 403 639 "-" "Nextcloud Server Crawler"
[Mon Feb 20 08:04:18.898765 2023] [access_compat:error] [pid 32] [client 23.17.32.59:0] AH01797: client denied by server configuration: /var/www/html/data/htaccesstest.txt
[Mon Feb 20 08:04:19.016971 2023] [access_compat:error] [pid 33] [client 23.17.32.59:0] AH01797: client denied by server configuration: /var/www/html/data/htaccesstest.txt

MySQL:

$ docker logs mysql
2023-02-20 15:03:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
2023-02-20 15:03:49+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-02-20 15:03:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2023-02-20T15:03:49.828320Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-02-20T15:03:49.830590Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 1
2023-02-20T15:03:49.842649Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-02-20T15:03:50.090330Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-02-20T15:03:50.506040Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-02-20T15:03:50.506666Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-02-20T15:03:50.513076Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-02-20T15:03:50.543854Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33070, socket: /var/run/mysqld/mysqlx.sock
2023-02-20T15:03:50.543930Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32'  socket: '/var/run/mysqld/mysqld.sock'  port: 3307  MySQL Community Server - GPL.

Accessing the MySQL instance using localhost:

$ docker exec -it mysql bash
bash-4.4#  mysql --host=localhost --port=3307 --user=nextcloud --password=[password] nextcloud
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status;
--------------
mysql  Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          11
Current database:       nextcloud
Current user:           nextcloud@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.32 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/run/mysqld/mysqld.sock
Binary data as:         Hexadecimal
Uptime:                 21 min 9 sec

Threads: 2  Questions: 14  Slow queries: 0  Opens: 146  Flush tables: 3  Open tables: 65  Queries per second avg: 0.011
--------------

Accessing the MySQL instance using ip address:

bash-4.4#  mysql --host=192.168.1.95 --port=3307 --user=nextcloud --password=[password] nextcloud
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
--------------
mysql  Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          12
Current database:       nextcloud
Current user:           nextcloud@192.168.1.95
SSL:                    Cipher in use is TLS_AES_256_GCM_SHA384
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.32 MySQL Community Server - GPL
Protocol version:       10
Connection:             192.168.1.95 via TCP/IP
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    latin1
Conn.  characterset:    latin1
TCP port:               3307
Binary data as:         Hexadecimal
Uptime:                 22 min 58 sec

Threads: 2  Questions: 24  Slow queries: 0  Opens: 153  Flush tables: 3  Open tables: 72  Queries per second avg: 0.017
--------------

Data Used
When the error message appears, I’ve tried the following:

Data folder
/var/www/html/data
Configure the database
MySQL/MariaDB
Database user
nextcloud
Database password
[password]
Database name
nextcloud
Database host
See below

For the DaAabase host I’ve tried combinations of:

localhost:3307
localhost 33070
mysql:3307
mysql:33070
192.168.1.95:3307
192.168.1.95:33070
id-services:3307
id-services:33070

What am I missing? Should I be exposing the MySQL socket via a volume mount in the docker compose file?

This is so frustrating because it looks correct, so I must be missing something obvious because I’ve been staring at it for too long.

1 Like

In general I would recommedn you not to use host networking mode, primarily for security reasons. Using default docker mode with explicit port forwarding reduce the risk to expose some unexpected port (or even completely hidden inside of Docker host which is best option for backend services like database). Likely host mode is the reason for your problem - likely you don’t have "mysql"DNS record in your network. Using default bridge mode Docker magically creted requred DNS records and you don’t need to care about it.

In general I would recommend you start with an official example, get basics work and add complexity like additional mounts once everything else works as expected.

I appreciate the concerns regarding the use of host network mode.

I have had Nextcloud running using the default methods, but I am looking to use a separately hosted database. To be clear, I mean a MariaDB or MySQL instance hosted as a docker container but on another server.

It should be simple enough to reference the database host by ip address and port, e.g. 192.168.1.95:3307, but this does not seem to work. Why is that?

I know using a Bridge network, default or custom, will allow the containers to access each other, but I am investigating access separately. I have other containers that allow this. What do I need to do to have Nextcloud find a database by ip address and port number?

I’m pretty sure Nextcloud can access the database by IP and port without special config. I would start “from the end”: once you have your DB up and running, verify you expose it correct using some DB tool from your client/server - once you can access the DB with specific ip:port combination continue with Nextcloud. If this still doesn’t work review routing and firewall config…

I am working on this - trying to host either a MySQL or MariaDB separate instance, and ensuring remote connections are in place.

Will report back with findings.

@instantdreams

I seem to be experiencing the same issue. Did you find a solution to the problem?

I have the database and the application in the same network, but the application won’t resolve the name of the database host as the error message states.

The host has IP 192.168.100.4. My database container is on a bridge network with ip 192.168.5.4 , same bridge network as my nextcloud application. They are linked properly. I have tried accessing it through 192.168.5.4, 192.168.5.4:3306, 192.168.100.4:3306 and the container name, but it none of these options has worked for me.

Thank you. =))