{{ it.name }}
{{ it.text }}
作者:杨涛涛
资深数据库专家,专研 MySQL 十余年。擅长 MySQL、PostgreSQL、MongoDB 等开源数据库相关的备份恢复、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MySQL 相关技术支持、MySQL 相关课程培训等工作。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
最近客户咨询了我一个关于如何更改 MySQL 库名的问题。其实如何安全的更改数据库名,是个非常棘手的问题,特别是针对 MySQL 来数据库来说。今天梳理出来,供大家参考。
rename database db_old to db_new
的命令来直接对数据库改名,可能由于实现的功能不完备(比如,这条命令可能是一个超大的事务,或者是由于之前的表很多还是 MyISAM 等),后来的版本直接取消了这条命令。
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
real 2m24.388s
user 0m5.422s
sys 0m1.120s
新库 yttdb_new 导入
root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old.sql
real 12m27.324s
user 0m3.778s
sys 0m0.947s
root@debian-ytt1:/home/ytt/mysql-sandboxes/3500/sandboxdata/yttdb_old# ls -l |wc -l
2002
root@debian-ytt1:/home/ytt/mysql-sandboxes/3500/sandboxdata/yttdb_old# du -sh
826M .
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.sql
real 1m41.901s
user 0m1.166s
sys 0m0.606s
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.sql
real 0m0.123s
user 0m0.007s
sys 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.048s
user 0m0.005s
sys 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.046s
user 0m0.006s
sys 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.050s
user 0m0.008s
sys 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.070s
user 0m0.006s
sys 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.054s
user 0m0.011s
sys 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.sql
real 0m0.222s
user 0m0.081s
sys 0m0.000s
root@debian-ytt1:/home/ytt# time mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old_view_lists.sql
real 0m0.158s
user 0m0.013s
sys 0m0.000s