Backing Up and Transferring MySQL Tables

From Littledamien Wiki
Revision as of 13:43, 5 March 2024 by Video8 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Copy entire database[edit]

Back up an entire database:

$ mysqldump -u user -p db-name > db-name-backup-YYMMDD.sql

Copy the back up to a new database

$ mysql -u user -p db-name < db-name.outdb-name-backup-YYMMDD.sql

Copy a table from one database to another[edit]

Create a table maintaining the engine, primary keys and indexes

CREATE TABLE `dst_database`.`table_name` LIKE `src_database`.`table_name`;

Populate table with data from a table in another database:

INSERT INTO `dst_database`.`table_name` SELECT * FROM `src_database`.`table_name`;

Note that these two statements are not supported by all MySQL Engine types though. InnoDB being one of them in MySQL ver 5.x