0

The full error is Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory in /app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php on line 113, but that's too long for the title.

I'm trying to set up a Symfony project locally, but I'm struggling to get the database connection to work. My parameters.yml looks as follows

parameters:
    database_host: 127.0.0.1
    database_port: 3306
    database_name: database_name
    database_user: username
    database_password: password

I've been googling this issue a lot and most people seem to solve the issue by changing database_host from localhost to 127.0.0.1, but this doesn't work for me. The app itself runs via Docker, but I've set up the database connection once via Brew and once with a MySQL server for Mac. In both cases I can connect via the command line and with SequelPro/TablePlus, but whenever I try to access the website through the browser I get the "No such file or directory" error.

I've also tried multiple ways of setting up a Docker MySQL container, but can't get it to work. My docker-compose.yml looks like this;

nginx:
  build: nginx
  ports:
    - "8080:80"
  links:
    - php
  volumes:
    - ../:/app

php:
  build: php-fpm
  volumes:
    - ../:/app
  working_dir: /app
  extra_hosts:
    - "site.dev: 172.17.0.1"

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: 'database_name'
      MYSQL_USER: 'username'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password_root'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - my-db:/var/lib/mysql

But whenever I run docker-compose up -d I get the error Unsupported config option for services: 'db'.

Another attempt was adding

mysql:
  image: mysql:latest
  volumes:
    - mysql_data:/var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD='password'
    - MYSQL_DATABASE='database_name'
    - MYSQL_USER='username'
    - MYSQL_PASSWORD='password'

To the docker-compose file, and while it does build the mysql image, I can't seem to connect to it with SequelPro/TablePlus. I ran docker-inspect on the container to get the IP (172.17.0.3), but can't seem to get access to it. I can exec into it, login using mysql -u root and create the required user and database, but then I'm still struggling to actually connect to it.

Running docker ps does show the sql container running btw;

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
b6de6030791d        docker_nginx        "nginx -g 'daemon of…"   19 minutes ago      Up 14 minutes       0.0.0.0:8080->80/tcp   docker_nginx_1
f26b832bb005        docker_php          "docker-php-entrypoi…"   19 minutes ago      Up 14 minutes       9000/tcp               docker_php_1
6c2a9e657435        mysql:latest        "docker-entrypoint.s…"   19 minutes ago      Up 14 minutes       3306/tcp, 33060/tcp    docker_mysql_1

I also thought it might be an issue with changes to the parameters.yml file not properly syncing with the container as I'm using Mac (at my old workplace we had to use docker-sync to make sync changes between our dev environment and the actual container), but when inspecting the container itself using exec I can see the changes in the parameters.yml file.

Could the issue be it trying to connect to a mysql server running outside the Docker container? I'm still very new to Docker so I wouldn't be surprised if that's the mistake. Any tips are appreciated 'cause I'm at a dead end.

Alex
  • 778
  • 3
  • 12
  • 27

2 Answers2

2

Your docker-compose file looks wrong to me, try below docker-compose file. I removed the links, network is much easier.

version: '3'
services:
    nginx:
        build: nginx
        ports:
            - "8080:80"
        networks:
            - backend
        volumes:
            - ../:/app

    php:
        build: php-fpm
        volumes:
            - ../:/app
        working_dir: /app
        networks:
            - backend
        extra_hosts:
            - "site.dev: 172.17.0.1"


    db:
        image: mysql:5.7
        restart: always
        environment:
            MYSQL_DATABASE: 'database_name'
            MYSQL_USER: 'username'
            MYSQL_PASSWORD: 'password'
            MYSQL_ROOT_PASSWORD: 'password_root'
        networks:
            - backend
        ports:
            - '3306:3306'
        volumes:
            - ./my-db:/var/lib/mysql

networks:
    backend:
        driver: bridge

then use database_host: db in php file. I would diagnose

  • Check docker logs in the mysql container => no errors
  • Login to the mysql container and login to mysql => no errors
  • Login to mysql from the host (mysql -u username -p since you are mapping to 3306 port of the host)
  • Make sure mysql.cnf doesn't block connect from outside(check bind-address in the mysql configuration if it 127.0.0.1 the its only allow to connect form locally, i would for now make it 0.0.0.0 or commented that line if exists)

    • mysqld --verbose --help => you will see all options
    • mysqld --verbose --help | grep bind-address=> check the bind-address
  • Make sure the user i tried to login has enough privileges to connect(SELECT user,host FROM mysql.user;) check your user can connect from docker network => 172.* or anywhere=> %

Ntwobike
  • 2,406
  • 1
  • 21
  • 27
  • Still doesn't seem to work. It creates the mysql container properly, but I still can't connect to it with a DB manager and I still get the same error as before. `docker inspect` tells me the IP address is 172.19.0.4 and I added `expose: - "3306"` to docker-compose.yml, but like I said, still can't connect in any way. – Alex Jun 18 '19 at 12:50
  • were u able to u login to the container and connect with provided credentials to the mysql? – Ntwobike Jun 18 '19 at 12:58
  • Yes, if I exec into the container I can log in using the mysql command, the issue lies in connecting to it from outside the container. – Alex Jun 18 '19 at 13:01
  • @Alex check the answer, i updated with the steps i would diagnose the issue. – Ntwobike Jun 18 '19 at 13:53
  • I managed to solve it, with some more debugging I got more specific error messages, first Connection refused, then one about the server requesting an unknown authentication method after which I knew I was finally connected to the database. I managed to solve that error with [this](https://stackoverflow.com/a/53881212/2466375) answer, so I'm now in the process of importing a 1.6gb database dump. – Alex Jun 18 '19 at 14:11
0

I think your issue is with your parameters.yml:

parameters:
    database_host: 127.0.0.1

When you run compose, MySQL and PHP will run in their own containers which will have their own IPs: 127.0.0.1 or localhost from the php won't be able to connect to the db container. It's like you deployed PHP on a virtual machine A and MySQL to another virtual machine B, but you try to access MySQL from machine A by using localhost where you should specify machine B IP or hostname.

With Docker Compose the internal DNS will resolve the service name to it's container, so you can use something like:

parameters:
    # name of the service in compose should be resolved
    database_host: db 

The error SQLSTATE[HY000] [2002] No such file or directory may be caused when the client tries to read MySQL socket usually present at /var/lib/mysql/mysql.sock which is probably not present in your PHP container.

Pierre B.
  • 11,612
  • 1
  • 37
  • 58