Linux bash MySQL 加载 infile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20410904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:54:18  来源:igfitidea点击:

Linux bash MySQL load infile

mysqllinuxbashterminal

提问by Spartacus38

I'm trying to simplify a load data local infile, by putting it into a .sh file, and bash to run it.

我试图简化加载数据本地 infile,将其放入 .sh 文件中,然后 bash 运行它。

Here is my count_portal.sh file

这是我的 count_portal.sh 文件

mysql -h host -u root -p password 
load data local infile
"/workplace/user/dump/count_portal.txt" 
replace into table test.icqa_count_portal 
fields terminated by '\t' lines terminated by '\n' ignore 1 lines;

And here is my bash script

这是我的 bash 脚本

bash /home/user/Desktop/count_portal.shI get an output that doesn't do what it is designed to do. When I simply make the count_portal.sh contain mysql -h hostit longs in when running the script.

bash/home/user/Desktop/count_portal.sh我得到的输出不符合它的设计目的。当我简单地让 count_portal.shmysql -h host在运行脚本时包含它时。

采纳答案by Spartacus38

I figured it out. Here is my file.

我想到了。这是我的文件。

#!/bin/bash
/usr/bin/mysql --host=host --user=root --password=password --database=test<<EOFMYSQL
load data local infile '/workplace/user/ETLdump/count_portal.txt' replace INTO TABLE count_portal fields terminated by '\t' LINES
TERMINATED BY '\n' ignore 1 lines;
EOFMYSQL

Works flawlessly!

完美运行!

回答by vishu9219

mysql -u<username> -p<password> -h<hostname> <db_name> --local_infile=1 -e "use <db_name>" -e"LOAD DATA LOCAL INFILE '<path/file_name>' 
IGNORE INTO TABLE <table_name>
FIELDS TERMINATED BY '\t'
OPTIONALLY ENCLOSED BY '\"'"

回答by PasteBT

missed -e ?

错过了 -e ?

mysql -h host -u root -ppassword dbname -e "load data local infile '/workplace/user/dump/count_portal.txt' replace into table test.icqa_count_portal fields terminated by '\t' lines terminated by '\n' ignore 1 lines;"

回答by RustProof Labs

Try removing the space between the user and password switches. So it would be something like:

尝试删除用户和密码开关之间的空格。所以它会是这样的:

mysql -h host -uroot -ppassword etc....

The spaces seem to cause problems for me when doing similar things inside bash scripts.

在 bash 脚本中做类似的事情时,这些空格似乎给我带来了问题。

回答by Jason Heo

I agree with @PasteBT, his answer should work well. I think there are escape problem or shell variable is empty.

我同意@PasteBT,他的回答应该很有效。我认为存在转义问题或 shell 变量为空。

Have you tried this?:

你试过这个吗?:

echo "LOAD DATA LOCAL INFILE '/workplace/user/dump/count_portal.txt' REPLACE INTO TABLE test.icqa_count_portal FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' IGNORE 1 LINES;" | mysql -h host -u root -ppassword

and you original shell script is wrong. could you post again what did you do?

你原来的shell脚本是错误的。你能再发一次你做了什么吗?