Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving InnoDb DB

I have DB InnoDb innodb_db_1. I have turned on innodb_file_per_table.

If I go to var/lib/mysql/innodb_db_1/ I will find files table_name.ibd, table_name.frm, db.opt.

Now, I'm trying to copy these files to another DB for example to innodb_db_2(var/lib/mysql/innodb_db_2/) but nothing happened.

But if my DB will be MyIsam, I can copy in such way and everything be ok.

What suggestions to move the DB by copying the file of InnoDb DB?

like image 592
sergio Avatar asked Dec 04 '22 08:12

sergio


1 Answers

Even when you use file-per-table, the tables keep some of their data and metadata in /var/lib/mysql/ibdata1. So you can't just move .ibd files to a new MySQL instance.

You'll have to backup and restore your database. You can use:

  • mysqldump, included with MySQL, reliable but slow.

  • mydumper, a community contributed substitute for mysqldump, this supports compression and parallel execution and other neat features.

  • Percona XtraBackup, which is free and performs high-speed physical backups of InnoDB (and also supports other storage engines). This is recommended to minimize interruption to your live operations, and also if your database is large.


Re your comment:

No, you cannot just copy .ibd files. You cannot turn off the requirement for ibdata1. This file includes, among other things, a data dictionary which you can think of like a table of contents for a book. It tells InnoDB what tables you have, and which physical file they reside in.

If you just move a .ibd file into another MySQL instance, this does not add it to that instance's data dictionary. So InnoDB has no idea to look in the new file, or which logical table it goes with.

If you want a workaround, you could ALTER TABLE mytable ENGINE=MyISAM, move that file and its .frm to another instance, and then ALTER TABLE mytable ENGINE=InnoDB to change it back. Remember to FLUSH TABLES WITH READ LOCK before you move MyISAM files.

But these steps are not for beginners. It would be a lot safer for you to use the backup & restore method unless you know what you're doing. I'm trying to save you some grief.

like image 184
Bill Karwin Avatar answered Dec 06 '22 21:12

Bill Karwin