2

I have a Dockerfile that I am working on that pulls Mysql 5.6 and configures it (mostly with a bash and sql script). I am able to build and run it but when I try to connect to the database in the container I always get:

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

I have tried accessing the mysql database by using:

mysql -u root -p

mysql -u root -h 127.0.0.1 -p

I have tried everything I could think of and looked up articles on the internet but nothing works. Can someone tell me why? Here is my Dockerfile and bash script respectively:

FROM mysql:5.6

MAINTAINER Ryan K.
USER root

ADD mysqlAddUser.sh /tmp/
CMD ["/tmp/mysqlAddUser.sh"]
ADD foo.sql /docker-entrypoint-initdb.d/foo.sql

EXPOSE 3306

## Starting mysqld and running Database Scripts
CMD ["/usr/bin/mysqld_safe"]

Bash script:

#!/bin/bash
DATABASE_PASSWORD=test

/usr/bin/mysqld_safe &
mysqladmin --login-path=local -uroot -p"$DATABASE_PASSWORD"
mysqladmin password "$DATABASE_PASSWORD"

mysql -uroot -p"$DATABASE_PASSWORD" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_PASSWORD') WHERE User='root'"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User=''"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES"

DB_ROOT_PASS=TEST
DB=portal

mysql --login-path=local -uroot -p"$DB_ROOT_PASS"
mysql -uroot -p"$DATABASE_PASWORD" -e "CREATE DATABASE portal";
mysql -uroot -p"$DATABASE_PASSWORD" -e "CREATE USER portaluser@'localhost' IDENTIFIED BY 'testing'";
mysql -uroot -p"$DATABASE_PASSWORD" -e "GRANT ALL PRIVILEGES ON portal.* TO portaluser@'localhost'";
mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES";

mysql -uroot -p"$DATABASE_PASSWORD" $DB < /tmp/foo.sql
ryekayo
  • 2,033
  • 2
  • 18
  • 42
  • The socket is present in the container. If you are connecting from outside the container you have to provide the host with `-h` so that mysql can find your database. – Norbert van Nobelen Jan 20 '16 at 19:44
  • I access it from within the container.. Basically run the container in /bin/bash and try to access the db from there using mysql -u root -p – ryekayo Jan 20 '16 at 19:45
  • Does the socket exist at that path in the container? – Etan Reisner Jan 20 '16 at 21:19
  • Last time I checked it didn't. – ryekayo Jan 20 '16 at 21:20
  • Does this help? http://stackoverflow.com/questions/5376427/cant-connect-to-local-mysql-server-through-socket-var-mysql-mysql-sock-38 – Alkis Kalogeris Jan 20 '16 at 21:56
  • Some questions that will help debugging: Can you confirm you are using `docker exec` to get into the container and test? It is unclear if you are connecting locally from inside the container or externally. Can you post the `docker run` commands used to start the daemon and get into the container? Can you list `/var/run/mysqld/` to confirm that the socket actually exists? Can you post the `my.cnf` being used? – Andy Shinn Jan 21 '16 at 05:42
  • It's strange but actually I can't even build the container. – Opal Jan 21 '16 at 09:03
  • @AndyShinn I will look that stuff up once I get to work – ryekayo Jan 21 '16 at 11:40
  • @AndyShinn the mysql.sock file doesn't exist. Not sure of a way to view the contents of my.cnf since this is just a mysql container – ryekayo Jan 21 '16 at 13:50
  • Are you running two containers? I mean, first "docker run -d -it " and second "docker exec -it bash" ; Well, I actually doubt that mysqld daemon had started. – VDR Jan 21 '16 at 14:56
  • No i am only running one container... I am grabbing the mysql container in my script and am trying to create the database and configure it – ryekayo Jan 21 '16 at 14:57
  • Also, I see an error in your dockerfile. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect. You might want to use "RUN /tmp/mysqlAddUser.sh" instead of CMD ["/tmp/mysqlAddUser.sh"]. And in your bash script, you are calling /tmp/foo.sql (mysql -uroot -p"$DATABASE_PASSWORD" $DB < /tmp/foo.sql) but the file didn't exit in that location(you are copying foo.sql to /docker-entrypoint-initdb.d/foo.sql ) so the last line will fail.. Put the ADD foo.sql instruction before the RUN /tmp/mysq.. instruction and update your script – VDR Jan 21 '16 at 15:09
  • OK I will try that.. I actually realized that yesterday and did entry point instead – ryekayo Jan 21 '16 at 15:10

1 Answers1

0

Was struggling with the same issue with mysql in a docker container. Sometimes I could connect with the mysql client, but more often not. Switched to mariadb and had the same problem.

What seems to have fixed it for me is to add some sleep commands in my scripts that create, start and destroy the docker containers. After commands like 'docker run' and 'docker stop' I added 'sleep 10' and that seems to help.

  • Thanks for that information. I actually found out why this was occurring.. The reason being is because i had an Ubuntu VM with only 1GB of Virtual ram allocated. That would most certainly cause issues especially if I had one container with Tomcat running. – ryekayo Aug 02 '16 at 12:55