Docker Compose: can't connect to my MySQL container - mysql

I'm getting the following error when I try to connect to my MySQL container from another container:
$ mysql -h127.0.0.1 -uroot
ERROR 2002 (HY000): Can't connect to MySQL server on '127.0.0.1' (115)
I can see that my container is running on port 3306:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a341fa5075c2 5961245d3135 "httpd-foreground" 3 minutes ago Up 3 minutes 127.0.0.1:8080->80/tcp sfcoding_cwj_1
e7683d546ad1 4da164162573 "docker-entrypoint.s…" 28 minutes ago Up 3 minutes 127.0.0.1:3306->3306/tcp sfcoding_mysql_1
Here is my docker-compose.yml:
---
version: '3.8'
services:
cwj:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/usr/src/app:cached
ports:
- "127.0.0.1:8080:80"
mysql:
image: mariadb:10.5.8
volumes:
- db_data:/var/lib/mariadb
ports:
- "127.0.0.1:3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
volumes:
db_data: {}
Any ideas as to why it won't connect?

Turns out I need to make my host match my service name, which is mysql.
mysql -h127.0.0.1 -uroot # does not work
mysql -hmysql -uroot # works

Related

Why can't I run the mysql command in docker compose?

I have a project with a mysql database in a container. I use docker-compose to set my project up. And I want to run the mysql command to inspect te database.
So I did, and get:
docker-compose run --rm database mysql
Creating myproject_database_run ... done
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
However when I tried this it works:
docker exec -it myproject_database_1 mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Can anybody explain me this?
My docker-compose file:
version: "3.7"
services:
database:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "127.0.0.1:3306:3306"
env_file: .env
volumes:
- type: volume
source: db_data
target: /var/lib/mysql
- type: bind
source: ./my.cnf
target: /etc/my.cnf
read_only: true
volumes:
db_data:
testing_images:
docker-compose run creates a new container. That's perfectly normal, but if your mysql client is configured to connect via a Unix socket, the new container will have a new filesystem and won't be able to see the main database container's /var/run directory.
When you use docker-compose run, you need to specify a TCP connection, using the setup described in Networking in Compose in the Docker documentation. For example,
docker-compose run --rm database \
mysql -h database
Since you publish ports: out of the container, you should be able to reach it from the host, without Docker. The trick here is that the mysql command-line client interprets localhost as a magic term to use a Unix socket and not a normal host name, so you need to specifically use the IP address 127.0.0.1 instead.
# From the same host, without anything Docker-specific
mysql -h 127.0.0.1
Try adding MYSQL_ROOT_PASSWORD in the environment.
environment:
MYSQL_ROOT_PASSWORD: root
This is from one of my working compose file
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
db_mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
## map volume
volumes:
db_mysql:
## in network, we can define any name under networks:
networks:
app-network:
driver: bridge
FYI: Official MySQL docker image - Docker Hub

Docker Compose: Import MySQL database into a running docker container from CLI

After trying various things whatever I do I get certain error messages - I want to import a local SQL file database into a running docker container.
This is what I try to do:
sudo docker-compose exec project-db mysql -uuser -ppassword database_name < "/localpath/2020-05-03-14_00-hexagon_brainformance.sql"
This is my docker compose file:
db:
image: mysql:5.7.25
container_name: "project-db"
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3306:3306
volumes:
This is what sudo docker ps gives me:
cb2c5e884c90 mysql:5.7.25 "docker-entrypoint.s…" 19 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp project-db
This is my current error:
ERROR: No such service: project-db
Tried various variations with using db instead of project-db and also with -it flag to go into the container first. What am I doing wrong?

MySQL commands inside Docker container

I have the following docker compose:
version: '3.1'
services:
db:
image: mysql:5.5
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=some_root_pw
- MYSQL_DATABASE=some_db
- MYSQL_USER=db_user
- MYSQL_PASSWORD=user_pw
ports:
- 8306:3306
volumes:
- ./db:/var/lib/mysql/some_db
I am accessing the container with the command:
docker-compose exec db /bin/sh
Which drops me at the shell as expected. However, I am unable to access mysql with the information from the docker-compose file.
mysql -u root -p
Prompts for the password, then rejects for bad username or password. I have tried with both root and user. Is there a way I can troubleshoot what is happening with user creation?
NOTE: intentionally older version of MySQL.

Django container cannot find its mysql container

I have built my django docker with hostname server_default and with --network=server_default and ran mysql with same network(mysql container has ran before django server) when I check my mysql container everything is ok but when I run my django server it fails with error :
"Can't connect to MySQL server on 'server_default' ([Errno -2] Name or service not known)"
I attached to my server container and I couldn't connect to mysql container.
server_default is a bridge type.
my run commands :
sudo docker run -d -p 8000:8000 --network=server_default scotech-server
sudo docker run -d --network=server_default scotech-db
I couldn't do it with docker individually but with docker-compose.yml and 2 build context I could connect them together : it seems a bad solution :(
version: '3'
services:
scotech-db:
build:
context: ./scotech-mysql-docker
expose:
- 3306
web:
build:
context: ./server
depends_on:
- scotech-db
restart: on-failure
ports:
- "8002:8000"

How to connect to mysql created by docker-compose

I can not connect to Mysql created with Docker in local environment (Mac OS X).
I have created the following configuration file.
version: '2'
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mysqldatabase
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
MYSQL_ROOT_PASSWORD: password
ports:
- "33333:3306"
container_name: mysql-db
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: local
Then, I started the docker container of mysql with the following command.
$ docker-compose up -d
$ docker start mysql-db
Up to this point there was no problem, but an error occurred when trying to connect to mysql.
$ mysql -p 33333 -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Please tell me how to deal with it.
Try mysql -h 0.0.0.0 -P 33333 -u root -p.
Note port here has upper case P and password has lower case p.
Hope this helps!
I think if you connect from the host to the container. mysql will see the connection from non-local host, so disconnect it. You can change mysql config to allow other hosts, or connect from inside that container using docker exec -it mysql ...