MySQL數(shù)據(jù)庫改名的詳細(xì)方法教程
前戲
有時候生產(chǎn)環(huán)境是以項目來命名,有時候會出現(xiàn)更名情況,其實如何安全的更改數(shù)據(jù)庫名,是個非常棘手的問題,特別是針對 MySQL 來數(shù)據(jù)庫來說
被取消的命令
MySQL 之前提供了一個 rename database db_old to db_new 的命令來直接對數(shù)據(jù)庫改名,可能由于實現(xiàn)的功能不完備(比如,這條命令可能是一個超大的事務(wù),或者是由于之前的表很多還是 MyISAM 等),后來 的版本直接取消了這條命令。
更改數(shù)據(jù)庫名大致上有以下幾種方案:
mysqldump 導(dǎo)入導(dǎo)出
要說最簡單的方法,就是直接用 mysqldump 工具,在舊庫導(dǎo)出再往新庫導(dǎo)入(最原始、最慢、最容易想到)的方法:
舊庫 yttdb_old 導(dǎo)出(包含的對象:表、視圖、觸發(fā)器、事件、存儲過程、存儲函數(shù))time mysqldump --login-path=root_ytt --set-gtidpurged=off --single-transaction --routines --events yttdb_old > /tmp/yttdb_old.sqlreal 2m24.388suser 0m5.422ssys 0m1.120s新庫 yttdb_new 導(dǎo)入time mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old.sqlreal 12m27.324suser 0m3.778ssys 0m0.947s
改整庫的表名
利用 MySQL 更改表名的方法來批量把舊庫的所有表依次遍歷,改名為新庫的表。
這種方法比第一種要快很多倍,但是沒有第一步操作起來那么順滑,不能一步到位。比如,要把數(shù)據(jù)庫 yttdb_old 改名為 yttdb_new,如果數(shù)據(jù)庫 yttdb_old 里只有磁盤表,那很簡單,直接改名即可
alter table yttdb_old.t1 to yttdb_new.t1;
或者寫個腳本來批量改,非常簡單。但是一般舊庫里不只有磁盤表,還包含其他各種對象。這時候可以先考慮把舊庫的各種對象導(dǎo)出來,完了在逐一改完表名后導(dǎo)進(jìn)去。
導(dǎo)出舊庫 yttdb_old 下除了磁盤表的其他所有對象(存儲函數(shù)、存儲過程、觸發(fā)器、事件)
time mysqldump --login-path=root_ytt -t -d -n --setgtid-purged=off --triggers --routines --events yttdb_old > /tmp/yttdb_old_other_object.sqlreal 1m41.901suser 0m1.166ssys 0m0.606s
視圖在 MySQL 里被看作是表,因此得先查找出視圖名字,再單獨導(dǎo)出:
view_list=`mysql --login-path=root_ytt -e "SELECT table_name FROM information_schema.views WHERE table_schema='yttdb_old';" -s | tr '\n' ' '`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
那這些額外的對象成功導(dǎo)出來后,就可以在舊庫里刪除他們了。當(dāng)然了,做這些操作之前,建議把舊庫的
所有對象,包括表,都備份出來,備份方式很多,這里就不細(xì)講了。 現(xiàn)在我們來依次刪除這些對象:(其實除了觸發(fā)器和視圖外,其他的對象也可以不用刪除,不過為了讓改 名完后舊庫清空,就必須得先刪掉它們)。
為了清晰期間,我這里每種對象單獨刪除,也可以直接一次性全部刪除。
批量刪除存儲函數(shù)
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 time mysql --login-path=root_ytt -e "use yttdb_old;$func_lists" real 0m0.048suser 0m0.005ssys 0m0.005s
批量刪除存儲過程:
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`time mysql --login-path=root_ytt -e "use yttdb_old;$trigger_lists"real 0m0.050suser 0m0.008ssys 0m0.003s
批量刪除觸發(fā)器:
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`time mysql --login-path=root_ytt -e "use yttdb_old;$trigger_lists"real 0m0.050suser 0m0.008ssys 0m0.003s
批量刪除視圖:
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`time mysql --login-path=root_ytt -e "use yttdb_old;$view_lists"real 0m0.070suser 0m0.006ssys 0m0.005s
批量刪除事件:
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` 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 秒。接下來再把之前導(dǎo)出的其他對象導(dǎo)入新庫 yttdb_new:
time mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old_other_object.sqlreal 0m0.222suser 0m0.081ssys 0m0.000stime mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old_view_lists.sqlreal 0m0.158suser 0m0.013ssys 0m0.000s
接下來進(jìn)行功能驗證,驗證表數(shù)量、觸發(fā)器、存儲過程、存儲函數(shù)、事件等數(shù)目是不是對的上。
古老的方案
其實在 MySQL 早期還有一種方法。
假設(shè) MySQL 部署好了后,所有的 binlog 都有備份,并且二進(jìn)制日志格式還是 statement 的話,那就可 以簡單搭建一臺從機(jī),讓它慢慢追主機(jī)到新的庫名,等確切要更改舊庫的時候,再直接晉升從機(jī)為主機(jī)即 可。 這里只需要從機(jī)配置一個參數(shù)來把舊庫指向為新庫: replicate-rewrite-db=yttdb_old->yttdb_new 不過這種局限性很大,不具備標(biāo)準(zhǔn)化,不推薦。
總結(jié)
其實針對 MySQL 本身改庫名,大致就這么幾種方法:
如果數(shù)據(jù)量小,推薦第一種;數(shù)據(jù)量大,則推薦第二種;數(shù)據(jù)量巨大,那就非 MySQL 本身能解決的了。
可通過部署第三方 ETL 工具,通過解析 MySQL 二進(jìn)制日志或其他的方式來把舊庫數(shù)據(jù)直接讀取到新庫達(dá)到改名的目的等等
到此這篇關(guān)于MySQL數(shù)據(jù)庫改名的文章就介紹到這了,更多相關(guān)MySQL數(shù)據(jù)庫改名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL與PHP的基礎(chǔ)與應(yīng)用專題之?dāng)?shù)據(jù)查詢語句
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),屬于 Oracle 旗下產(chǎn)品。MySQL 是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,本系列將帶你掌握php與mysql的基礎(chǔ)應(yīng)用,本篇從數(shù)據(jù)查詢語句開始2022-02-02Mysql查詢語句如何實現(xiàn)無限層次父子關(guān)系查詢
這篇文章主要介紹了Mysql查詢語句如何實現(xiàn)無限層次父子關(guān)系查詢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07MySQL參數(shù)lower_case_table_name的實現(xiàn)
lower_case_table_names是一個重要的系統(tǒng)變量,它影響著MySQL如何處理表名的大小寫,本文主要介紹了MySQL參數(shù)lower_case_table_name的實現(xiàn),感興趣的可以了解一下2024-08-08阿里云服務(wù)器手動實現(xiàn)mysql雙機(jī)熱備的兩種方式
阿里云服務(wù)器由于不支持keepalive虛擬ip,導(dǎo)致無法通過keepalive來實現(xiàn)mysql的雙機(jī)熱備。我們這里要實現(xiàn)阿里云的雙機(jī)熱備有兩種方式。感興趣的朋友跟隨小編一起看看吧2019-10-10