Batch clear all tables in the mysql database _ MySQL

Source: Internet
Author: User
Tags mdb database
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 databaseBecause 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 tableCommand 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:

  1. #!/bin/bash
  2. MUSER="$1"
  3. MPASS="$2"
  4. MDB="$3"
  5. # Detect paths
  6. MYSQL=$(which mysql)
  7. AWK=$(which awk)
  8. GREP=$(which grep)
  9. if[ $# -ne 3 ]
  10. then
  11. echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name}"
  12. echo "Drops all tables from a MySQL"
  13. exit1
  14. fi
  15. TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -e 'show tables'| $AWK '{ print $1}'| $GREP -v '^Tables')
  16. for t in $TABLES
  17. do
  18. echo "Deleting $t table from $MDB database..."
  19. $MYSQL -u $MUSER -p$MPASS $MDB -e "drop table $t"
  20. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.