021.Docker mysql啓動時執行初始化sql

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.sqlmysql

# 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/mysqlsql

 

# 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;

 

 

參考:docker

https://blog.csdn.net/10km/article/details/79046864ide

https://www.jianshu.com/p/12fc253fa37dspa

相關文章
相關標籤/搜索