ITPub博客

首页 > IT职业 > IT职场 > MySQL中修改库名的操作教程

MySQL中修改库名的操作教程

IT职场 作者:ITpubnet 时间:2021-09-09 09:38:21 0 删除 编辑

MySQL在5.1引入了一个rename database操作,但在MySQL5.1.23后又不支持这个命令。可以说是一个实验性的功能,没有在生产中支持过(mysql-5.1 release在mysql-5.1.30),那么生产中我们有时为了追求完美需要改一下库名。怎么操作呢?
这里提供一个变通的方法。
1. 创建出新库名:

?

123 mysql>create database db_v2;  mysql>create database db_v2;

2.生成rename语句,从olddb里迁移,我这里olddb里sbtest;

?

123 mysql>select concat("rename table ",table_schema,".",table_name," to db_v2.",table_name,";") into outfile '/tmp/rename_to_db_v2.sql' from information_schema.tables where table_schema='sbtest';  mysql>select concat("rename table ",table_schema,".",table_name," to db_v2.",table_name,";") into outfile '/tmp/rename_to_db_v2.sql' from information_schema.tables where table_schema='sbtest';

3.执行生成的sql

?

123 mysql>source /tmp/rename_to_db_v2.sql  mysql>source /tmp/rename_to_db_v2.sql

就这么简单可以搞定了。
Good luck!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4556/viewspace-2811095/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    91
  • 访问量
    900