Renaming a MySQL Database: An Efficient Solution for InnoDB
Renaming a MySQL database can be a daunting task, especially for large databases and those utilizing InnoDB, which stores data differently than MyISAM. However, with the right approach, it can be done efficiently.
To rename an InnoDB database, we need to create a new empty database and then move each table individually into it:
RENAME TABLE old_db.table TO new_db.table;
It's important to adjust the database permissions after the table migration.
For scripting in a shell, you can use one of the following commands:
mysql -u username -ppassword old_db -sNe 'show tables' | while read table; \ do mysql -u username -ppassword -sNe "rename table old_db.$table to new_db.$table"; done
or
for table in `mysql -u root -ppassword -s -N -e "use old_db;show tables from old_db;"`; do mysql -u root -ppassword -s -N -e "use old_db;rename table old_db.$table to new_db.$table;"; done;
Notes:
The above is the detailed content of How Can I Efficiently Rename a MySQL InnoDB Database?. For more information, please follow other related articles on the PHP Chinese website!