【Mysql】改資料庫庫名操作

小亮520cl發表於2016-03-21

MySQL在5.1引入了一個rename database操作,但在MySQL5.1.23後又不支援這個命令。可以說是一個實驗性的功能,沒有在生產中支援過(mysql-5.1 release在mysql-5.1.30),那麼生產中我們有時為了追求完美需要改一下庫名。怎麼操作呢?
這裡提供一個變通的方法。
1. 建立出新庫名:

點選(此處)摺疊或開啟

  1. mysql> create database testbak;

2生成rename語句,從olddb裡遷移;
點選(此處)摺疊或開啟
  1. mysql> select concat("rename table ",table_schema,".",table_name," to testbak.",table_name,";") into outfile '/tmp/rename_to_db_v2.sql' from information_schema.tables where table_schema='test';

[root@host-192-168-1-56 ~]# more /tmp/rename_to_db_v2.sql   
rename table test.bc_dbfsxiang to testbak.bc_dbfsxiang;
rename table test.bc_dkfsxiang to testbak.bc_dkfsxiang;
rename table test.bc_dkytxiang to testbak.bc_dkytxiang;
rename table test.bc_gzffxiang to testbak.bc_gzffxiang;
rename table test.bc_option_map to testbak.bc_option_map;
rename table test.bc_qylxxiang to testbak.bc_qylxxiang;
rename table test.bc_service_city to testbak.bc_service_city;
rename table test.bc_service_qi_city to testbak.bc_service_qi_city;
rename table test.bc_wffkhyxiang to testbak.bc_wffkhyxiang;
rename table test.bc_wffkzyxiang to testbak.bc_wffkzyxiang;
rename table test.bc_xindai to testbak.bc_xindai;
rename table test.bc_xindai_fields_v to testbak.bc_xindai_fields_v;
rename table test.bc_xindai_geren to testbak.bc_xindai_geren;
rename table test.bc_xindai_qiye to testbak.bc_xindai_qiye;
rename table test.bc_xyjlxiang to testbak.bc_xyjlxiang;
rename table test.bc_zyyqxiang to testbak.bc_zyyqxiang;
rename table test.fake_names to testbak.fake_names;
rename table test.idmap to testbak.idmap;
rename table test.sbtest to testbak.sbtest;
rename table test.tbUser to testbak.tbUser;
rename table test.test to testbak.test;
rename table test.test2 to testbak.test2;


3  執行即可

點選(此處)摺疊或開啟

  1. mysql> source /tmp/rename_to_db_v2.sql;





原文地址:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2061136/,如需轉載,請註明出處,否則將追究法律責任。

相關文章