29

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?

6 Answers 6

66

Assuming you're using bash:

cat *.sql | mysql

Sign up to request clarification or add additional context in comments.

4 Comments

Awsome...worked: ) i used: cat data*.sql | mysql -u root -p dbName
This will only work if all files end with semicolons after commands. Otherwise you'll get a SQL syntax error.
@StanJames Yep, I'm having the same problem! Is there any way to fix that you can think of? I suppose the other solution is to use a bash loop
To specify a location: cat *.sql | mysql --host=yourDatabaseIP --user=xxxxx --password=xxxxx -p databaseName.
11
for %S in (*.sql) do mysql -u user_name database_name < %S

or

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

1 Comment

This works for me. The current version of mysql does not allow spaces between -u and username. The same applies for password as well.
11

For Windows:

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

1 Comment

When running this in cmd (not powershell) I get an error %%A was unexpected at this time.
7

bash:

mysql < <(cat *.sql)

2 Comments

i think someone marked this down because running this command gets an error "syntax error near unexpected token `('"
That's because process substitution needs bash. Not sh, not dash, but bash proper. tldp.org/LDP/abs/html/process-sub.html
6

bash:

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

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.