Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

How can I import multiple .sql files into MySQL?

Writer Andrew Mclaughlin

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

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
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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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