问题描述
我只是在学习docker的基础知识,但是一直坚持从本地系统导入SQl文件.我在Windows 10上,并已允许我的Docker容器访问我的共享驱动器.我在D上有一个SQL文件,我想导入从docker hub获得的Maria DB的基本映像.
I am just learning the basics of docker, but have come stuck on importing an SQl file from the local system. I am on windows 10 and have allowed my docker containers to access my shared drives. I have an SQL file located on D i would like to import to the base image of Maria DB i got from docker hub.
我找到了一个在映像中安装该sql文件的命令,并尝试直接从容器sql命令提示符内部导入该映像,但出现无法打开文件的错误.
I have found a command to install that sql file on my image and tried to directly import the image from inside the container sql command prompt, but get a failed to open file error.
下面是我尝试过的两种方法,但是我在哪里存储我的sql dump以及如何导入它呢?
Below are the two methods i have tried, but where do i store my sql dump and how do i import it?
方法1 通过mysql命令行
Method 1 tried via mysql command line
winpty docker run -it --link *some-mariadb*:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
然后
use *database-created* // previously created
然后
source d:/sql_dump_file.sql
方法2
docker exec -i my-new-database mysql -uroot -pnew-db-password --force < d:/sql_dump_file.sql
更新2016年5月12日
因此,在一个令人失望的周末玩耍之后.我目前将驱动器更改为C:驱动器,因为似乎有些未知问题我无法通过使D:驱动器正常工作来进行调试.
So after a disappointing weekend of playing around. I currently change the drive to C: drive as there seemed to be some unknown issue i can't debug with getting D: drive to work.
然后,我找到了inspect命令,以查看将哪些卷安装到容器上.我有以下内容,但是我无法导入SQL,因为它说文件不存在,但是它清楚地表明在docker中已经存在并挂载了该文件,该SQL文件位于map_me文件夹内.我在C的主目录中创建了一个名为map_me的文件夹:
Then i found the inspect command to see what volumes are mounted to a container. I have the following, but i can't import to SQL as it says file does not exist, but it clearly says in docker that it is there and mounted, the SQL file is inside the map_me folder. I created a folder called map_me in the main directory of C:
docker inspect dbcontainer
"Mounts":
[
{
"Source": "/map_me",
"Destination": "/docker-entrypoint-initdb.d/sl_dump_file.sql",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
推荐答案
因此,在进行了一些调整和更好的理解之后,我在测试后得出了结论,即docker-compose是可行的方法.因此,第一个文件夹包含一个进行配置的my.cnf文件,然后使用@Farhad标识的另一个文件夹来引入.sql文件.
So after some tweaking and better understanding, i came to the conclusion after testing, that docker-compose is the way to go. So the first folder contains a my.cnf file that does the configuration, then the other folder which @Farhad identified is used to intilize the .sql file.
version: "2"
services:
mariadb_a:
image: mariadb:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=111111
volumes:
- c:/some_folder:/etc/mysql/conf.d
- c:/some_other_folder:/docker-entrypoint-initdb.d
这篇关于使用mariaDB将SQL转储文件安装到Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!