0

I am running a mysql server inside a docker container using docker-compose, here is my yaml file:

version: '3'
services:
 mysqltest:
  image: mysql
  network_mode: host
  ports:
   - "3306:3306"
  volumes:
   - "/home/myuser/bds/mysql/:/var/lib/mysql"
  user: "1000:1000"
  environment:
   - "MYSQL_ROOT_PASSWORD=mysecret"

The container loads file with the docker-compose up command, but when i try to connect from the host machine to the mysql server with user root, it fails:

mysql -h 127.0.0.1 -u root -pmysecret
ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)

However, i can connect to the server inside the container using the same command, if i start the container using docker command line:

docker run -it --rm --name mysqltest --user 1000:1000 -v /home/myuser/bds/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecret -p 3306:3306 -d mysql

I tried setting network_mode to host, none and also without specifying it, but the result is the same using docker-compose.

What could be wrong with my YAML file so that i cannot connect as when i use docker command ?

Thanks in advance

Rodolfo Conde
  • 49
  • 1
  • 7
  • For me it works when I don't specify it (= default mode which is bridge). What is your default mode? You can check: docker network ls and search for the network created w your docker-compose – lvthillo Sep 17 '21 at 22:26

2 Answers2

0

I assume you didn't change your default Docker configuration. By default Docker will run in the network mode: 'bridge' and not host. See the difference here.

You can check using docker inspect container. When you just start the container using the command you will start your container in bridge mode and the container port 3306 will be mapped on 3306. This will not happen when you try host. Again see the link for the difference.

So update your docker-compose.yaml and define bridge as network mode:

version: '3'
services:
 mysqltest:
  image: mysql
  network_mode: bridge
  ports:
   - "3306:3306"
  volumes:
   - "/home/myuser/bds/mysql/:/var/lib/mysql"
  user: "1000:1000"
  environment:
   - "MYSQL_ROOT_PASSWORD=mysecret"
lvthillo
  • 28,263
  • 13
  • 94
  • 127
  • I checked the default network mode for my docker install. It is bridge mode, so it is in this mode that i can connect to the mysql server using the command line docker command to start the container. I tried to specify manually bridge network mode in the yaml file, but the result is the same, i still cannot connect to the server from the host – Rodolfo Conde Sep 17 '21 at 23:49
  • do the container logs show something special? – lvthillo Sep 18 '21 at 07:22
  • I have not check the logs, i will take a look. I am begining to suspect that it could be the mysql version – Rodolfo Conde Sep 18 '21 at 22:27
0

SOLVED !! The issue was originated from setting different mysql root passwords with the different docker commands:

  1. The first time i was testing the container i user std docker command and the environment MYSQL_ROOT_PASSWORD=othersecret. Everything worked fine. i connected from host using mysql command and restored a database
  2. Once i finished testing, i wrote yaml file, but with the root password i wanted to use for producction:
environment:
 MYSQL_ROOT_PASSWORD: "mysecret"

But then, when i started the container with docker-compose and the yaml file, i could not connect from host using mysql command. It seems the original root password ("othersecret") was written to the user table inside the mysql schema and that is why i could not connect using the password set in the yaml file. I cleaned up the mysql directory and started the container using docker-compose (without specifying a network mode) and finally i was able to connect from host.

Rodolfo Conde
  • 49
  • 1
  • 7
  • In a few days, if you can, please come back to this and mark it as the solution so others know what to do if they have similar issues. Thank you. –  Sep 19 '21 at 01:13
  • 1
    I tried to mark it right now, but SO says i must wait an hour, will come back later :) – Rodolfo Conde Sep 19 '21 at 19:47