Drop multiple databases with names matching a pattern

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

提交回复
热议问题