makstaks
makstaks

Reputation: 2111

Run multiple sql files in mysql batch

to run a single file you can run in mysql

.\ filename

or you outside of mysql you can run

mysql < filename

I have a directory of sql files so I'm trying to run them all at once by using a wildcard

*.sql

but it doesn't work.

Any ideas?

Upvotes: 29

Views: 47062

Answers (6)

Erlangga Dewa
Erlangga Dewa

Reputation: 1

cat *.sql | mysql -u root -p "database name"

you can change root with your username and if you have password you can add your password behind -p

i hope this help

Upvotes: 0

mpschmitt
mpschmitt

Reputation: 111

For Windows:

FOR %%A IN ("*.sql") DO "D:\mysql\Install\MySQL Server 5.5\bin\mysql" --user=scooby --password=pwd123 databasename < %%A >output.tab

Upvotes: 11

Sr.PEDRO
Sr.PEDRO

Reputation: 2067

bash:

for sql_file in `ls /path/to/directory/*.sql`; do mysql -uUSER -pPASSWORD DATABASE < $sql_file ; done

Upvotes: 7

Anuchit
Anuchit

Reputation: 121

for %S in (*.sql) do mysql -u user_name database_name < %S

or

mysql -u user_name -p password database_name < file.sql

Upvotes: 12

Etienne Dechamps
Etienne Dechamps

Reputation: 25311

Assuming you're using bash:

cat *.sql | mysql

Upvotes: 66

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798516

bash:

mysql < <(cat *.sql)

Upvotes: 7

Related Questions