Cant connect to local mysql server through socket docker

This question is tagged with mysql docker

~ Asked on 2014-04-23 03:47:28

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.

~ Answered on 2014-05-05 11:59:55

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

Start mysql server :

/etc/init.d/mysql start

~ Answered on 2017-03-15 13:25:21

When I try to run mysql command through bash, I get this error.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

This is my docker-compose file

db:
    image: mariadb:10.0.34
    volumes:
      - ./latest_dump.sql:/docker-entrypoint-initdb.d/latest_dump.sql
    restart: always
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: *******

How can I solve this issue?

From the error message, I can only assume you're using the mysql client from your host and trying to connect it to localhost. The mysql client has a special case where it converts a hostname of localhost to the default Unix socket path instead, hence the error you're seeing. Swap localhost for 127.0.0.1 and it'll probably work.

In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

letanthang, nakarimi, ArturFortunato, MRROOX, and umarmughal824 reacted with thumbs up emojirodgomesc, simonjcarr, faouziMohamed, and umarmughal824 reacted with hooray emojiiltoga, xrysanthos, aadiene, james868198, simonjcarr, bernardobarreto, letanthang, rsegeda, faouziMohamed, and umarmughal824 reacted with heart emoji

I encountered the same problem, and I confirm, it works with host explicitly set with 127.0.0.1
mysql -u ${mariadb_username} -p${mariadb_password} -h 127.0.0.1
Thanks @tianon

From the error message, I can only assume you're using the mysql client from your host and trying to connect it to localhost. The mysql client has a special case where it converts a hostname of localhost to the default Unix socket path instead, hence the error you're seeing. Swap localhost for 127.0.0.1 and it'll probably work.

In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

thanks a lot. It really worked.

I started docker mysql container and try to connect it outside docker using mysql command but got error:

$mysql-uroot-proot

mysql:[Warning]Usingapassword on the command line interfacecan be insecure.

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

It's because mysql clinet will use unix socket protocol as default protocol value to connect to mysql server if not specifying protocol or host argument, but mysql docker container is listening on tcp socket.

Solution

$mysql-uroot-proot-h227.0.0.1

Specify IP address as host argument value in mysql command will make mysql to connect using TCP protocol instead of default unix socket.
(Note that using localhost as host argument value will still using unix socket protocol)

Or use

$mysql-uroot-proot--protocol tcp

This command will specify protocol argument explicitly

Post Views: 6

Can't connect to local MySQL server through socket Kubernetes?

How to Fix 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.
Method 1: Check the MySQL Service..
Method 2: Verify the mysqld.sock Location..
Method 3: Check the MySQL Folder Permission..
Method 4: Check for Multiple MySQL Instances..

How do I connect to a MySQL docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Can not connect to local MySQL server?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can't connect to local MySQL server through socket Ubuntu?

"Try" to run mysql via /etc/init. d/mysql start if it gives you the exact same error from above then you need to copy the mysql. server file from the mysql you downloaded which can be found in the support-files folder inside the mysql folder you downloaded or in the /usr/local/mysql folder and copy it to /etc/init.

How can docker container connect to localhost?

You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.