简体   繁体   English

将 csv 文件导入 mysql docker 容器

[英]importing csv file into mysql docker container

> docker exec -it craig bash  
> mysql -u root -p --local-infile 
> use craigslist;
> 
> LOAD DATA LOCAL INFILE 'C:/Users/hank/Desktop/craigs/data.csv' 
> INTO TABLE books_mags  
> FIELDS TERMINATED BY ',' 
> LINES TERMINATED BY '\n'
> IGNORE 1 ROWS;

I have a csv file on my desktop that I am looking to enter into a MySQL table.我的桌面上有一个 csv 文件,我希望将其输入到 MySQL 表中。 But i am having a rough time figuring out the file path errors.但是我很难弄清楚文件路径错误。

ERROR 2 (HY000): File 'C:/Users/hank/Desktop/craigs/data.csv' not found (OS errno 2 - No such file or directory)错误 2 (HY000):找不到文件“C:/Users/hank/Desktop/craigs/data.csv”(操作系统 errno 2 - 没有这样的文件或目录)

I keep getting this error.我不断收到此错误。

I am using a windows 10 and a docker containter for mySQL.我正在为 mySQL 使用 windows 10 和 docker 容器。

How can i put my csv into MYSQL?如何将我的 csv 放入 MYSQL?

Directory path C:/Users/hank/Desktop/craigs/data.csv is in your host system(Windows).目录路径C:/Users/hank/Desktop/craigs/data.csv在您的主机系统 (Windows) 中。 File system of your docker will be entirely different unless you have mounted the volume.除非您安装了卷,否则 docker 的文件系统将完全不同。 Two ways to access the file inside the container访问容器内文件的两种方式

First approach第一种方法

You'll have to mount the volume when you start the container启动容器时必须挂载卷

docker run -d image_name -v "C:/Users/hank/Desktop/craigs/:/data"

This is not always recommended.这并不总是推荐的。

Second Approach第二种方法

Copy the file from your host machine to the container using docker cp使用docker cp将文件从主机复制到容器

See 'docker cp --help'.

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
    docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

It is similar to copying files to remote server using scp command.它类似于使用scp命令将文件复制到远程服务器。

1- Get you CSVs into your computer 1-让你的CSV进入你的电脑

2- Load them into your container, to manually get them there you can use: 2-将它们加载到您的容器中,要手动将它们放在那里,您可以使用:

docker cp file container:/<container-directory>

3- Enter your container and execute 3-输入您的容器并执行

> sudo docker exec -it jose_signot_mysql bash
container> mysql -uUser -pPassword
mysql> LOAD DATA LOCAL INFILE '/path/in/container/to/file' into table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS;

or execute script from outside, using an inline password (security issue)或使用内联密码从外部执行脚本(安全问题)

  sudo docker exec jose_signot_mysql bash mysql -u<User> -p<Password> <databaseName> -e "LOAD DATA LOCAL INFILE '/path/in/container/to/file' into table <tableName> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS;"

If you have an issue with secure-file-priv being set, when running this, in your instance check: this https://github.com/docker-library/mysql/issues/447#issuecomment-403393323如果您在设置secure-file-priv时遇到问题,请在运行时在您的实例中检查:this https://github.com/docker-library/mysql/issues/447#issuecomment-403393323

In order to import csv there are two solutions:为了导入 csv 有两种解决方案:

Adminer a tool for managing content for the relational databases (MySql, Postgresql, etc.)管理用于管理关系数据库内容的工具(MySql、Postgresql 等)

Here below configuration for adminer in docker-compose.yml.下面是 docker-compose.yml 中管理员的配置。 Then you can go to adminer by entering URL like "ip_addd_server:4041" then enter credentials of database ( host, password, dialect ) and log in.然后您可以通过输入 URL (如“ip_addd_server:4041”)来 go 到管理员,然后输入数据库凭据(主机、密码、方言)并登录。

 adminer:
  container_name: adminer
  image: adminer
  restart: always
  ports:
   - 4042:8080
  networks:
  - main-network
  depends_on:
   - db  

Another solution to simply attach to the container and through this command.简单地附加到容器并通过此命令的另一种解决方案。

docker exec -it db bin/bash // attach to container 

Then you can log in with the correct user and import data through CSV file.然后您可以使用正确的用户登录并通过 CSV 文件导入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM