Drop multiple databases with names matching a pattern

后端 未结 10 2252
南旧
南旧 2021-02-02 10:39

I want to drop all the databases starting with a word.

abc
xyz
cms_db1
cms_db2
cms_xyz
pqr

In the example given above, I will like to drop all

10条回答
  •  半阙折子戏
    2021-02-02 11:05

    Linux way:

    #!/bin/bash
    
    DB_STARTS_WITH="cms"
    MUSER="root"
    MPWD="yourpass"
    MYSQL="mysql"
    
    DBS="$($MYSQL -u$MUSER -p$MPWD -Bse 'show databases')"
    for db in $DBS; do
    
    if [[ "$db" =~ "^${DB_STARTS_WITH}" ]]; then
        echo "Deleting $db"
        $MYSQL -u$MUSER -p$MPWD -Bse "drop database $db"
    fi
    
    done
    

    Of course use the drop part at your own risk ;)

提交回复
热议问题