今天准备对一个MySQL数据库进行更换数据库名的操作。因为以前做过表名的更名,很简单的命令,所以想当然的以为 一个rename 就搞定的。当忙碌的一天结束,准备为晚上的操作写个步骤(这个习惯很好,不管多么简单的事情,只要涉及线上操作,我都会提前准备好对应的步骤,并进行测试)。然后晚上查了下注意事项,不查不知道,一查吓一跳。原来MySQL的rename database 并没有想象中的简单,这里面涉及了table、view、trigger、proceduer、event等很多的rename。可参考stackoverflow 上的一个帖子:https://stackoverflow.com/questions/12190000/rename-mysql-database
那么有没有简单的办法呢,答案是有的,那就是Percona Remote DBA写过的一个shell脚本,简单看了下,基本上是把手动做的这些rename 变成了自动,并加了一些判断,防止误操作。
#!/bin/bash# Copyright 2013 Percona LLC and/or its affiliatesset -eif [ -z "$3" ]; thenecho "rename_db <server> <database> <new_database>"exit 1fidb_exists=`mysql -h $1 -e "show databases like '$3'" -sss`if [ -n "$db_exists" ]; thenecho "ERROR: New database already exists $3"exit 1fiTIMESTAMP=`date +%s`character_set=`mysql -h $1 -e "show create database $2G" -sss | grep ^Create | awk -F'CHARACTER SET ' '{print $2}' | awk '{print $1}'`TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss`STATUS=$?if [ "$STATUS" != 0 ] || [ -z "$TABLES" ]; thenecho "Error retrieving tables from $2"exit 1fiecho "create database $3 DEFAULT CHARACTER SET $character_set"mysql -h $1 -e "create database $3 DEFAULT CHARACTER SET $character_set"TRIGGERS=`mysql -h $1 $2 -e "show triggersG" | grep Trigger: | awk '{print $2}'`VIEWS=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='VIEW'" -sss`if [ -n "$VIEWS" ]; thenmysqldump -h $1 $2 $VIEWS > /tmp/${2}_views${TIMESTAMP}.dumpfimysqldump -h $1 $2 -d -t -R -E > /tmp/${2}_triggers${TIMESTAMP}.dumpfor TRIGGER in $TRIGGERS; doecho "drop trigger $TRIGGER"mysql -h $1 $2 -e "drop trigger $TRIGGER"donefor TABLE in $TABLES; doecho "rename table $2.$TABLE to $3.$TABLE"mysql -h $1 $2 -e "SET FOREIGN_KEY_CHECKS=0; rename table $2.$TABLE to $3.$TABLE"doneif [ -n "$VIEWS" ]; thenecho "loading views"mysql -h $1 $3 < /tmp/${2}_views${TIMESTAMP}.dumpfiecho "loading triggers, routines and events"mysql -h $1 $3 < /tmp/${2}_triggers${TIMESTAMP}.dumpTABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss`if [ -z "$TABLES" ]; thenecho "Dropping database $2"mysql -h $1 $2 -e "drop database $2"fiif [ `mysql -h $1 -e "select count(*) from mysql.columns_priv where db='$2'" -sss` -gt 0 ]; thenCOLUMNS_PRIV=" UPDATE mysql.columns_priv set db='$3' WHERE db='$2';"fiif [ `mysql -h $1 -e "select count(*) from mysql.procs_priv where db='$2'" -sss` -gt 0 ]; thenPROCS_PRIV=" UPDATE mysql.procs_priv set db='$3' WHERE db='$2';"fiif [ `mysql -h $1 -e "select count(*) from mysql.tables_priv where db='$2'" -sss` -gt 0 ]; thenTABLES_PRIV=" UPDATE mysql.tables_priv set db='$3' WHERE db='$2';"fiif [ `mysql -h $1 -e "select count(*) from mysql.db where db='$2'" -sss` -gt 0 ]; thenDB_PRIV=" UPDATE mysql.db set db='$3' WHERE db='$2';"fiif [ -n "$COLUMNS_PRIV" ] || [ -n "$PROCS_PRIV" ] || [ -n "$TABLES_PRIV" ] || [ -n "$DB_PRIV" ]; thenecho "IF YOU WANT TO RENAME the GRANTS YOU NEED TO RUN ALL OUTPUT BELOW:"if [ -n "$COLUMNS_PRIV" ]; then echo "$COLUMNS_PRIV"; fiif [ -n "$PROCS_PRIV" ]; then echo "$PROCS_PRIV"; fiif [ -n "$TABLES_PRIV" ]; then echo "$TABLES_PRIV"; fiif [ -n "$DB_PRIV" ]; then echo "$DB_PRIV"; fiecho " flush privileges;"fi
文章转载自三杯酒coO,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




