Mysql – How move MySql table to another disk

alter-tableMySQLmysql-5.6table

According to this it is impossible to use alter table for moving tables. So if I understand correctlly – to move table it is neccessary to do:

  1. Create new table
  2. Copy all row from old one
  3. drop old table
  4. Rename new table created.

First of all – it these steps are correct?
Second – is there more beautiful way to this operation?

Best Answer

  1. SHOW CREATE TABLE real; -- to avoid typos
  2. edit it change name to new and to add DATA_DIRECTORY= ... or whatever
  3. perform the CREATE TABLE new ...
  4. INSERT INTO new SELECT * FROM real; -- single, fast, step. But not instantaneous
  5. RENAME TABLE real TO old, new TO real;
  6. DROP TABLE old;

Step 5 is the main added "elegance". It is atomic and instantaneous; it won't interrupt the live system. Also, it give you a chance to test the new real before DROP TABLE old.

If this is InnoDB, you must have innodb_file_per_table ON before step 3. Was it ON when the old real was created? If not, ibdata1 will not free up any space on the old disk. Resolving this takes several more steps.

(My steps should work fine for a MyISAM table.)