从shell导入多个.sql转储文件到mysql数据库 [英] Import Multiple .sql dump files into mysql database from shell

查看:138
本文介绍了从shell导入多个.sql转储文件到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,其中包含一堆.sql文件,mysql将这些文件转储到服务器上的每个数据库中.

I have a directory with a bunch of .sql files that mysql dumps of each database on my server.

例如

database1-2011-01-15.sql
database2-2011-01-15.sql
...

实际上有很多.

我需要创建一个shell脚本或单行,可能会导入每个数据库.

I need to create a shell script or single line probably that will import each database.

我正在Linux Debian机器上运行.

I'm running on a Linux Debian machine.

我认为有一种方法可以将ls的结果传递给一些find命令或其他内容.

I thinking there is some way to pipe in the results of a ls into some find command or something..

非常感谢您的帮助和教育.

any help and education is much appreciated.

编辑

所以最终我想一次自动将一个文件导入数据库.

So ultimately I want to automatically import one file at a time into the database.

例如如果我手动做一个,那就是:

E.g. if I did it manually on one it would be:

mysql -u root -ppassword < database1-2011-01-15.sql

推荐答案

cat *.sql | mysql?您是否需要按任何特定顺序使用它们?

cat *.sql | mysql? Do you need them in any specific order?

如果您有太多方法无法处理,请尝试执行以下操作:

If you have too many to handle this way, then try something like:

find . -name '*.sql' | awk '{ print "source",$0 }' | mysql --batch

这也解决了通过管道传递脚本输入时遇到的一些问题,尽管您在Linux下的管道处理上应该没有任何问题.这种方法的好处是,mysql实用程序会读取每个文件,而不是从stdin中读取文件.

This also gets around some problems with passing script input through a pipeline though you shouldn't have any problems with pipeline processing under Linux. The nice thing about this approach is that the mysql utility reads in each file instead of having it read from stdin.

这篇关于从shell导入多个.sql转储文件到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆