A novel way for MySQL to modify database names

  • 2020-06-23 02:07:51
  • OfStack

MySQL introduced an rename database operation in 5.1 but did not support this command after ES4en5.1.23. It can be said that it is an experimental function that has not been supported in production (mysql-5.1 release in ES7en-5.1.30). Therefore, in production, we sometimes need to change the name of 1 lower library in order to pursue perfection. How do you do that?
Here is a workaround.

1. Create a new library name:


mysql>create database db_v2;

2. Generate rename statement, migrate from olddb, I am olddb 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. Execute generated sql
mysql>source /tmp/rename_to_db_v2.sql

That's it. That's it.
Good luck!


Related articles: