Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Multiple .sql dump files into mysql database from shell

Tags:

linux

shell

mysql

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

e.g.

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

There are quite a lot of them actually.

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

I'm running on a Linux Debian machine.

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.

EDIT

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 
like image 771
Derek Organ Avatar asked Jan 16 '11 21:01

Derek Organ


People also ask

How import SQL dump MySQL?

From the normal command line, you can import the dump file with the following command: mysql -u username -p new_database < data-dump. sql.


2 Answers

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 

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.

like image 104
D.Shawley Avatar answered Sep 22 '22 11:09

D.Shawley


One-liner to read in all .sql files and imports them:

for SQL in *.sql; do DB=${SQL/\.sql/}; echo importing $DB; mysql $DB < $SQL; done 

The only trick is the bash substring replacement to strip out the .sql to get the database name.

like image 42
Ronnie Avatar answered Sep 19 '22 11:09

Ronnie