Home > Enterprise >  ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

Time:12-16

I am trying to move existing project to Docker. I followed this tutorial at M.Academy and followed the setup instructions from Docker on Existing Project

While executing trying to connect with MySQL I am getting this error ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

I also tried to execute: telnet db 3306

Response: Trying 172.17.0.1... telnet: Unable to connect to remote host: Connection timed out

I tried everything but couldn't figure out the problem. I am new to Docker. I am using Ubuntu 18.04 LTE. Haven't changed db.env and got no error on any other step before importing the database.

I further checked and found out that the container is not able to establish connection with MySQL.

P.S. I am successfully able to connect with same MySQL service outside Docker container by using (external MySQL port) mysql -h 127.0.01 -u root -p -P 3306

Steps To Reproduce

  1. Install Docker & Docker Compose on Ubuntu 18.04
  2. Download the Docker Compose template: curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash
  3. Replace with existing source code of your existing Magento instance: cp -R ~/Sites/existing src
  4. Execute: docker-compose -f docker-compose.yml up -d
  5. Copy files to container: bin/copytocontainer --all
  6. Import existing database: bin/mysql < /var/www/magento243.sql

Expected Result Database should have been successfully imported

Actual Result ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

P.S. Issue has already been raised here: https://github.com/markshust/docker-magento/issues/589

docker-compose.yml

version: "3"

services:
  app:
    image: markoshust/magento-nginx:1.18-5
    ports:
      - "80:8000"
      - "443:8443"
    depends_on:
      - "db"
    volumes: &appvolumes
      - ~/.composer:/var/www/.composer:cached
      - ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
      - ~/.ssh/known_hosts:/var/www/.ssh/known_hosts:cached
      - appdata:/var/www/html
      - sockdata:/sock
      - ssldata:/etc/nginx/certs
    extra_hosts: &appextrahosts
      ## M1 Mac support to fix Docker delay, see #566
      - "app:172.17.0.1"
      - "phpfpm:172.17.0.1"
      - "db:172.17.0.1"
      - "redis:172.17.0.1"
      - "elasticsearch:172.17.0.1"
      - "rabbitmq:172.17.0.1"
      ## Selenium support, replace "magento.test" with URL of your site
      - "magento.test:172.17.0.1"

  phpfpm:
    image: markoshust/magento-php:7.4-fpm-11
    volumes: *appvolumes
    extra_hosts: *appextrahosts
    env_file: env/phpfpm.env

  db:
    image: mariadb:10.4
    command: --max_allowed_packet=256M
    ports:
      - "3306:3306"
    env_file: env/db.env
    volumes:
      - dbdata:/var/lib/mysql
    extra_hosts: *appextrahosts

  redis:
    image: redis:5.0-alpine
    ports:
      - "6379:6379"
    extra_hosts: *appextrahosts

  elasticsearch:
    image: markoshust/magento-elasticsearch:7.9.3-1
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - "discovery.type=single-node"
      ## Set custom heap size to avoid memory errors
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
      ## Avoid test failures due to small disks
      ## More info at https://github.com/markshust/docker-magento/issues/488
      - "cluster.routing.allocation.disk.threshold_enabled=false"
      - "index.blocks.read_only_allow_delete"
    extra_hosts: *appextrahosts

  rabbitmq:
    image: rabbitmq:3.8.22-management-alpine
    ports:
      - "15672:15672"
      - "5672:5672"
    volumes:
      - rabbitmqdata:/var/lib/rabbitmq
    environment:
      - RABBITMQ_VM_MEMORY_HIGH_WATERMARK=1GB
    extra_hosts: *appextrahosts

  mailcatcher:
    image: sj26/mailcatcher
    ports:
      - "1080:1080"
    extra_hosts: *appextrahosts

  ## Selenium support, uncomment to enable
  #selenium:
  #  image: selenium/standalone-chrome-debug:3.8.1
  #  ports:
  #    - "5900:5900"
  #  extra_hosts: *appextrahosts

volumes:
  appdata:
  dbdata:
  rabbitmqdata:
  sockdata:
  ssldata:

CodePudding user response:

This issue was finally resolved by

  1. Removing extra_hosts entries from the YML file
  2. Adding networks in YML

Final docker-compose.yml

version: "3"

services:
  app:
    image: markoshust/magento-nginx:1.18-5
    ports:
      - "81:8000"
      - "444:8443"
    depends_on:
      - "db"
    volumes: &appvolumes
      - ~/.composer:/var/www/.composer:cached
      - ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
      - ~/.ssh/known_hosts:/var/www/.ssh/known_hosts:cached
      - appdata:/var/www/html
      - sockdata:/sock
      - ssldata:/etc/nginx/certs
    networks:
      - customNetwork

  phpfpm:
    image: markoshust/magento-php:7.4-fpm-11
    volumes: *appvolumes
    env_file: env/phpfpm.env
    networks:
      - customNetwork

  db:
    image: mariadb:10.4
    command: --max_allowed_packet=256M
    ports:
      - "3307:3306"
    env_file: env/db.env
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - customNetwork

  redis:
    image: redis:5.0-alpine
    ports:
      - "6379:6379"
    networks:
      - customNetwork

  elasticsearch:
    image: markoshust/magento-elasticsearch:7.9.3-1
    ports:
      - "9201:9200"
      - "9301:9300"
    environment:
      - "discovery.type=single-node"
      ## Set custom heap size to avoid memory errors
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
      ## Avoid test failures due to small disks
      ## More info at https://github.com/markshust/docker-magento/issues/488
      - "cluster.routing.allocation.disk.threshold_enabled=false"
      - "index.blocks.read_only_allow_delete"
    networks:
      - customNetwork

  rabbitmq:
    image: rabbitmq:3.8.22-management-alpine
    ports:
      - "15672:15672"
      - "5672:5672"
    volumes:
      - rabbitmqdata:/var/lib/rabbitmq
    environment:
      - RABBITMQ_VM_MEMORY_HIGH_WATERMARK=1GB
    networks:
      - customNetwork

  mailcatcher:
    image: sj26/mailcatcher
    ports:
      - "1080:1080"
    networks:
      - customNetwork

volumes:
  appdata:
  dbdata:
  rabbitmqdata:
  sockdata:
  ssldata:

networks:
  customNetwork:
  • Related