Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql : How to run batch of sql scripts from a folder

Tags:

sql

mysql

I have a folder with o lot of sql scripts. I want to run all of them without specifying names of them. Just specify a folder name. Is it possible?

like image 926
Constantine Gladky Avatar asked Dec 27 '13 09:12

Constantine Gladky


2 Answers

You can not do that natively, but here's simple bash command:

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

here USER, PASSWORD and DATABASE are the corresponding credentials and /path/to/directory is full path to folder that contains your files.

If you want to filter, for example, only sql files, then:

for sql_file in `ls /path/to/directory/*.sql`; do mysql -uUSER -pPASSWORD DATABASE < $sql_file ; done
like image 146
Alma Do Avatar answered Sep 20 '22 18:09

Alma Do


That was what worked for me: 1. Created a shell script in the folder of my scripts

for f in *.sql
  do
   echo "Processing $f file..."

   mysql -u user "-pPASSWORD" -h HOST DATABASE < $f

 done
like image 35
eduardocurva Avatar answered Sep 17 '22 18:09

eduardocurva