docker mysql

MySQL环境变量:https://hub.docker.com/r/mysql/mysql-server/

详见上述官方链接,个人觉得比较有用的是几个启动Docker时可以带上的数据库初始化参数:

#设置监听地址,默认只有本机可连接,可设为%使得所有客户端均可连
MYSQL_ROOT_HOST

#root密码设置
MYSQL_ROOT_PASSWORD                   #指定密码
MYSQL_ALLOW_EMPTY_PASSWORD   #空密码
MYSQL_RANDOM_ROOT_PASSWORD #随机密码,一次性,一段时间后过期
MYSQL_ONETIME_PASSWORD

#docker启动后创建初始数据库
MYSQL_DATABASE
#docker启动后创建初始用户,只对初始数据库拥有所有权限
MYSQL_USER
MYSQL_PASSWORD

使用示例:

#!/usr/bin/env bash
set -o nounset
set -o errexit
set -o pipefail

dbRootPwd=123
dbSensestudyDbName=sensestudydev
dbSensestudyUsername=sensestudy
dbSensestudyUserPwd="LKxx880)Hsens#estudyKSy8po4A"
dbSensestudyDbSqlFilePath=dump.sql

echo start mysql from docker ...

docker run  --name mysql_sensestudy -p 3307:3306 -d  \
-e MYSQL_ROOT_PASSWORD=$dbRootPwd  \
-e MYSQL_ROOT_HOST=%  \
-e MYSQL_DATABASE=$dbSensestudyDbName  \
-e MYSQL_USER=$dbSensestudyUsername  \
-e MYSQL_PASSWORD=$dbSensestudyUserPwd \
mysql/mysql-server:5.7

echo start mysql from docker done.

初始化数据库数据:

若在Docker启动时指定了初始化的数据库,此时若想同时初始化该数据库里的数据,可以用Docker命令把sql脚本复制到Docker并执行之(当然也可以在启动时挂载进去从而省去手动复制,不过挂载要求源文件所在目录的绝对路径,麻烦点),如下:

//值得注意的是这种手动执行脚本的方法要求在启动完Docker一定时间后才执行之因为MySQL没初始化完成,否则会报连不上的错误。...在这上面费了好长时间
docker cp $dbSensestudyDbSqlFilePath $dockerMySQLInstanceName:/$dbSensestudyDbSqlFilePath
docker exec   $dockerMySQLInstanceName /bin/sh -c "  mysql  -uroot -p$dbRootPwd   $dbSensestudyDbName  < /$dbSensestudyDbSqlFilePath "

更简单的做法是把sql脚本所在目录挂载到Docker的 /docker-entrypoint-initdb.d 目录下,容器创建时会自动执行该目录下的脚本。

it will execute files with extensions .sh.sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. 

dump mysql through docker:

 $ docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql 

 

完整脚本:

  1 #!/bin/bash
  2 
  3 curCMD=$1
  4 
  5 # set -x
  6 set -o nounset
  7 set -o errexit
  8 set -o pipefail
  9 
 10 dockerMySQLInstanceName=mysql_sensestudy
 11 dbPort=3307
 12 dbRootPwd=123
 13 dbSensestudyDbName=sensestudydev
 14 dbSensestudyUsername=sensestudy
 15 dbSensestudyUserPwd="xxx"
 16 dbSensestudyDbInitSqlFilePath=init.sql
 17 
 18 
 19 #docker service is required
 20 #echo check if docker service has started ...
 21 if [ ` systemctl is-active docker` != "active" ]; then ` systemctl start docker` ; fi
 22 #echo docker service started
 23 
 24 
 25 
 26 #创建容器
 27 function createMySQL()
 28 {
 29     echo create mysql from docker ...
 30 
 31     docker run  --name $dockerMySQLInstanceName -p $dbPort:3306 -d  \
 32     -e MYSQL_ROOT_PASSWORD=$dbRootPwd  \
 33     -e MYSQL_ROOT_HOST=%  \
 34     -e MYSQL_DATABASE=$dbSensestudyDbName  \
 35     -e MYSQL_USER=$dbSensestudyUsername  \
 36     -e MYSQL_PASSWORD=$dbSensestudyUserPwd  \
 37     -v  $PWD/mysql_data:/var/lib/mysql  \
 38     mysql/mysql-server:5.7 --character-set-server=utf8
 39 
 40     echo create mysql from docker done.
 41 } # -v $PWD/mysql_script:/docker-entrypoint-initdb.d
 42 
 43 
 44 #初始化Docker里MySQL数据库数据,也可以把sql脚本所在目录的绝对路径挂载到/docker-entrypoint-initdb.d
 45 function initMySQLData()
 46 {
 47     echo init db data ...
 48     set -x
 49 
 50     docker cp $dbSensestudyDbInitSqlFilePath $dockerMySQLInstanceName:/$dbSensestudyDbInitSqlFilePath
 51     docker exec   $dockerMySQLInstanceName \
 52         /bin/sh -c "  mysql  -uroot -p$dbRootPwd   $dbSensestudyDbName  < /$dbSensestudyDbInitSqlFilePath "
 53    set +x 
 54 
 55     echo init db data done.
 56 }
 57 
 58 
 59 
 60 #启动MySQL容器,若不存在则创建容器并从sql脚本初始化数据
 61 function start()
 62 {
 63     echo start $dockerMySQLInstanceName on port $dbPort ...
 64 
 65     if [   "`docker ps -a|grep "$dockerMySQLInstanceName" `" = "" ]
 66     then
 67         createMySQL
 68         sleep 15
 69         initMySQLData
 70     else
 71         docker container start $dockerMySQLInstanceName
 72     fi
 73 
 74      echo start $dockerMySQLInstanceName done.
 75 }
 76 
 77 
 78 # 关闭容器
 79 function stop()
 80 {
 81     echo stop  $dockerMySQLInstanceName ...
 82     if [ "`docker container ls| grep ${dockerMySQLInstanceName}`" != "" ]
 83     then
 84         docker container stop $dockerMySQLInstanceName
 85     fi
 86     echo stop  $dockerMySQLInstanceName done.
 87 }
 88 
 89 
 90 # 删除容器
 91 function remove()
 92 {
 93     # first stop
 94     stop
 95 
 96     # then remove
 97     echo delete  $dockerMySQLInstanceName ...
 98     if [ "`docker ps -a| grep ${dockerMySQLInstanceName}`" != "" ]
 99     then
100         docker container rm $dockerMySQLInstanceName
101     fi
102     echo delete  $dockerMySQLInstanceName done.
103 }
104 
105 function restart()
106 {
107     stop
108     sleep 2
109     start
110 }
111 
112 function status()
113 {
114     if [ "`docker container ls| grep ${dockerMySQLInstanceName}`" = "" ]
115     then
116         echo mysql container is not running.
117     else
118         echo `docker container ls| grep ${dockerMySQLInstanceName}`
119     fi
120 }
121 
122 
123 
124 if [ "$curCMD" = "start" ]
125 then
126   start
127 
128 elif [ "$curCMD" = "stop" ]
129 then
130   stop
131 
132 elif [ "$curCMD" = "restart" ]
133 then
134   restart
135 
136 elif [ "$curCMD" = "status" ]
137 then
138   status
139 
140 elif [ "$curCMD" = "remove" ]
141 then
142   remove
143 
144 
145 else
146   echo "error command: please use option: start|stop|restart|status|remove"
147 fi
View Code

 

 

更全面的内容:https://hub.docker.com/_/mysql/

 

参考资料:

https://hub.docker.com/r/mysql/mysql-server/:环境变量

https://hub.docker.com/_/mysql/:构建、连接、环境变量、初始化、数据存储等

 

posted @ 2018-08-01 11:37  March On  阅读(790)  评论(0编辑  收藏  举报
top last
Welcome user from
(since 2020.6.1)