使用 mariaDB 将 sql 转储文件安装到 docker 容器

2022-01-15 00:00:00 docker mariadb mysql

我只是在学习 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 转储以及如何导入它?

Below are the two methods i have tried, but where do i store my sql dump and how do i import it?

方法一尝试通过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

方法二

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.

然后我找到了检查命令来查看哪些卷已安装到容器.我有以下内容,但我无法导入到 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_fil‌​e.sql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
           ]

推荐答案

所以经过一些调整和更好的理解,我经过测试得出结论,docker-compose 是要走的路.所以第一个文件夹包含一个执行配置的 my.cnf 文件,然后@Farhad 标识的另一个文件夹用于 intilize .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

相关文章