2

I have a folder structure with multiple SQL files each.

I would like to know how to run all source files with MySQL massively from the shell. I use Ubuntu.

Any suggestions are welcome. Thanks.

sql
    admin
        adm_a.sql
        adm_b.sql
        adm_c.sql
    user
        usr_new.sql
        usr_upd.sql
    operator
        opr_ctrl.sql
        opr_migrate.sql

3 Answers 3

1

You need to do your research to get the exact commands for your shell type, but on a high-level this what you need to do in your script:

connect to mysql (with username/password)
for subfolder in `sql`:
    sql_files = list(*.sql)
    for sql_file in sql_files:
        pipe sql_file to mysql db
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your contribution.
1

Thank you all for your responses, I could finally get:

for i in `find . -name "*.sql"`; 
do mysql -e "source `expr ${i:2}`" -u MyUser -pMyPassword MyDataBase; 
done

I hope this will serve somebody.

Regards.

Comments

0

Not exact commands but something like this will work

for directory in admin user operator
do
  for sql_file in directory/*.sql
  do
     mysql db_name < sql_file.sql
  done
done

1 Comment

Thank you very much for your contribution.

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.