4

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? Jan 15, 2014 at 15:07
  • in the conf file in /etc? or otherwise put it behind the mysql command.
    – Rinzwind
    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" Jan 15, 2014 at 15:37
  • the database is in the import file.
    – Rinzwind
    Jan 15, 2014 at 15:37
  • hm, but how to be if name is other? Jan 15, 2014 at 15:42
4
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
    Feb 19, 2019 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.