技術分享 | MySQL 資料庫如何改名?

愛可生 發佈 2020-06-17T12:57:53+00:00

資深資料庫專家,專研 MySQL 十餘年。擅長 MySQL、PostgreSQL、MongoDB 等開源資料庫相關的備份恢復、SQL 調優、監控運維、高可用架構設計等。

作者:楊濤濤

資深資料庫專家,專研 MySQL 十餘年。擅長 MySQL、PostgreSQL、MongoDB 等開源資料庫相關的備份恢復、SQL 調優、監控運維、高可用架構設計等。目前任職於愛可生,為各大運營商及銀行金融企業提供 MySQL 相關技術支持、MySQL 相關課程培訓等工作。

本文來源:原創投稿

*愛可生開源社區出品,原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。


最近客戶諮詢了我一個關於如何更改 MySQL 庫名的問題。其實如何安全的更改資料庫名,是個非常棘手的問題,特別是針對 MySQL 來資料庫來說。今天梳理出來,供大家參考。


被取消的命令

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

更改資料庫名大致上有以下幾種方案:


一、mysqldump 導入導出

要說最簡單的方法,就是直接用 mysqldump 工具,在舊庫導出再往新庫導入(最原始、最慢、最容易想到)的方法:

舊庫 yttdb_old 導出(包含的對象:表、視圖、觸發器、事件、存儲過程、存儲函數)

root@debian-ytt1:/home/ytt# time mysqldump --login-path=root_ytt --set-gtid-purged=off  \> --single-transaction --routines --events yttdb_old > /tmp/yttdb_old.sqlreal    2m24.388suser    0m5.422ssys     0m1.120s

新庫 yttdb_new 導入

root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old.sqlreal    12m27.324suser    0m3.778ssys     0m0.947s

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

root@debian-ytt1:/home/ytt/mysql-sandboxes/3500/sandboxdata/yttdb_old# ls -l |wc -l2002root@debian-ytt1:/home/ytt/mysql-sandboxes/3500/sandboxdata/yttdb_old# du -sh826M    .

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


二、改整庫的表名

利用 MySQL 更改表名的方法來批量把舊庫的所有表依次遍歷,改名為新庫的表。

這種方法比第一種要快很多倍,但是沒有第一步操作起來那麼順滑,不能一步到位。比如,要把資料庫 yttdb_old 改名為 yttdb_new,如果資料庫 yttdb_old 里只有磁碟表,那很簡單,直接改名即可。

alter table yttdb_old.t1 to yttdb_new.t1;

或者寫個腳本來批量改,非常簡單。

但是一般舊庫里不只有磁碟表,還包含其他各種對象。這時候可以先考慮把舊庫的各種對象導出來,完了在逐一改完表名後導進去。

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

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.sqlreal    1m41.901suser    0m1.166ssys     0m0.606s

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

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 '\n' ' '`root@debian-ytt1:~# time  mysqldump --login-path=root_ytt --set-gtid-purged=off \> --triggers=false yttdb_old $view_list  > /tmp/yttdb_old_view_lists.sqlreal    0m0.123suser    0m0.007ssys     0m0.007s

那這些額外的對象成功導出來後,就可以在舊庫里刪除他們了。當然了,做這些操作之前,建議把舊庫的所有對象,包括表,都備份出來,備份方式很多,這裡就不細講了。

現在我們來依次刪除這些對象:(其實除了觸發器和視圖外,其他的對象也可以不用刪除,不過為了讓改名完後舊庫清空,就必須得先刪掉它們)。

為了清晰期,我這裡每種對象單獨刪除,也可以直接一次性全部刪除。

批量刪除存儲函數:

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`root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$func_lists"real    0m0.048suser    0m0.005ssys     0m0.005s

批量刪除存儲過程:

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`root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$procedure_lists"real    0m0.046suser    0m0.006ssys     0m0.005s

批量刪除觸發器:

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`root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$trigger_lists"real    0m0.050suser    0m0.008ssys     0m0.003s

批量刪除視圖:

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`root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e "use yttdb_old;$view_lists"real    0m0.070suser    0m0.006ssys     0m0.005s

批量刪除事件:

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`root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -e \> "use yttdb_old;$event_lists"real    0m0.054suser    0m0.011ssys     0m0.000s

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

(debian-ytt1:3500)|(yttdb_new)>set group_concat_max_len = 18446744073709551615;Query OK, 0 rows affected (0.00 sec)(debian-ytt1:3500)|(yttdb_new)>SELECT CONCAT('rename table ',                GROUP_CONCAT(CONCAT(' yttdb_old.',table_name,' to yttdb_new.',table_name)) )                FROM information_schema.TABLES                WHERE table_schema = 'yttdb_old' AND table_type = 1 INTO @rename_lists;Query OK, 1 row affected (0.01 sec)(debian-ytt1:3500)|(yttdb_new)>prepare s1 from @rename_lists;Query OK, 0 rows affected (0.00 sec)Statement prepared(debian-ytt1:3500)|(yttdb_new)>execute s1;Query OK, 0 rows affected (55.41 sec)(debian-ytt1:3500)|(yttdb_new)>drop prepare s1;Query OK, 0 rows affected (00.01 sec)

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

root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D \> yttdb_new < /tmp/yttdb_old_other_object.sqlreal    0m0.222suser    0m0.081ssys     0m0.000sroot@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D \> yttdb_new < /tmp/yttdb_old_view_lists.sqlreal    0m0.158suser    0m0.013ssys     0m0.000s

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


三、歷史方案

其實在 MySQL 早期還有一種方法。

假設 MySQL 部署好了後,所有的 binlog 都有備份,並且二進位日誌格式還是 statement 的話,那就可以簡單搭建一台從機,讓它慢慢追主機到新的庫名,等確切要更改舊庫的時候,再直接晉升從機為主機即可。

這裡只需要從機配置一個參數來把舊庫指向為新庫:

replicate-rewrite-db=yttdb_old->yttdb_new

不過這種局限性很大,不具備標準化,不推薦。


總結

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

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

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

關鍵字: