MySQL数据库改名的详细方法教程
作者:弓长三虎 时间:2024-01-18 11:46:27
前戏
有时候生产环境是以项目来命名,有时候会出现更名情况,其实如何安全的更改数据库名,是个非常棘手的问题,特别是针对 MySQL 来数据库来说
被取消的命令
MySQL 之前提供了一个 rename database db_old to db_new 的命令来直接对数据库改名,可能由于实现的功能不完备(比如,这条命令可能是一个超大的事务,或者是由于之前的表很多还是 MyISAM 等),后来 的版本直接取消了这条命令。
更改数据库名大致上有以下几种方案:
mysqldump 导入导出
要说最简单的方法,就是直接用 mysqldump 工具,在旧库导出再往新库导入(最原始、最慢、最容易想到)的方法:
旧库 yttdb_old 导出(包含的对象:表、视图、触发器、事件、存储过程、存储函数)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 导入time mysql --login-path=root_ytt -D yttdb_new < /tmp/yttdb_old.sqlreal 12m27.324suser 0m3.778ssys 0m0.947s
改整库的表名
利用 MySQL 更改表名的方法来批量把旧库的所有表依次遍历,改名为新库的表。
这种方法比第一种要快很多倍,但是没有第一步操作起来那么顺滑,不能一步到位。比如,要把数据库 yttdb_old 改名为 yttdb_new,如果数据库 yttdb_old 里只有磁盘表,那很简单,直接改名即可
alter table yttdb_old.t1 to yttdb_new.t1;
或者写个脚本来批量改,非常简单。但是一般旧库里不只有磁盘表,还包含其他各种对象。这时候可以先考虑把旧库的各种对象导出来,完了在逐一改完表名后导进去。
导出旧库 yttdb_old 下除了磁盘表的其他所有对象(存储函数、存储过程、触发器、事件)
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 里被看作是表,因此得先查找出视图名字,再单独导出:
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
那这些额外的对象成功导出来后,就可以在旧库里删除他们了。当然了,做这些操作之前,建议把旧库的
所有对象,包括表,都备份出来,备份方式很多,这里就不细讲了。 现在我们来依次删除这些对象:(其实除了触发器和视图外,其他的对象也可以不用删除,不过为了让改 名完后旧库清空,就必须得先删掉它们)。
为了清晰期间,我这里每种对象单独删除,也可以直接一次性全部删除。
批量删除存储函数
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
批量删除触发器:
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 秒。接下来再把之前导出的其他对象导入新库 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
接下来进行功能验证,验证表数量、触发器、存储过程、存储函数、事件等数目是不是对的上。
古老的方案
其实在 MySQL 早期还有一种方法。
假设 MySQL 部署好了后,所有的 binlog 都有备份,并且二进制日志格式还是 statement 的话,那就可 以简单搭建一台从机,让它慢慢追主机到新的库名,等确切要更改旧库的时候,再直接晋升从机为主机即 可。 这里只需要从机配置一个参数来把旧库指向为新库: replicate-rewrite-db=yttdb_old->yttdb_new 不过这种局限性很大,不具备标准化,不推荐。
来源:https://blog.csdn.net/weixin_44772835/article/details/128222569
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php fread函数使用方法总结
如何区分SQL数据库中的主键与外键
Jupyter Notebook远程登录及密码设置操作
![](https://img.aspxhome.com/file/2023/5/118635_0s.jpg)
js表单验证控制代码大全
pyspark操作hive分区表及.gz.parquet和part-00000文件压缩问题
![](https://img.aspxhome.com/file/2023/0/79580_0s.png)
VMware中安装CentOS7(设置静态IP地址)并通过docker容器安装mySql数据库(超详细教程)
![](https://img.aspxhome.com/file/2023/3/92303_0s.jpg)
Access数据库出现0x80004005问题的解决方法
泛泛而谈界面中的斑马纹设计
![](https://img.aspxhome.com/file/UploadPic/20107/15/bm-001-76s.jpg)
flask开启多线程的具体方法
python 动态导入模块实现模块热更新的方法
![](https://img.aspxhome.com/file/2023/1/111991_0s.jpg)
Python wxPython库消息对话框MessageDialog用法示例
![](https://img.aspxhome.com/file/2023/7/107277_0s.png)
Python如何优雅获取本机IP方法
基于Python共轭梯度法与最速下降法之间的对比
![](https://img.aspxhome.com/file/2023/5/65885_0s.jpg)
通过实例简单了解Python中yield的作用
JS 中如何判断 null
原生Java操作mysql数据库过程解析
![](https://img.aspxhome.com/file/2023/0/64110_0s.png)
python实现整数序列求和
![](https://img.aspxhome.com/file/2023/9/93619_0s.png)
haskell实现多线程服务器实例代码
oracle查看被锁的表和被锁的进程以及杀掉这个进程
Pytorch 数据加载与数据预处理方式
![](https://img.aspxhome.com/file/2023/8/127708_0s.jpg)