简体   繁体   中英

Docker mysql container not creating user with password when specified with database

I have a usecase where i need to run mysql on a container and link it to another container. I also have my db files data in my host location which is mounted as a volume on to the database container... The condition is to run the container not as root but as a different user with all privileges. The db is there is in the mounted volume.

I ran the following command:

docker run -d -v ~/testdata:/var/lib/mysql -e MYSQL_DATABASE=Testdata_DB -e MYSQL_USER=testdata -e MYSQL_PASSWORD=mypasswordhere -p 3306:3306 --name=testdata_db mysql

The above command will start the container but i am not able to see the user with the password when i bash into the running container. Only the mysql is running

docker exec -it testdata_db bash

Kindly let me know where i am going wrong. I followed the documentation under the docker official repo link.

I solved it by creating a init.sql with the required sql commands to create user , tables which it loaded from my host to the container under docker-entrypoint-initdb.d/ and set the env varibles as required. This made mysql instance load as a fresh instance and loaded all the required tables and data. The final command is:

docker run --name testdata_db -p 3306:3306 -e "MYSQL_ROOT_PASSWORD= " -e "MYSQL_USER=test" -e "MYSQL_PASSWORD=mypass" -e "MYSQL_DATABASE=mysql" -v ~/mysql/db/:/var/lib/mysql/ -v ~/mysql/init/:/docker-entrypoint-initdb.d/ -d mysql

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