Batch clearing all tables in the mysql database sometimes we want to clear a database without deleting it. For example, if the local database is synchronized with the latest database file backed up by the server, the local database is cleared first.
Of course it cannot be useddrop database
Because it will be deleted together with the database. In fact, this is acceptable, but it is necessary to re-create an empty database with the same name. Or usedrop table
Command to delete a table from a table? Er... In that case, it's not just the style of our geek programmers.
Here we will introduceHigh-endMethod, you have guessed it, that is, using shell script:
#!/bin/bash
MUSER="$1"
MPASS="$2"
MDB="$3"
# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
if[ $# -ne 3 ]
then
echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name}"
echo "Drops all tables from a MySQL"
exit1
fi
TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -e 'show tables'| $AWK '{ print $1}'| $GREP -v '^Tables')
for t in $TABLES
do
echo "Deleting $t table from $MDB database..."
$MYSQL -u $MUSER -p$MPASS $MDB -e "drop table $t"
done
This script is from the network. I have tried it myself and it is easy to use.
Usage
Save the preceding script as a sh file (For example, drop. table. sh), Execute./drop.table.sh USERNAME PWD DBNAME
(Replace USERNAME, PWD, and DBNAME with your mysql User name, password, and database name..
Note:: In this way, your password is displayed in history and may be exploited by hackers.