溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL數(shù)據(jù)庫如何改名

發(fā)布時間:2023-03-13 13:53:41 來源:億速云 閱讀:131 作者:iii 欄目:MySQL數(shù)據(jù)庫

這篇“MySQL數(shù)據(jù)庫如何改名”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“MySQL數(shù)據(jù)庫如何改名”文章吧。

被取消的命令

MySQL 之前提供了一個 rename database db_old to db_new 的命令來直接對數(shù)據(jù)庫改名,可能由于實(shí)現(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ú)導(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)在我們來依次刪除這些對象:(其實(shí)除了觸發(fā)器和視圖外,其他的對象也可以不用刪除,不過為了讓改 名完后舊庫清空,就必須得先刪掉它們)。

為了清晰期間,我這里每種對象單獨(dú)刪除,也可以直接一次性全部刪除。

批量刪除存儲函數(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)

批量更改表名總共才花費(fèi) 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ù)目是不是對的上。

古老的方案

其實(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)化,不推薦。

以上就是關(guān)于“MySQL數(shù)據(jù)庫如何改名”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI