I am trying to set up a simple container with MySQL docker image. I am able to run the container and set it up using command prompt as below
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD="myrootpassoword" -d mysql
But when I try to do it using docker-compose, the container gets up but I am not able to connect to mysql db using mysql -p
. My password is not working, it keeps getting access denied and I am not able to work with the container. Below is my compose file. Am I missing anything?
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3'
# Define services
services:
# Mysql Service
db:
image: "mysql" # Use a public mysql image to build the mysql service
volumes:
- dbvol:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: "myrootpassowrd"
MYSQL_DATABASE: "mydb"
MYSQL_USER: "myadmin"
MYSQL_PASSWORD: "mypassword"
ports:
- "3306:3306" # Forward the exposed port 3306 on the container to port 3306 on the host machine
container_name: test-mysql
volumes:
dbvol:
In combination with your docker-compose file you should be able to connect to your mysql instance via
mysql -h localhost -u myadmin -pmypassword
note the missing space for the password!
Finally I found the issue causing this trouble. My password contained $ symbol which is used to get variable in docker, so the next character/part was considered as variable and as value was not set, it was taken as blank without actually throwing any error.
To escape it I had to use two dollar signs $$
So I modified the code from
MYSQL_ROOT_PASSWORD: "myroot$passowrd"
to
MYSQL_ROOT_PASSWORD: "myroot$$passowrd"
and it worked properly.
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.