Why does MariaDB docker container not require a password from the host, but does from inside the container?

Viewed 33

Have a very odd occurrence with MariaDB Container (10.9 latest image) spun up using a docker-compose file.

The container all works, if I access the container's shell I can run mysql -u root -p and use my password to access mysql.

I patch port 3306 through to my M2 Mac and I get " Access Denied for root@'localhost' " when trying mysql -h 127.0.0.1 -u root -p and the same password.

If however I leave the password field blank, it connects. I'am at a loss why though! I thought it may be something like root@'%' having a blank pwd, but I have tried setting that to the same password. But either way I would expect the Access denied message to say I was using root@'%'

I have tried rebuilding the images, deleting the volumes. I found a github issue around the MYSQL_PWD being blank, so I have added that in for good measure, but it still doesn't play nicely.

db: 
  image: mariadb:latest
  environment:
    MYSQL_ROOT_PASSWORD: password
    MYSQL_DATABASE: dev_db
    MYSQL_USER: user
    MYSQL_PASSWORD: password
    MYSQL_PWD: password
  ports:
    - "3306:3306"
  volumes:
    - data-volume:/var/lib/mysql

Has anyone come across this before, or have any suggestions how to resolve it? It is like the root@'localhost' is somehow configured with two different credentials?

1 Answers

When you connect to mysql/mariadb inside a container, that instance does not see you as coming from 'localhost'. See below how you can test that. So if you see an error saying 'root'@'localhost' that is most definitely because you are connecting to a process running on host, not inside the container.

Run the test container

docker run -d --rm --name testmariadb -P \
  -e MYSQL_RANDOM_ROOT_PASSWORD=true \
  mariadb:latest

Check the port:

export MARIADB_PORT=$(docker inspect testmariadb --format='{{range $k, $v := .NetworkSettings.Ports}}{{with index $v 0}}{{.HostPort}}{{end}}{{end}}')

echo Container running on port ${MARIADB_PORT}

Try to connect and check the error

mysql -h127.0.0.1 -P${MARIADB_PORT} -uroot

You should see something similar to:

ERROR 1045 (28000): Access denied for user 'root'@'172.17.0.1' (using password: NO)

Cleanup

docker container rm -f testmariadb
Related