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
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
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
.
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>
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
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
For me it was simply a matter of restarting the docker daemon..