Drop multiple databases with names matching a pattern

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

    I would use something like:

    echo "SHOW DATABASES LIKE 'cms_%'" \
      | mysql \
      | tail -n +2 \
      | xargs -n1 mysqladmin -f drop
    

    If you don't have your default username and password configured inside ~/my.cnf, you may need to supply the username and password via the -u and -p switches to the mysql/mysqladmin commands above.

    (Edit - added -n arg to tail.)

提交回复
热议问题