Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am trying to connect to mysql server using mysql cli. Image was created using following command:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d mysql

The docker container is running. docker ps prints:

johnd@john-pc:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
598c0f8680dc        mysql               "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        3306/tcp, 33060/tcp    some-mysql

When i enter the following command: mysql -h 127.0.0.1 -P 3306 -u root -p it returns:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

I also tried with --protocol=tcp attribute. How do i connect from client to mysql server running on docker using terminal from client machine (not from another docker)

EDIT: I also tried connecting to mysql using this command:

docker run -it --rm mysql mysql -h127.0.0.1 -uexample-user -p

It returns the same error

like image 680
Matej J Avatar asked May 10 '19 07:05

Matej J


1 Answers

mysql -h 127.0.0.1 -P 3306 -u root -p

You are connecting to your localhost's sql server but you didn't map the docker's container port to the host. Try mapping the port to your host by this:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d -p 3306:3306 mysql

Then retry:

mysql -h 127.0.0.1 -P 3306 -u root -p

like image 62
nivhty Avatar answered Oct 12 '22 13:10

nivhty