不清楚你問的是哪種資料庫,不同資料庫系統的實現方法也不同。


被取消的命令MySQL 之前提供了一個 rename database db_old to db_new 的命令來直接對資料庫改名,可能由於實現的功能不完備(比如,這條命令可能是一個超大的事務,或者是由於之前的表很多還是 MyISAM 等),後來的版本直接取消了這條命令。更改資料庫名大致上有以下幾種方案:

一、mysqldump 導入導出要說最簡單的方法,就是直接用 mysqldump 工具,在舊庫導出再往新庫導入(最原始、最慢、最容易想到)的方法:舊庫 yttdb_old 導出(包含的對象:表、視圖、觸發器、事件、存儲過程、存儲函數)

  1. root@debian-ytt1:/home/ytt# time mysqldump --login-path=root_ytt --set-gtid-purged=off --single-transaction --routines --events yttdb_old &> /tmp/yttdb_old.sql
  2. real 2m24.388s
  3. user 0m5.422s
  4. sys 0m1.120s

新庫 yttdb_new 導入

  1. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D yttdb_new &< /tmp/yttdb_old.sql
  2. real 12m27.324s
  3. user 0m3.778s
  4. sys 0m0.947s

以上結果是在我個人筆記本的虛擬機上測試,時間上花費了 12 分 27 秒,這裡源庫 yttdb_old 上的表個數為 2002,總共也就 826M,不到 1G,並且包含了視圖,觸發器,存儲過程,存儲函數,事件等都有。

  1. root@debian-ytt1:/home/ytt/mysql-sandboxes/3500/sandboxdata/yttdb_old# ls -l |wc -l
  2. 2002
  3. root@debian-ytt1:/home/ytt/mysql-sandboxes/3500/sandboxdata/yttdb_old# du -sh
  4. 826M .

接下來,記得刪除舊庫 yttdb_old, 那資料庫改名就完成了。看起來這個方法非常簡單,可是最大的缺點是太慢了!那有沒有其他的比較快的方法呢?答案是有的,不過步驟比這個要複雜很多。接下來來看第二種方法。

二、改整庫的表名利用 MySQL 更改表名的方法來批量把舊庫的所有表依次遍歷,改名為新庫的表。這種方法比第一種要快很多倍,但是沒有第一步操作起來那麼順滑,不能一步到位。比如,要把資料庫 yttdb_old 改名為 yttdb_new,如果資料庫 yttdb_old 里只有磁碟表,那很簡單,直接改名即可。

  1. alter table yttdb_old.t1 to yttdb_new.t1;

或者寫個腳本來批量改,非常簡單。但是一般舊庫里不只有磁碟表,還包含其他各種對象。這時候可以先考慮把舊庫的各種對象導出來,完了在逐一改完表名後導進去。

導出舊庫 yttdb_old 下除了磁碟表外的其他所有對象(存儲函數、存儲過程、觸發器、事件)

  1. root@debian-ytt1:/home/ytt# time mysqldump --login-path=root_ytt -t -d -n --set-gtid-purged=off --triggers --routines --events yttdb_old &> /tmp/yttdb_old_other_object.sql
  2. real 1m41.901s
  3. user 0m1.166s
  4. sys 0m0.606s

視圖在 MySQL 里被看作是表,因此得先查找出視圖名字,再單獨導出:

  1. root@debian-ytt1:~# view_list=`mysql --login-path=root_ytt -e "SELECT table_name FROM information_schema.views WHERE table_schema = yttdb_old;" -s | tr
    `
  2. root@debian-ytt1:~# time mysqldump --login-path=root_ytt --set-gtid-purged=off --triggers=false yttdb_old $view_list &> /tmp/yttdb_old_view_lists.sql
  3. real 0m0.123s
  4. user 0m0.007s
  5. sys 0m0.007s

那這些額外的對象成功導出來後,就可以在舊庫里刪除他們了。當然了,做這些操作之前,建議把舊庫的所有對象,包括表,都備份出來,備份方式很多,這裡就不細講了。現在我們來依次刪除這些對象:(其實除了觸發器和視圖外,其他的對象也可以不用刪除,不過為了讓改名完後舊庫清空,就必須得先刪掉它們)。為了清晰期間,我這裡每種對象單獨刪除,也可以直接一次性全部刪除。

批量刪除存儲函數:

  1. root@debian-ytt1:/home/ytt# func_lists=`mysql --login-path=root_ytt -e "SELECT concat(drop function if exists ,routine_name,;) FROM information_schema.routines WHERE routine_schema = yttdb_old AND routine_type = 1 " -ss`
  2. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$func_lists"
  3. real 0m0.048s
  4. user 0m0.005s
  5. sys 0m0.005s

批量刪除存儲過程:

  1. root@debian-ytt1:/home/ytt# procedure_lists=`mysql --login-path=root_ytt -e "SELECT concat(drop procedure if exists ,routine_name,;) FROM information_schema.routines WHERE routine_schema = yttdb_old AND routine_type = 2 " -ss`
  2. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$procedure_lists"
  3. real 0m0.046s
  4. user 0m0.006s
  5. sys 0m0.005s

批量刪除觸發器:

  1. root@debian-ytt1:/home/ytt# trigger_lists=`mysql --login-path=root_ytt -e "SELECT concat(drop trigger if exists yttdb_old.,trigger_name,;) FROM information_schema.TRIGGERS WHERE trigger_schema=yttdb_old" -ss`
  2. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$trigger_lists"
  3. real 0m0.050s
  4. user 0m0.008s
  5. sys 0m0.003s

批量刪除視圖:

  1. root@debian-ytt1:/home/ytt# view_lists=`mysql --login-path=root_ytt -e "SELECT concat(drop view if exists ,table_name,;) FROM information_schema.VIEWS WHERE table_schema=yttdb_old" -ss`
  2. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$view_lists"
  3. real 0m0.070s
  4. user 0m0.006s
  5. sys 0m0.005s

批量刪除事件:

  1. root@debian-ytt1:/home/ytt# event_lists=`mysql --login-path=root_ytt -e "SELECT concat(drop event if exists ,event_name,;) FROM information_schema.EVENTS WHERE event_schema=yttdb_old" -ss`
  2. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$event_lists"
  3. real 0m0.054s
  4. user 0m0.011s
  5. sys 0m0.000s

完了後利用 rename table old_table to new_table 語句來批量更改表名到新庫:

  1. (debian-ytt1:3500)|(yttdb_new)&>set group_concat_max_len = 18446744073709551615;
  2. Query OK, 0 rows affected (0.00 sec)
  3. (debian-ytt1:3500)|(yttdb_new)&>SELECT CONCAT(rename table ,
  4. GROUP_CONCAT(CONCAT( yttdb_old.,table_name, to yttdb_new.,table_name)) )
  5. FROM information_schema.TABLES
  6. WHERE table_schema = yttdb_old AND table_type = 1 INTO @rename_lists;
  7. Query OK, 1 row affected (0.01 sec)
  8. (debian-ytt1:3500)|(yttdb_new)&>prepare s1 from @rename_lists;
  9. Query OK, 0 rows affected (0.00 sec)
  10. Statement prepared
  11. (debian-ytt1:3500)|(yttdb_new)&>execute s1;
  12. Query OK, 0 rows affected (55.41 sec)
  13. (debian-ytt1:3500)|(yttdb_new)&>drop prepare s1;
  14. Query OK, 0 rows affected (00.01 sec)

批量更改表名總共才花費 55.41 秒。接下來再把之前導出來的其他對象導入新庫 yttdb_new:

  1. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D yttdb_new &< /tmp/yttdb_old_other_object.sql
  2. real 0m0.222s
  3. user 0m0.081s
  4. sys 0m0.000s
  5. root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D yttdb_new &< /tmp/yttdb_old_view_lists.sql
  6. real 0m0.158s
  7. user 0m0.013s
  8. sys 0m0.000s

接下來進行功能驗證,驗證表數量、觸發器、存儲過程、存儲函數、事件等數目是不是對的上。

三、歷史方案其實在 MySQL 早期還有一種方法。假設 MySQL 部署好了後,所有的 binlog 都有備份,並且二進位日誌格式還是 statement 的話,那就可以簡單搭建一台從機,讓它慢慢追主機到新的庫名,等確切要更改舊庫的時候,再直接晉陞從機為主機即可。這裡只需要從機配置一個參數來把舊庫指向為新庫:replicate-rewrite-db=yttdb_old-&>yttdb_new不過這種局限性很大,不具備標準化,不推薦。

總結其實針對 MySQL 本身改庫名,大致就這麼幾種方法:

  • 如果數據量小,推薦第一種;
  • 數據量大,則推薦第二種;
  • 數據量巨大,那就非 MySQL 本身能解決的了。

可通過部署第三方 ETL 工具,通過解析 MySQL 二進位日誌或其他的方式來把舊庫數據直接讀取到新庫達到改名的目的等等。


我猜資料庫應該提供rename的sql的命令的


1、如果是 MySQL,並沒有 RENAME DATABASE 的用法;

2、如果是 MySQL InnoDB,可以先創建一個空的資料庫,然後使用如下命令:

CREATE DATABASE new_db;
RENAME TABLE old_db.table TO new_db.table;

3、以上只是參考 SQL,題主還需要在 old_db 執行 SHOW TABLES 獲取所有的表名;

4、執行完之後,再重新對之前的 MySQL 用戶授權即可。


……建議你導出資料庫腳本,在腳本改名字,或者,用可視化工具,通過數據傳輸,把資料庫搬你想改的資料庫


推薦閱讀:
相关文章