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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
.
Method 2
I had the same problem, in fact, I juste forgot to run the service after installation ..
Start mysql server :
/etc/init.d/mysql start
Method 3
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
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8aab7b7ac98eceaeabeecbce9beecedecb9">[email protected]</a>:/# 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)
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c6e7373685c282e2e7a28782d7a2829287d">[email protected]</a>:/# mysql -u root -h database -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g...........
Method 4
In my case I tried to connect to DB (which was inside docker) like this:
mysql -ppass -u root
but got same error as OP.
Specifying host and port helped:
mysql --host 0.0.0.0 --port 3306 -ppass -u root
Method 5
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
Method 6
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
Method 7
Check out what’s in your database.yml
file. If you already have your plain Rails app and simply wrapping it with Docker, you should change (inside database.yml
):
socket: /var/run/mysqld/mysqld.sock #just comment it out
to
host: db
where db
is the name of my db-service from docker-compose.yml
. And here’s my docker-compose.yml
:
version: '3' services: web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" links: - db db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: root
You start your app in console (in app folder) as docker-compose up
. Then WAIT 1 MINUTE (let your mysql service to completely load) until some new logs stop appearing in console. Usually the last line should be like
db_1 | 2017-12-24T12:25:20.397174Z 0 [Note] End of list of
non-natively partitioned tables
Then (in a new terminal window) apply:
docker-compose run web rake db:create
and then
docker-compose run web rake db:migrate
After you finish your work stop the loaded images with
docker-compose stop
Don’t use docker-compose down
here instead because if you do, you will erase your database content.
Next time when you want to resume your work apply:
docker-compose start
The rest of the things do exactly as explained here: https://docs.docker.com/compose/rails/
Method 8
Months after this question, I’ve levelup my Docker skills. I should use Docker container name instead.
That use dokerized-nginx as bridge to expose ip+port of the container.
Within WEB configuration, I now use mysql://USERNAME:[email protected]_container_name/DB_NAME
to access to Mysql socket through docker (also works with docker-compose, use compose-name instead of container one)
Method 9
Specifying the host as 0.0.0.0 did work for me.
I created docker container using below command
docker run --detach --name=mysql --env="MYSQL_ROOT_PASSWORD=root" --publish 3306:3306 mysql
Then installed mysql client
sudo apt-get install mysql-client
Connected to mysql via terminal using below command
mysql --host 0.0.0.0 --port 3306 -proot -u root
Method 10
Assuming you’re using docker-compose, where your docker-compose.yml
file looks like:
version: '3.7' services: mysql_db_container: image: mysql:latest command: --default-authentication-plugin=mysql_native_password environment: MYSQL_ROOT_PASSWORD: rootpassword ports: - 3307:3306 volumes: - mysql_db_data_container:/var/lib/mysql web: image: ${DOCKER_IMAGE_NAME-eis}:latest build: context: . links: - mysql_db_container ports: - 4000:3000 command: ["./scripts/wait-for-it.sh", "mysql_db_container:3306", "--", "./scripts/start_web_server.sh"] volumes: - .:/opt/eis:cached env_file: - .env volumes: mysql_db_data_container:
Notice the ports
definition for mysql_db_container
ports: - 3307:3306
<= That indicates that mysql will be accessible via port 3307 to the localhost workstation and via port 3306 within the docker net
Run the following to see your container names:
$ dc config --services mysql_db_container web
In this case, we have two containers.
Errors
If you connect to mysql_db_container
from your localhost workstation and try to access the mysql console there, you’ll get that error:
docker-compose run mysql_db_container bash <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6614090912265e5e5e5600000352515f5054">[email protected]</a>:/# mysql -u root -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cfee3e3f8ccb4b4b4bceaeae9b8bbb5babe">[email protected]</a>:/# exit
Also, if you try to connect from your local workstation, you’ll also get that error:
$ mysql -u root -p -P 3307 Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Solutions
Connecting from local workstation
Just add the --protocol=tcp
parameter (otherwise mysql assumes you want to connect via the mysql socket):
$ mysql --protocol=tcp -u root -p -P 3307 Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 11 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
Connecting from web container
Reference the docker hostname -h mysql_db_container
. Note that when you’re running within the context of Docker that the TCP protocol is assumed.
$ dc run web bash Starting eligibility-service_mysql_db_container_1_d625308b5a77 ... done <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9d80809baf8ad8d7dadd8989dfddd9d7dc">[email protected]</a>:/opt/eis# mysql -h mysql_db_container -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or g. Your MySQL connection id is 18 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MySQL [(none)]>
Connecting from mysql container
Assuming your mysql container name is eis_mysql_db_container_1_d625308b5a77
(that you can see when running docker ps
), the following should work:
$ docker exec -it eis_mysql_db_container_1_d625308b5a77 bash <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebca1a1ba8efdf9fdf6ada8f8abacfdabf7">[email protected]</a>:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 19 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
Method 11
My problem was that I was trying to connect from a version of mysql client that seems to be incompatible with the mysql server I installed (mysql:latest which installed version 8.0.22 at the time of this writing).
my mysql client version:
$ mysql --version mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper
The docker command that I used to install mysql:latest:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:latest
The errors I got when connecting from my local mysql client to the mysql server:
$ mysql -u someuser -p -h 127.0.0.1 ERROR 2026 (HY000): SSL connection error: unknown error number
(sometimes I would get another error: “ERROR 2013 (HY000): Lost connection to MySQL server at ‘reading initial communication packet’, system error: 2”. But I think this happens when I try to connect to the server too early after I started it)
My solution was to install mysql:5.7 instead:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:5.7
and then I can connect to the server (after waiting perhaps 1 minute until the server is ready to accept connections):
$ mysql -u someuser -p -h 127.0.0.1
Method 12
For me it was simply a matter of restarting the docker daemon..
Method 13
I ran into the same issue today,
try running ur container with this command.
docker run --name mariadbtest -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb/server:10.3
Method 14
use ip address ‘127.0.0.1’ instead hostname ‘localhost’
Method 15
Basically, it takes some time to start after pulling MySQL. Make sure you tried after a few minutes (maybe 2-3 minutes).
Method 16
To connect to mysql from your machine use 127.0.0.1
To connect to mysql-container from inside another container you need to figure out the internal ip.
docker inspect mysqlcontainername | grep Gateway
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0