Renaming a database
suggest changeThere is no single command to rename a MySQL database but a simple workaround can be used to achieve this by backing up and restoring:
mysqladmin -uroot -p<password> create <new name>
mysqldump -uroot -p<password> --routines <old name> | mysql -uroot -pmypassword <new name>
mysqladmin -uroot -p<password> drop <old name>
Steps:
- Copy the lines above into a text editor.
- Replace all references to
<old name>,<new name>and<password>(+ optionallyrootto use a different user) with the relevant values. - Execute one by one on the command line (assuming the MySQL “bin” folder is in the path and entering “y” when prompted).
Alternative Steps:
Rename (move) each table from one db to the other. Do this for each table:
RENAME TABLE `<old db>`.`<name>` TO `<new db>`.`<name>`;
You can create those statements by doing something like
SELECT CONCAT('RENAME TABLE old_db.', table_name, ' TO ',
'new_db.', table_name)
FROM information_schema.TABLES
WHERE table_schema = 'old_db';
Do not rename a database or table by moving files in the data directory. That only worked for old MyISAM layouts. InnoDB tablespaces and the MySQL 8.0 data dictionary will not follow those files. Moving a partition of an InnoDB table, rather than dropping it, uses transportable tablespaces. RENAME TABLE also does not move views, routines, events, or triggers. Copy those separately. The mysqldump path above includes routines only because of --routines. It still omits events unless you add --events.