Drop multiple databases with names matching a pattern

后端 未结 10 2097
南旧
南旧 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 10:46

    Here's a pure mySQL solution in two queries:

    SELECT CONCAT('DROP DATABASE `', SCHEMA_NAME, '`;')
    FROM `information_schema`.`SCHEMATA`
    WHERE SCHEMA_NAME LIKE 'cms_%';
    

    Then copy and paste the resulting recordset and run

    0 讨论(0)
  • 2021-02-02 10:46

    I had to improve neurinos script because of special chars in my password, missing 'drop DATABASE ...' and not working comparision for DB_STARTS_WITH expression. The following script did work on Ubuntu Server:

    #!/bin/bash
    
    DB_STARTS_WITH="grp"
    MUSER="root"
    MPWD="YOUR_PASSWORD"
    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
    
    0 讨论(0)
  • 2021-02-02 10:47

    If you wish to stay completely within MySQL/MariaDB (i.e. without using bash scripts and such) you can do the following:

    DELIMITER //
    CREATE PROCEDURE clean()
    BEGIN
        SET @query := (SELECT CONCAT('DROP DATABASE ', SCHEMA_NAME, ';') FROM `information_schema`.`SCHEMATA` WHERE SCHEMA_NAME LIKE 'dbtVDB%' LIMIT 1);
        WHILE @query != '' DO
            PREPARE stmt FROM @query;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            SET @query := (SELECT CONCAT('DROP DATABASE ', SCHEMA_NAME, ';') FROM `information_schema`.`SCHEMATA` WHERE SCHEMA_NAME LIKE 'cms%' LIMIT 1);
        END WHILE;
        DELETE FROM mysql.db WHERE mysql.db.Db LIKE 'cms%';
    END;
    //
    DELIMITER ;
    CALL clean();
    DROP PROCEDURE clean;
    
    0 讨论(0)
  • 2021-02-02 10:49

    Using @léo-alves-de-araujo I have modified it to ask the user/password (More secure way) from command line (with linux)

    #!/bin/bash 
    echo -n "Enter Mysql User:"
    read user
    echo -n "Enter Mysql Password:"
    read -s password
    for db_name in $(mysql -u $user --password=$password -e "SHOW DATABASES LIKE 'cms_%'" -ss 2>/dev/null)
    do
            mysql -u $user --password=$password -e "DROP DATABASE ${db_name}" 2>/dev/null;
    done
    
    0 讨论(0)
  • 2021-02-02 10:54

    I liked the answer suggesting a "for" loop from the shell. In my case, I had subdirectory names matching my database names so I made arrays, then used them in the command.

    (I could have done this using the mysql data directory come to think of it, even if I hadn't had the setup I had. On my bitnami VM this is /opt/bitnami/mysql/data.)

    1. created array from subset of files: tbtdirs=(tbt*2015*)

    2. Tested a potentially spooky command first w/ "echo": for d in ${tbtdirs[@]}; do echo mysql -pPASS -e "drop database $d"; done

    3. dropped all databases in the array: for d in ${tbtdirs[@]}; do mysql -pPASS -e "drop database $d"; done

    Worked like a charm! Also modified the loop to remove subdirectories. I used Linux command line for quite some time before learning how useful the bash commands could be.

    0 讨论(0)
  • 2021-02-02 10:56

    A Linux way:

    for db_name in $(mysql -u USER -pPASS -e "show databases like 'cms_%'" -ss)
    do
         mysql -u USER -pPASS -e "drop database ${db_name}";
    done
    
    0 讨论(0)
提交回复
热议问题