Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

mysql

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:

  • https://github.com/eugeneware/docker-wordpress-nginx
  • https://github.com/tutumcloud/tutum-docker-mysql
like image 751
Mohammad Avatar asked Apr 23 '14 03:04

Mohammad


People also ask

Can't 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 server through socket?

It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ . There are a couple of solutions for this error. Then try to connect again.

How to fix “can’t connect to local MySQL server through socket” error?

If you happen to get “Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)” errors in logging in to mysql-server while working on docker containers: check the path of socket file in /etc/mysql/my.cnf check if mysqld.sock and mysqld.pid file is present in /var/run/mysqld/ directory or not.

How to connect to MySQL in a docker container?

This Connect to mysql in a docker container from the host may help. mysql command try to connect via mysqld.sock, but your mysql server are running inside a container, so no .sock file here. You can try to mount it to the host.

Why am I getting a MySQL connection failed error message?

This error message is basically telling you that the application is not able to locate the socket file needed to establish a MySQL connection. And as you well know the socket file is used by the operating system to enable interface services such as MySQL or PHP to interact and communicate among each other.

How to check if MySQL is running or not?

To check if MySQL is running, run the following command: And if the service is not running, then, by all means, start it up by running the following command: Once you restart the service, try again to connect to MySQL.


2 Answers

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.

like image 161
odk Avatar answered Oct 20 '22 04:10

odk


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

Start mysql server :

/etc/init.d/mysql start
like image 28
Mega Avatar answered Oct 20 '22 06:10

Mega