shrikant.sharma
shrikant.sharma

Reputation: 63

docker-compose drupal mysql SQLSTATE[HY000] [2002] No such file or directory

I have tried all the solutions available on all forms but no success even tried using accepted answers docker-compose file but still getting the same error.

enter image description here

I am trying this on a freshly installed ubuntu 18 on VirtualBox. I have tried with all possible combinations of localhost, 127.0.0.1, changing user, etc.

Docker version 19.03.6, build 369ce74a3c
docker-compose version 1.25.4, build 8d51620a

docker-compose.yaml

version: '3.1'
services:
  drupal:
    image: drupal:8
    container_name: drupal_container
    environment:
     DRUPAL_PROFILE: standard
     DRUPAL_SITE_NAME: Drupal
     DRUPAL_USER: user
     DRUPAL_PASS: pass
     DRUPAL_DBURL: mysql://user:pass@database:3306/db
    #or mysql://user:pass@mysql_container:3306/db tried with both
    links:  
     - "mysql:mysql"
    depends_on:
     - mysql
    ports:
     - 80:80
    restart: always
    volumes:
     - /var/www/html/modules
     - /var/www/html/profiles
     - /var/www/html/themes
     - /var/www/html/sites
    restart: always
  mysql:
    image: mysql:5.7
    container_name: mysql_container
    restart: always
    environment:
     MYSQL_ROOT_PASSWORD: root
     MYSQL_DATABASE: db
     MYSQL_USER: user
     MYSQL_PASSWORD: pass
    ports:
     - 3306:3306
    expose:
     - 3306

I am able to connect MySQL via command line and the docker exec command

sudo mysql --protocol=TCP -h127.0.0.1 -p3306 -uroot -proot
sudo mysql --protocol=TCP -h127.0.0.1 -p3306 -uuser -ppass
sudo mysql --protocol=TCP -hlocalhost -p3306 -uroot -proot
sudo mysql --protocol=TCP -hlocalhost -p3306 -uuser -ppass
docker-compose exec mysql mysql -uroot -proot -e "Show databases;"
docker-compose exec mysql mysql -uuser -ppass -e "Show databases;"

How can I fix this?

Upvotes: 0

Views: 1201

Answers (2)

nareshkumarganesan
nareshkumarganesan

Reputation: 1

I've attached the working compose file, you seem to be missing network through which the dockers should be connected.

Create the shared network for your containers

docker network create shared
version: '3.1'
networks:
  shared: 
    external:
      name: shared
services:
  drupal:
    image: drupal:8
    container_name: drupal_container
    environment:
     DRUPAL_PROFILE: standard
     DRUPAL_SITE_NAME: Drupal
     DRUPAL_USER: user
     DRUPAL_PASS: pass
     DRUPAL_DBURL: mysql://user:pass@mysql:3306/db    # MySQL host should be same as service name created for mysql.
    #or mysql://user:pass@mysql_container:3306/db tried with both
    links:  
     - "mysql:mysql"
    depends_on:
     - mysql
    ports:
     - 80:80
    restart: always
    volumes:
     - /var/www/html/modules
     - /var/www/html/profiles
     - /var/www/html/themes
     - /var/www/html/sites
    networks:
      - shared
  mysql:
    image: mysql:5.7
    container_name: mysql_container
    restart: always
    environment:
     MYSQL_ROOT_PASSWORD: root
     MYSQL_DATABASE: db
     MYSQL_USER: user
     MYSQL_PASSWORD: pass
    ports:
     - 3306:3306
    expose:
     - 3306
    networks:
      - shared

When you try to setup the database in drupal use the following value for host, rest should be same as what you have been using.

Host: mysql

Also, you could just change the 'DRUPAL_DBURL' value in your compose file. This way you are using the default network created by docker compose.

DRUPAL_DBURL: mysql://user:pass@mysql:3306/db

Upvotes: 0

shrikant.sharma
shrikant.sharma

Reputation: 63

it worked, not the way I expected but it did.

sudo docker inspect mysql_container

op->

"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:16:00:02",
"DriverOpts": null

I tried with this IP add ie "IPAddress": "172.22.0.2" drupal and below commands are able to connect

sudo mysql --protocol=TCP -h172.22.0.2 -p3306 -uroot -proot
sudo mysql --protocol=TCP -h172.22.0.2 -p3306 -uuser -ppass

but still would like to know what is the significance of this IP address, how is this different from localhost or 127.0.0.1 and will this remain the same every time I pull the same image on different machines or it changs??

Upvotes: 1

Related Questions