Docker: Database connection fails

Created on 18 Nov 2018  路  10Comments  路  Source: nextcloud/docker

I'm following the README.md of this repo but am unable to finish the WebUI setup. As the readme file does not specify where to put certain variables, I'm writing them into both apps. Here is my config:

version: '2'

volumes:
  nextcloud:
  db:

services:
  db:
    image: mariadb
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=123
      - MYSQL_PASSWORD=supersecret
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=127.0.0.1:3306
      - NEXTCLOUD_ADMIN_USER=admin
      - NEXTCLOUD_ADMIN_PASSWORD=supersecret

  app:
    image: nextcloud:fpm
    links:
      - db
    volumes:
      - nextcloud:/var/www/html
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=supersecret
      - MYSQL_PASSWORD=supersecret
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=127.0.0.1:3306
      - NEXTCLOUD_ADMIN_USER=admin
      - NEXTCLOUD_ADMIN_PASSWORD=supersecret

  web:
    image: nginx
    ports:
      - 9000:80
    links:
      - app
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    volumes_from:
      - app
    restart: always

Upon opening the WebUI, the setup screen is loaded. It doesn't ask for any database values first (the option to show database specific parameters is not there at first). I can only create an admin account. There is the first bug as these values should have been read from the environment variables.
If I enter the admin account credentials, I get the following error:

Error while trying to create admin user: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused 

It seems like the docker images seem to ignore certain environment variables which makes the automatic configuration currently broken.

docs enhancement

All 10 comments

Hello,

instead of

    - MYSQL_HOST=127.0.0.1:3306

use

    - MYSQL_HOST=db

That fixes the issue. Thanks!
The readme file also doesn't mention that you have to create the environment variables MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_USER and MYSQL_HOST for both services, db and app. I'd suggest updating it.

Thats not entirely correct.

The mysql container just needs MYSQL_ROOT_PASSWORD, but you can provide the following:

      - MYSQL_ROOT_PASSWORD=
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

And the nextcloud container needs nothing. It will then show the web installer on first connect. You can however give these (or the equivalent for postgresql) to automatically trigger the install on first start:

      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db
      - NEXTCLOUD_ADMIN_USER=
      - NEXTCLOUD_ADMIN_PASSWORD=

You are right.
What I wanted to point out was that the readme doesn't mention which of these variables have to go to what service. The variables are not mandatory, but if you want to use them, the docs do not clearly state how to do that properly.

We'd welcome a PR to enhance our docs :wink:

Hello,

instead of

    - MYSQL_HOST=127.0.0.1:3306

use

    - MYSQL_HOST=db

if I try this I'm getting this error:

SQLSTATE[HY000] [2006] MySQL server has gone away

my docker-compose.yml looks like:

version: '2'

volumes:
  nextcloud:
  db:

services:
  db:
    image: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_PASSWORD=9876
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db

  app:
    image: nextcloud
    ports:
      - 8080:80
    links:
      - db
    volumes:
      - nextcloud:/var/www/html
    environment:
      - MYSQL_HOST=db
    restart: always`

I'm getting the same issue with a mostly identical docker-compose file. And that's driving me crazy. Does anyone have an idea to solve it ?

if I try this I'm getting this error:

SQLSTATE[HY000] [2006] MySQL server has gone away

Use database field db when you are in first time set-up page. Ignore localhost, port etc. message in the page, simply enter db, which is your environment variable. Since you already selected mysql as database, it will use 3306 port.

@jabossu @ChrisKoh83 it seems like it may be an issue with the mariadb image. When I set the image to "mariadb:10.1", the issue goes away.

Was this page helpful?
0 / 5 - 0 ratings