How to Resolve Docker Local MySQL “Warning: Error in connection_create: Failed to connect” Issue

Most of the developers building the docker images to reduce the setup time for development testing environment. Developer’s use the production equal docker container to do the testing for a developed product. Also, it enables better continuous integration and continuous deployment strategies.

As a development testing setups, most of the people run the MySQL as a service inside the docker container rather than using the cloud database service or separate database server.

Please find the below docker file for example -

Use the Ubuntu OS for the docker image

FROM ubuntu:16.04

Installing the MYSQL using the apt commands

RUN apt-get updateRUN apt-get install -y mysql-server

Can start the MySQL server

RUN service mysql start

Most of the time when building the docker image process is stopped and throwing an error by saying

Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”)

This is basically happening because of the MySQL file directory permission issue.

To resolve this issue, you want to set the correct permissions for MySQL directories.

RUN chown -R mysql:mysql /var/lib/mysql && service mysql start

If you use the CMD command and start the MySQL server when the container launching in build image.

Set the MySQL directories permissions as below,

CMD chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && service mysql restart

Reference —

https://support.openanalytics.eu/t/docker-cant-connect-to-local-mysql-server-through-socket/1408

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet