How do I quickly rename a MySQL database (change schema name)?

前端 未结 30 2700
余生分开走
余生分开走 2020-11-22 14:54

The MySQL manual at MySQL covers this.

Usually I just dump the database and reimport it with a new name. This is not an option for very big databases. Apparently

30条回答
  •  [愿得一人]
    2020-11-22 15:19

    For your convenience, below is a small shellscript that has to be executed with two parameters: db-name and new db-name.

    You might need to add login-parameters to the mysql-lines if you don't use the .my.cnf-file in your home-directory. Please make a backup before executing this script.


    #!/usr/bin/env bash
    
    mysql -e "CREATE DATABASE $2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
    for i in $(mysql -Ns $1 -e "show tables");do
        echo "$1.$i -> $2.$i"
        mysql -e "rename TABLE $1.$i to $2.$i"
    done
    mysql -e "DROP DATABASE $1"
    

提交回复
热议问题