Installing MySQL in Docker fails with error message "Can't connect to local MySQL server through socket"

docker mysql
mysql docker-compose
docker connect to mysql outside container
docker-mysql client
docker mysql port
docker connect to mysql on host
docker run mysql in background
docker mysql connection refused

I'm trying to install mysql inside a docker container,Tried various images from github, it seems they all manage to successfully install the mysql but when I try to run the mysql it gives an error:

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

System specifications:

  • Ubuntu 12,04 its on AWS
  • Docker 0.10.0

Packages I tried so far:


Remember that you will need to connect to running docker container. So you probably want to use tcp instead of unix socket. Check output of docker ps command and look for running mysql containers. If you find one then use mysql command like this: mysql -h 127.0.0.1 -P <mysql_port> (you will find port in docker ps output). If you can't find any running mysql container in docker ps output then try docker images to find mysql image name and try something like this to run it: docker run -d -p 3306:3306 tutum/mysql where "tutum/mysql" is image name found in docker images.

Can't connect with mysql-cli from host to mysql docker container , Hi, I did the following to start a MySQL docker container: docker run keltik85 opened this issue on Dec 17, 2018 · 20 comments mysql -h localhost -P 3306 --protocol=tcp -u root ERROR 2003 (HY000): Can't connect to MySQL server on  Once the server is ready, you can run the mysql client within the MySQL Server container you just started, and connect it to the MySQL Server. Use the docker exec -it command to start a mysql client inside the Docker container you have started, like the following: docker exec -it mysql1 mysql -uroot -p


Don't know how do i achieve this, but, i've be able to reach MYSQL by typing

$ mysql -u root -h

mywebsite:
  image: benftwc/pldev-webserver
  volumes:
    - ./mywebsite.fr/:/var/www/
  working_dir: /var/www/
  ports:
    - "8009:8009"
  command: php -S 0.0.0.0:8009
  links:
    - database
database:
  image: library/mysql
  environment:
    MYSQL_ROOT_PASSWORD: root
  ports:
    - "3310:3306

root@422f4d1f454a:/# mysql -u root -h 127.0.0.1 -p3310
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

root@422f4d1f454a:/# mysql -u root -h database -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g...........

Unable to start mysql service inside container unless I touch files first , ljfranklin opened this issue on Aug 3, 2017 · 5 comments Failure case: docker build -t mysql-fails . docker run -it mysql-fails /bin/bash -c 'service mysql start'  ifischer changed the title Installing mysql gem inside Docker container fails with "Failed to build gem native extension" Installing mysql2 gem inside Docker container fails with "Failed to build gem native extension" Jun 10, 2018


I had the same problem, in fact, I juste forgot to run the service after installation ..

Start mysql server :

/etc/init.d/mysql start

Docker: Can't connect to local MySQL server through socket , But once I create a docker image & run it, the app launched but is unable to connect to my Warning: Error in connection_create: Failed to connect: Can't connect to local MySQL server The newest docker file I've used was based on r​-db. The init scripts run by the entrypoint, internally, use the variable "${mysql[@]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).


If you don't have MySQL installed on your host, you have to execute it in the container (https://docs.docker.com/engine/reference/commandline/exec/#/examples gives explanation about docker run vs docker exec).

Considering your container is running, you might use docker exec yourcontainername mysql -u root -p to access to the client.

Also, if you are using Docker Compose, and you've declared a mysql db service named database, you can use : docker-compose exec database mysql -u root -p

MySQL Installation Guide :: 7.7.1 Basic Steps for MySQL , For more topics on deploying MySQL Server with Docker like server configuration​, persisting data and configuration, server error log, and container environment  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Docker fails to install php mysql extension


I might be little late for answer and probably world knows about this now.

All you have to open your ports of docker container to access it. For example while running the container :

docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306 mysql/mysql-server:5.7

This will allow your container's mysql to be accessible from the host machine. Later you can connect to it.

docker exec -it mysql_container mysql -u root -p

MySQL Installation Guide :: 7.7.2 More Topics on , 7.7.2 More Topics on Deploying MySQL Server with Docker. Note Server Error Log A MySQL Docker installation is different from a common, non-Docker installation in the following This is a known issue of the MySQL Server containers. Take the mysql installation stuff out of the Dockerfile; Change localhost in your python code to mysql or whatever you want to call your MySQL container. Start a MySQL container with something like docker run -d --name mysql mysql; Start your container and link to the mysql container e.g: docker run myapp --link mysql:mysql


MySQL 8.0 Reference Manual :: B.4.2.2 Can't connect to , The error (2003) Can't connect to MySQL server on ' server ' (10061) indicates here: Section B.4.2.2.1, “Connection to MySQL Server Failing on Windows”. You can always run mysqladmin version to check whether the Unix socket file that  I am trying to run mysql into modified ubuntu image which includs installation of Node.js and basic mysql installation using below docker file # Memcached # use the ubuntu base image provided by


MySQL 8.0 Reference Manual :: 2.5.4 Installing MySQL on , 2.5.4 Installing MySQL on Linux Using RPM Packages from Oracle however, using rpm -Uvh instead makes the installation process more prone to failure, due​  When using the docker image to start without an existing database, the container's entrypoint.sh script tries to call the mysqld binary to create the database. This fails in versions later than 5.7.5 because the script starts by calling


MySQL Docker Containers: Understanding the Basics, So, welcome to our new blog series - “MySQL on Docker”. error: database is uninitialized and password option is not specified The MySQL image was successfully downloaded but Docker failed to run it as container  Logs and troubleshooting Estimated reading time: 17 minutes This page contains information on how to diagnose and troubleshoot problems, send logs and communicate with the Docker Desktop team, use our forums and Knowledge Hub, browse and log issues on GitHub, and find workarounds for known problems.