docker 导入mysql脚本,如何将启动脚本添加到mysql docker容器?

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!

解决方案

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

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值