Friday, November 14, 2014

Drop All Databases in MySQL

The Following command drops all databases in the mysql dbms except mysql, information_schema,test and OLD db’s.
The command is pretty handy when one needs to drop all the databases in one go:

# mysql -uroot -p  -e "show databases" | grep -v Database | grep -v mysql| grep -v information_schema| grep -v test | grep -v OLD |gawk '{print "drop database " $1 ";select sleep(0.1);"}' | mysql -uroot -ppassword

What this does is
  1. connect to a mysql dbms server and execute the command for showing all databases
  2. Omit lines that match “Database” while printing.
  3. Omit lines with mysql,infomation_schema and test.
  4. use gawk to print out the words “drop database” followed by the daabase name (which is in $1) and then a semicolon. Call sleep command.
  5. pipe all of  above back to the mysql dbms to drop all those databases
Also,

mysql -uroot -pxxxxx  -e "show databases" | grep -v Database | grep -v mysql | grep -v information_schema| grep -v test | grep -v OLD | gawk '{print "drop database " $1";select sleep(0.1);"}' > droppeddatabases.sql

0 comments: