5

In my folder, I have a lot of *.sql files... How via one command I can import them?

For example; is something like this possible?

mysql -u root -p db_development < *.sql

If not, how can I import every .sql file into MySQL?

2 Answers 2

7

This is the best one-line I know:

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

or with password:

for SQL in *.sql; do DB=${SQL/\.sql/}; mysql -u user --password=11111 $DB < $SQL; done"

Warning: the password is plain text so visible when using 'ps'.

6
  • hm, and where to put here login, password and db?
    – pavel
    Commented Jan 15, 2014 at 15:07
  • in the conf file in /etc? or otherwise put it behind the mysql command.
    – Rinzwind
    Commented Jan 15, 2014 at 15:33
  • what if i have multiple db names? will this be ok? for SQL in *.sql; do DB=${SQL/\db_development/}; mysql -u user --password=11111 $DB < $SQL; done"
    – pavel
    Commented Jan 15, 2014 at 15:37
  • the database is in the import file.
    – Rinzwind
    Commented Jan 15, 2014 at 15:37
  • hm, but how to be if name is other?
    – pavel
    Commented Jan 15, 2014 at 15:42
6
cat *.sql | mysql -u root -p db_development

This will ask one time for the password.

If the statements belong to different databases, insert a USE databasename; at the top of all .sql files.

1
  • how to add all sql files in sequence with respect to their names ?
    – ZaidRehman
    Commented Feb 19, 2019 at 11:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .