简体   繁体   中英

MySQL with docker Access denied

I need to use MySQL on docker. The steps I've followed:

  1. Installed docker
  2. Pulled MySQL docker image
  3. Ran docker container for MySQL
  4. Installed MySQL client.

But When I run this

sudo docker exec -it mysql8 MySQL -uroot -p

It asks for a password, I enter the password and it gives me this:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password : Yes)

How do I fix this and what am I missing? I followed the tutorial here: https://www.techrepublic.com/article/how-to-deploy-and-use-a-mysql-docker-container/

In order to create a docker container for mysql, you can use this command given below.

docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=user -e MYSQL_PASSWORD=password -d -p 3308:3306 -v $HOME/docker/volumes/mysql:/var/lib/mysql mysql

You can also set more environment variables for mysql in this given command.

In order to access mysql inside this container you have 2 easy ways:

  1. Use docker exec -it container_name. Then write mysql -u root -p inside the container or along with the command given is your wish.

  2. Use mysql client and access from local itself.

The article you have followed is outed and also running container and the change password is not the proper way when working in the container. With Offical image, you can set using environment variable.

use below command and further details you can find on Mysql Docker hub .

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=mypass -d mysql

then try to connect

docker exec -it some-mysql bash -c "mysql -u root -pmypass"

Found the answer. Have to run

docker logs <container_name>

which gives a generated password using which we have to use when prompted for password.

Afterward, we can change the password by:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM