MySQL在5.1引入了一個rename database操作,但在MySQL5.1.23後又不支持這個命令。可以說是一個實驗性的功能,沒有在生產中支持過(mysql-5.1 release在mysql-5.1.30),那麼生產中我們有時為了追求完美需要改一下庫名。怎麼操作呢?
這裡提供一個變通的方法。
1. 創建出新庫名:
mysql>create database db_v2; mysql>create database db_v2;
2.生成rename語句,從olddb裡遷移,我這裡olddb裡sbtest;
mysql>select concat("rename table ",table_schema,".",table_name," to db_v2.",table_name,";") into outfile '/tmp/rename_to_db_v2.sql' from information_schema.tables where table_schema='sbtest'; mysql>select concat("rename table ",table_schema,".",table_name," to db_v2.",table_name,";") into outfile '/tmp/rename_to_db_v2.sql' from information_schema.tables where table_schema='sbtest';
3.執行生成的sql
mysql>source /tmp/rename_to_db_v2.sql mysql>source /tmp/rename_to_db_v2.sql
就這麼簡單可以搞定了。
Good luck!