Search code examples
mysqldockerdockerfiledocker-machine

Docker container can't connect to its mysql server (ACCESS DENIED)


I created a docker container and its mysql server like so:

docker run -d\
    --name mysql-server
    --restart on-failure\
    -e MYSQL_ROOT_PASSWORD=1234\
    -e MYSQL_DATABASE=users\
    -v volume1:/var/lib/mysql\
    mysql:latest --default-authentication-plugin=mysql_native_password

but when I try to login I get an access denied error. Here's how I'm trying to log in to the mysql server:

docker exec -it spawning-pool mysql -uroot -p

This is the response I get:

Enter password: (I put 1234 here)
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

docker ps -a shows:

6909b7003519        mysql:latest        "docker-entrypoint.s…"   38 minutes ago      Up 38 
    minutes            3306/tcp, 33060/tcp    mysql-server

docker exec mysql-server env shows:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    HOSTNAME=6909b7003519
    MYSQL_ROOT_PASSWORD=1234
    MYSQL_DATABASE=users
    GOSU_VERSION=1.7
    MYSQL_MAJOR=8.0
    MYSQL_VERSION=8.0.18-1debian9
    HOME=/root

I've tried changing the mysql version but that doesn't work. Getting a random password works and I don't know why. I also can't use the random password because this is a school project and there is guidelines I have to follow. I've spent hours on this and any help is appreciated.


Solution

  • seems like you have to change the password and run the command with existing volume, so it's not going to set new password as the default behaviour of Docker MySQL image.

    You have two option

    • try to remvoe volume and run the container
    docker volume rm volume1 && docker run -it --rm -v volume1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=12345 -e MYSQL_DATABASE=users mysql:latest --default-authentication-plugin=mysql_native_password
    
    • try to run with an old password that was set during volume creation