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

# mysql -uroot -p123456

# show databases;

 

 

参考:

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

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

你可能感兴趣的:(021.Docker mysql启动时执行初始化sql)