如何将启动脚本添加到mysql docker容器? [英] How to add a startup script to a mysql docker container?
问题描述
我用mysql启动了一个docker容器.
实际上,我想创建一个新用户和一个新表-并且必须在MySQL Workbench中进行.
这是我的docker run命令:
docker run -p 3306:3306 --name mysql-server -v ~/Development/web/myproject/docker/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
我的问题是:
如何添加创建用户和表的sql启动脚本(仅用于容器的首次启动)?
我必须执行哪些步骤?
有人可以帮我吗?
非常感谢!
I started a docker container with mysql.
Actually i want to create a new user and a new table - and i have to do it in the MySQL Workbench.
This ist my docker run command:
docker run -p 3306:3306 --name mysql-server -v ~/Development/web/myproject/docker/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
My Question is:
How can i add a sql startup script (only for the first start of the container) which creates my user and my table?
Which steps do i have to do?
Could some one help me here?
Thanks a lot!
推荐答案
您需要通过MYSQL_USER
,MYSQL_PASSWORD
env vars创建用户,并使用卷/docker-entrypoint-initdb.d
将启动脚本(.sh
, .sql
,.sql.gz
)
You need to create user via MYSQL_USER
, MYSQL_PASSWORD
env vars and use volume /docker-entrypoint-initdb.d
to map directory with your startup scripts (.sh
, .sql
, .sql.gz
)
docker run -p 3306:3306 --name mysql-server \
-v ~/Development/web/myproject/docker/mysql:/var/lib/mysql \
-v ~/Development/web/myproject/docker/yourstartupscripts:/docker-entrypoint-initdb.d \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_USER=youruser \
-e MYSQL_PASSWORD=youruserpassword \
-d mysql:latest
来自以下位置的解释: https://hub.docker.com/_/mysql/
Explanation from: https://hub.docker.com/_/mysql/
MYSQL_USER,MYSQL_PASSWORD
这些变量是可选的,与创建新用户和设置该用户的密码一起使用.将为该用户授予MYSQL_DATABASE
变量指定的数据库的超级用户权限(请参见上文).这两个变量都是创建用户所必需的.
MYSQL_USER, MYSQL_PASSWORD
These variables are optional, used in conjunction to create a new user and to set that user's password. This user will be granted superuser permissions (see above) for the database specified by the MYSQL_DATABASE
variable. Both variables are required for a user to be created.
初始化一个新实例
首次启动容器时,将创建一个具有指定名称的新数据库,并使用提供的配置变量对其进行初始化.此外,它将执行在/docker-entrypoint-initdb.d
中找到的扩展名为.sh
,.sql
和.sql.gz
的文件.文件将按字母顺序执行.您可以通过自定义映像提供贡献的数据.默认情况下,SQL文件将导入到MYSQL_DATABASE
变量指定的数据库中.
Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, 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. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE
variable.
这篇关于如何将启动脚本添加到mysql docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!