Removing multiple databases with mysql command

I have many databases with different names. I want to delete several databases, is there any command since all db names are different.

For example: mysql db, Test db, live db.

+4
source share
3 answers

Unfortunately, there is nothing like this unless you create your own function.

+3
source

If you create a shell script, this should delete all the databases. You will need to edit it to suit your needs.

DBUSER='user' DBPASS='password' SQLFILE='/path/to/file/databases.sql' echo '* Dropping ALL databases' DBS="$(mysql -u$DBUSER -p$DBPASS -Bse 'show databases' | grep -v Database | grep -v database | grep -v mysql | grep -v information_schema)" for db in $DBS; do echo "Deleting $db" mysql -u$DBUSER -p$DBPASS -Bse "drop database $db; select sleep(0.1);" done 
+2
source

As far as I know, there is no specific command / request for deleting multiple databases without having a specific template in their names. Even I was asked to do a favor several times. Therefore, I did not research and did not find a specific solution. Then I tried the bottom hack. He worked without any problems. Maybe this can help you too.

Take all the databases using the command below.

 SHOW DATABASES ; 

Paste all of them into an excel / some other text file (I prefer nuclear power plants). Keep the only names you want to remove from the list. Remember to remove the worklist from the list.

Add DROP DATABASE in front of these names.

It's simple. Copy and paste all the ones on your desktop. You can complete all of them with one shot.

+1
source

Source: https://habr.com/ru/post/1335072/


All Articles