Docker Compose mysql import .sql
我在用Docker Compose导入.sql转储文件时遇到问题。我已经跟踪了这些文档,它们显然会从docker entrypoint initdb.d加载.sql文件。但是,当我运行
我试过用
我的compose文件所在的根目录database/db dump/中有dump.sql。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | frontend: image: myimage ports: -"80:80" links: - mysql mysql: image: mysql ports: -"3306:3306" environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_USER: dbuser MYSQL_PASSWORD: userpass MYSQL_DATABASE: myimage_db volumes: - ./database/db-dump:/docker-entrypoint-initdb.d |
在多次尝试卷设置后,我发现了一个解决方法
我创建了另一个基于mysql的图片,在dockerfile中包含以下内容
然后从撰写中删除卷并运行新映像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | frontend: image: myimage ports: -"80:80" links: - mysql mysql: image: mymysql ports: -"3306:3306" environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_USER: dbuser MYSQL_PASSWORD: userpass MYSQL_DATABASE: myimage_db |
这样,转储总是被复制并在启动时运行。
这出现在docker mysql图片的文档页面:https://hub.docker.com//mysql/
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 theMYSQL_DATABASE variable.
这对我很有用,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
mysql dump必须是目录。将导入目录中的所有.sql。
mysql database dump schema.sql驻留在/mysql dump/schema.sql目录中,它在初始化过程中创建表。
docker-compose.yml:
1 2 3 4 5 6 7 8 9 10 | mysql: image: mysql:5.7 command: mysqld --user=root volumes: - ./mysql-dump:/docker-entrypoint-initdb.d environment: MYSQL_DATABASE: ${MYSQL_DATABASE} MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD} MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} |