1.拉取Mysql鏡像 docker pull mysql:5.7 2.檢查mysql鏡像 docker inspect mysql:5.7 "Entrypoint": [ "docker-entrypoint.sh" ], 3.本地創建mysql外掛的目錄 ##掛載到容器內/docker-entr ...
1.拉取Mysql鏡像
# docker pull mysql:5.7
2.檢查mysql鏡像
# docker inspect mysql:5.7
##
"Entrypoint": [
"docker-entrypoint.sh"
],
3.本地創建mysql外掛的目錄
##掛載到容器內/docker-entrypoint-initdb.d;MySQL啟動時將執行 01_create_database.sql
# pwd
# /root/mysql-5.7/init-data
# ls
# 01_create_database.sql
##content
create database test_database DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
grant all privileges on `test_database`.* to 'test_user'@'%' identified by '123456';
flush privileges;
##掛載到容器內 /var/lib/mysql
# cd /root/mysql-5.7
# mkdir mysql
# cd mysql
# pwd
# /root/mysql-5.7/mysql
4.啟動mysql
# docker run -p 33336:3306 \
-v /root/mysql-5.7/mysql:/var/lib/mysql \
-v /root/mysql-5.7/init-data:/docker-entrypoint-initdb.d \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql_5.7 \
-d mysql/mysql:5.7
5.進入容器,登錄mysql,檢查發現已創建庫 test_database
# docker exec -ti <containerID> sh
# mysql -uroot -p123456
# show databases;
參考:
https://blog.csdn.net/10km/article/details/79046864
https://www.jianshu.com/p/12fc253fa37d