Skip to content

Instantly share code, notes, and snippets.

@acataluddi
Forked from cweinberger/mysql-drop-all-tables.sh
Created September 18, 2019 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acataluddi/cf381a90a81e144963a35fa426c0f3b0 to your computer and use it in GitHub Desktop.
Save acataluddi/cf381a90a81e144963a35fa426c0f3b0 to your computer and use it in GitHub Desktop.
drops all tables of a specific db
#!/bin/bash
#usage: mysql-drop-all-tables -d database -u dbuser -p dbpass
TEMP_FILE_PATH='./drop_all_tables.sql'
while getopts d:u:p: option
do
case "${option}"
in
d) DBNAME=${OPTARG};;
u) DBUSER=${OPTARG};;
p) DBPASS=${OPTARG};;
esac
done
echo "SET FOREIGN_KEY_CHECKS = 0;" > $TEMP_FILE_PATH
( mysqldump --add-drop-table --no-data -u$DBUSER -p$DBPASS $DBNAME | grep 'DROP TABLE' ) >> $TEMP_FILE_PATH
echo "SET FOREIGN_KEY_CHECKS = 1;" >> $TEMP_FILE_PATH
mysql -u$DBUSER -p$DBPASS $DBNAME < $TEMP_FILE_PATH
rm -f $TEMP_FILE_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment