ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

docker
mysql

#1

Last times I often get error when using MySQL via Docker Compose:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
read unix @->/var/run/docker.sock: read: connection reset by peer
Exited with code 1

Part of my config:

version: 2
jobs:
  build:
    machine: true

    steps:
      - checkout

      - run:
          name: Install Docker Compose
          command: |
            curl -L https://github.com/docker/compose/releases/download/1.11.2/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
            chmod +x ~/docker-compose
            mv ~/docker-compose /usr/local/bin/docker-compose

      - run:
          name: Running Docker containers
          command: docker-compose up -d

      - run:
          name: Import test database and run migrations
          command: docker exec -i database mysql -u root -ppass --database=name < dump.sql

I have more containers but they work good, only problem with MySQL when I try to import database.
First time this error appeared 3-4 weeks ago, maybe more. Before everything worked fine, and now it works fine, but sometimes I get this error, and I need to rebuild 2-3 times and this error should disappear.

Part of my docker-compose.yml:

version: "2"

networks:
  app-tier:
    driver: bridge

services:
  database:
    container_name: database
    image: "mariadb:10.2"

  phpfpm:
    container_name: php-fpm
    depends_on:
      - database
    image: "php:7.1"
    networks:
      - app-tier

#2

It’d be best for readers to see your docker-compose.yml so we can see what services you’re running.

However, at a guess, you need to wait a bit before running the docker exec, so that the server is fully initialised. Try this:

command:  sleep 2 && docker exec -i database mysql -u root -ppass --database=name < dump.sql

#3

Thank you for reply, I added part of docker-compose.yml to my first message. But the problem is that php-fpm depends on mysql docker and it works fine. But I will try set sleep, maybe it will be fix my problem, but I dont think so, before run mysql container docker container with php run composer install and it takes some time, and then only mysql try to import database, I think it had much time for startup.


#4

Hmm, the other thing that might be an issue - which I advised someone about recently on this board - is that you may be trying to connect to a MySQL database via a local socket when a TCP connection is required. I believe that TCP is necessary, since the server is not running locally.

It’s very odd that it is intermittent, though.


#5

For today I had 10+ builds and all works fine when I added sleep 2 as you said, maybe it was solution for me.


#6

Great. There is also a widely available wait-for-port command, you could try that as well (I forget what it is called). It basically keeps checking a specified port (e.g. 3306 for MySQL) until it is open. It may be a bit more reliable than a sleep.


#7

You may be referring to the Dockerize utility.

- run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

#8

That’s the puppy, thanks @KyleTryon :smiley_cat: