2

I'm trying to connect, from WSL2, to a MySQL DB hosted in a docker container. I can reach the container from Windows side, but I cannot reach the container from WSL side.

I've found several answers that explain how to connect from container to container, from windows to the container or from the container to windows, but no clear way of reaching a container from WSL2.

I've connected to the DB using Workbench from Windows side without any problem. But if I try connecting to the DB from Ubuntu mysql -h host.docker.internal -p 3306 -u THE_USERNAME -pTHE_PASSWORD (Yes I've checked the credentials and port are ok) I get ERROR 2003 (HY000): Can't connect to MySQL server on 'host.docker.internal' (110). Also tried using localhost instead of host.docker.internal, but I get ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

If I ran docker ps I can clearly see the container running with the correct ports set. For what I've read I should be using host.docker.internal, however in the Ubuntu hosts file it's mapped to some ip which I cannot reach from WSL, but can reach from windows side.

I have the following set up: The whole setup was done following the official docs.

  • Windows 10 Pro Version 2004 Build 19041
  • WSL2 w/ Ubuntu 20.04
  • Docker Desktop WSL2 backend (with WSL2 integration enabled)

The containers docker-compose.yml:

services:
  mysql:
    image: mysql:5.7.26
    command: --default-authentication-plugin=mysql_native_password
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: 
    volumes:
      - mydata:/var/lib/mysql
      - ./:/home/xx/yy/zz
    ports:
      - 3306:3306
    networks:
      - local_network
networks:
    local_network:
volumes:
  mydata:

Everything points that it's a networking - naming problem, but I cannot find nothing related... Any help would be appreciated :D

FireHuillin
  • 31
  • 2
  • 5
  • 1
    I've found a workaround, according to this [question](https://stackoverflow.com/questions/32360687/connect-to-docker-mysql-container-from-localhost?rq=1) using `docker-compose up` should work...but in my case it doesn't, but after running `docker-compose run --service-ports ` I could connect using `127.0.0.1` instead of `host.docker.internal`, which I would rather use... – FireHuillin May 05 '21 at 20:32
  • Kinda same setup here but connecting to postgresql. answer worked for me: `psql -h 127.0.0.1 -p 5432` from inside the WSL2 Ubuntu to a postgres docker running on the windows 10 host. Port 5232 was mapped out of the container. – agenteo Nov 01 '22 at 19:53

1 Answers1

1

You need to use a capital letter P for the port and a lowercase letter p for the password:

mysql -h host.docker.internal -P 3306 -u THE_USERNAME -pTHE_PASSWORD
intcreator
  • 4,206
  • 4
  • 21
  • 39