mysqldockersocketsdocker-composeunix-socket

Unable to connect to docker mysql unix socket


I'm trying to setup mysql within a docker container but I want to connect to it via a unix socket when I try to connect I get this error:

(web-YtJryoMm-py3.8) redacted@myhost testsocket % mysql -u root --socket=/Users/redacted/testsocket/mysql.sock
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/Users/redacted/testsocket/mysql.sock' (61)

As you can see the mysql.sock file does exist within that location:

(web-YtJryoMm-py3.8) redacted@myhost testsocket % ls -la /Users/redacted/testsocket 
total 16
drwxr-xr-x    5 redacted  staff   160 Oct 19 10:08 .
drwxrwxrwx+ 159 redacted  staff  5088 Oct 19 10:18 ..
srwxrwxrwx@   1 redacted  staff     0 Oct 19 10:08 mysql.sock
-rwxrwxrwx@   1 redacted  staff     2 Oct 19 10:08 mysql.sock.lock
-rwxrwxrwx@   1 redacted  staff     2 Oct 19 10:08 mysqld.pid

I chmod-ed all of the socket files for testing purposes to make sure permissions were not an issue.

When I try and connect via the tcp protocol it works fine:

(web-YtJryoMm-py3.8) redacted@myhost testsocket % mysql -u root --protocol=tcp
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.32 Source distribution

Here is my docker-compose.yaml file I'm using:

version: '3.9'

services:
  
  mysql:
    image: bitnami/mysql:8.0.32
    container_name: 'mysql'
    restart: always
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    volumes:
      - '~/volumes/mysql:/bitnami/mysql'
      - '~/testsocket:/opt/bitnami/mysql/tmp'
    ports:
      - '3306:3306'

networks:
  default:
    driver: bridge

What am I missing here?


Solution

  • The unix socket works only for local interprocess communication, not for tcp.

    Since your MySQL Server is running inside a Docker container, it's not a local process outside the container. You can't connect to it using a unix socket, even if you have mapped your Docker container's filesystem so you can see the socket file.

    You could use the unix socket to connect by a client that is also running inside the same Docker container with the MySQL Server process.