Reputation: 1929
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:
Packages I tried so far:
Upvotes: 118
Views: 237171
Reputation: 376
As informed by the user Jozala, adding the --protocol fixes the issue:
mysql -h "$container_ip" -P 3307 --protocol=tcp -u root -p
Upvotes: 0
Reputation: 483
Try defining hostname like this
mysql -h mysql -u root -p
where
mysql -h <hostname(could be localhost,127.0.0.1,mysql)> -u root -p
Upvotes: 3
Reputation: 31225
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
Upvotes: 1
Reputation: 8055
Basically, it takes some time to start after pulling MySQL. Make sure you tried after a few minutes (maybe 2-3 minutes).
Upvotes: 2
Reputation: 86
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
Upvotes: 2
Reputation: 31226
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.
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
root@8880ffe47962:/# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@8880ffe47962:/# 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)
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>
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
root@e7852ff02683:/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)]>
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
root@3738cf6eb3e9:/# 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>
Upvotes: 10
Reputation: 704
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
Upvotes: 6
Reputation: 4313
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
Upvotes: 17
Reputation: 840
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
Upvotes: 1
Reputation: 454
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:PASSWORD@docker_container_name/DB_NAME
to access to Mysql socket through docker (also works with docker-compose, use compose-name instead of container one)
Upvotes: 4
Reputation: 2376
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/
Upvotes: 5
Reputation: 2984
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
Upvotes: 10
Reputation: 415
I had the same problem, in fact, I juste forgot to run the service after installation ..
Start mysql server :
/etc/init.d/mysql start
Upvotes: 31
Reputation: 199
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
Upvotes: 7
Reputation: 454
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
root@422f4d1f454a:/# 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)
root@422f4d1f454a:/# mysql -u root -h database -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g...........
Upvotes: 19
Reputation: 2252
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
.
Upvotes: 122