Import multiple SQL file via script
I would like to share a script I found quite useful, it’s very basic bash scripting but it still can help the unexperienced ones.
- First create a
- Add the following content to the script:
#!/bin/bash db=$1 user=$2 passwd=$3 if [ "$db" = "" ]; then echo "Usage: $0 db_name user password" exit 1 fi clear for sql_file in *.sql; do echo "Importing $sql_file"; mysql --user=$user --password=$passwd $db < $sql_file; done
- Run the script with the following terminal command:
bash import.sh yourTargetDatabaseName yourMySQLUser yourMySQLPassword
The script will simply run sequentially all the imports you dumped beforehand.
PS: If you get some Foreign Key errors, you can disable them changing the “for” cycle mysql command with the following:
echo "SET FOREIGN_KEY_CHECKS=0" | mysql --user=$user --password=$passwd $db < $sql_file
Have a good import!