Home > Database > Mysql Tutorial > How Can I Efficiently Rename a MySQL InnoDB Database?

How Can I Efficiently Rename a MySQL InnoDB Database?

Mary-Kate Olsen
Release: 2024-12-23 17:34:11
Original
449 people have browsed it

How Can I Efficiently Rename a MySQL InnoDB Database?

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;
Copy after login

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
Copy after login

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;
Copy after login

Notes:

  • Do not leave a space between the -p option and the password.
  • If the database has no password, remove the -u username -ppassword part.
  • If a table contains a trigger, it cannot be moved using the above method. In such cases, use the traditional database cloning technique: mysqldump old_db | mysql new_db.
  • Stored procedures can be copied after the database rename: mysqldump -R old_db | mysql new_db.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template