无法获取长参数列表的mysqldump

rryofs0p  于 2022-12-03  发布在  Mysql
关注(0)|答案(1)|浏览(132)

我尝试使用以前接受的解决方案来解决我的问题,但它们不起作用:
示例“id_list”

[jlefler@server ~]$ cat id_list | tail -10
17900018
17900019
17900020
17900021
17900022
17900023
17900024
17900025
17900026
17900027[jlefler@server ~]$ cat id_list |wc -l
51624

我尝试执行的一次转储2000个ID的命令:

xargs -a id_list -d '\n' -n 2000 sh -c 'mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in ($0 $@)" >> dump.sql' x

但是,我得到了以下意外输出:

[jlefler@server ~]$ xargs -a id_list -d '\n' -n 2000 sh -c 'mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in ($0 $@)" >> dump.sql' x
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17851966
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17853966
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17855967
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17857967
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17859975
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17861978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17863978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17865978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17867978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17869978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17871978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17873979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17875979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17877979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17879979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17881979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17883979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17885979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17887979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17889979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17891979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17893979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17895979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17897980
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17899986
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17850378

基本上,最终结果应该是这样的:

mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (17900018,17900019,17900020...)" >> dump.sql

它一次取2000个ID,将它们添加到dump.sql中,然后继续下一个2000个ID,直到从文件中读取所有ID。
使用echo命令显示输出,我得到以下结果:

)17853963ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17851964
)17855964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17853964
)17857964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17855965
)17859972ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17857965
)17861975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17859973
)17863975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17861976
)17865975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17863976
)17867975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17865976
)17869975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17867976
)17871975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17869976
)17873976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17871976
)17875976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17873977
)17877976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17875977
)17879976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17877977
)17881976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17879977
)17883976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17881977
)17885976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17883977
)17887976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17885977
)17889976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17887977
)17891976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17889977
)17893976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17891977
)17895976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17893977
)17897977ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17895977
)17899983ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17897978
)17850375ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17899984
 17900027)mp -uuser -ppassword --compact --no-create-info db table_name --where=id in (17850376
)17853963ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17851964
)17855964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17853964
)17857964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17855965
)17859972ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17857965
)17861975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17859973
)17863975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17861976
)17865975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17863976
)17867975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17865976
)17869975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17867976
)17871975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17869976
)17873976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17871976
)17875976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17873977
)17877976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17875977
)17879976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17877977
)17881976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17879977
)17883976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17881977
)17885976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17883977
)17887976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17885977
)17889976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17887977
)17891976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17889977
)17893976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17891977
)17895976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17893977
)17897977ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17895977
)17899983ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17897978
)17850375ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17899984
 17900027)mp -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17850376
lpwwtiir

lpwwtiir1#

备注:

  • OP当前的语法问题似乎是由于输入文件(id_list)中的dos/windows行尾(\r\n)引起的
  • 假设OP可以删除\r字符,下一个问题可能是无效的in()列表...这也是本答案的重点...

输入文件示例:

$ cat id_list
1
2
3
4
5
6
7
8
9
10

修改的xargs调用:

$ xargs -a id_list -n 3
1 2 3
4 5 6
7 8 9
10

添加echo以查看当前代码生成的内容:

$ xargs -a id_list -n 3 bash -c 'echo mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in ($0 $@)"'
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (1 2 3)
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (4 5 6)
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (7 8 9)
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (10 )

备注:

  • OP已将问题标记为bash,因此我已将sh -c替换为bash -c
  • 从该输出中可以看出,我们需要将--where/in()子句中的空格替换为逗号
  • 看起来我们还需要添加一对转义双引号,以便在--where/in()子句周围显示它们

解决这些问题的一个想法是:

$ xargs -a id_list -n 3 bash -c 'list=$(sed "s/ /,/g;s/,$//g" <<< "$0 $@"); echo mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="\"id in ($list)\""'
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (1,2,3)"
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (4,5,6)"
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (7,8,9)"
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (10)"

相关问题