博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux / mysql: is it safe to copy mysql db files with cp command from one db to another?
阅读量:5288 次
发布时间:2019-06-14

本文共 3558 字,大约阅读时间需要 11 分钟。

Copying is very simple for MyISAM and completely 100% risky (near suicidal) with InnoDB.

From your question, you brought up

cp /db1/mytable.frm /db2/mytable.frm

MyISAM

This is OK to do. However, you cannot just move the .frm. You must move all components. From you question, let's take a table called db1.mytable. In a normal installation, the table is located in /var/lib/mysql/db1. There would be three files making up the table.

  • /var/lib/mysql/db1/mytable.frm
  • /var/lib/mysql/db1/mytable.MYD (Table Database)
  • /var/lib/mysql/db1/mytable.MYI (Table Indexes)

You must move all three file to move the one table. If all your tables use the MyISAM storage engine, you can shutdown mysql and copy away. If you are simply making a copy of the table and placing it in another database, you should do that using SQL.

For example, if you want to copy db1.mytable to database db2, do this:

CREATE TABLE db2.mytable LIKE db1.mytable;ALTER TABLE db2.mytable DISABLE KEYS;INSERT INTO db2.mytable SELECT * FROM db1.mytable;ALTER TABLE db2.mytable ENABLE KEYS;

Now if you just moving the table from db1 to db2, you can do this:

ALTER TABLE db1.mytable RENAME db2.mytable;

InnoDB

Copying is very dangerous because of the infrastructure that InnoDB works under. There are two basic infrastructures: 1) innodb_file_per_table disabled and 2) innodb_file_per_table enabled

The Achilles' Heel of InnoDB is the system tablespace file known as ibdata1 (normally located in /var/lib/mysql). ?

  • Table Data Pages
  • Table Index Pages
  • Table MetaData (tablespace id management list)
  •  Data (to support Transaction Isolation and )

InnoDB (innodb_file_per_table disabled)

With innodb_file_per_table disabled, all these types of InnoDB info live within ibdata1. The only manifestation of any InnoDB table outside of ibdata1 is the .frm file of the InnoDB table. Copying all InnoDB data at once requires copying all of /var/lib/mysql.

Copying an individual InnoDB table is total impossible. You must mysqldump to extract a dump of the table as a logical representation of the data and its corresponding index definitions. You would then load that dump to another database on the same server or another server.

InnoDB (innodb_file_per_table enabled)

With innodb_file_per_table enabled, table data and its indexes live in the database folder next to the .frm file. For example, for the table db1.mytable, the manifestation of that InnoDB table outside of ibdata1 would be:

  • /var/lib/mysql/db1/mytable.frm
  • /var/lib/mysql/db1/mytable.ibd

All the metadata for db1.mytable still resides in ibdata1 and there is absolutely no way around that. Redo logs and MVCC data also still live with ibdata1.

WARNING (or DANGER as )

If you are thinking of just copying the .frm and .ibd file, you are in line for world of hurting. Copying the .frm and .ibd file of an InnoDB table is only good if you can guarantee that the tablespace id of the .ibd file matches exactly with the tablespace id entry in the metdata of the ibdata1 file.

I wrote two posts in DBA StackExchange about this tablespace id concept

  •  (under the heading 'Restoring Databases')

Here is excellent link on how to reattach and .ibd file to ibdata1 in the event of mismatched tablespace ids : . After reading this, you should be able to see why I said near suicidal.

For InnoDB you only need to this

CREATE TABLE db2.mytable LIKE db1.mytable;INSERT INTO db2.mytable SELECT * FROM db1.mytable;

to make a copy of an InnoDB table. If you are migrating it to another DB server, use mysqldump.

来源: <>
 

转载于:https://www.cnblogs.com/jins-note/p/9513165.html

你可能感兴趣的文章
.net 基础面试题二
查看>>
leetcode 347. Top K Frequent Elements
查看>>
nil、Nil、NULL和NSNull的理解
查看>>
app后端设计(12)--图片的处理
查看>>
FTP上传下载文件
查看>>
maven build无反应,报terminated
查看>>
关于View控件中的Context选择
查看>>
mediaplayer state
查看>>
C# DataTable 详解
查看>>
2018icpc徐州OnlineA Hard to prepare
查看>>
spark_load csv to hive via hivecontext
查看>>
R语言-rnorm函数
查看>>
Spark的启动进程详解
查看>>
Java 字符终端上获取输入三种方式
查看>>
javascript 简单工厂
查看>>
java调用oracle存储过程,返回结果集
查看>>
使用命令创建数据库和表
查看>>
数据库的高级查询
查看>>
HttpClient(一)-- HelloWorld
查看>>
dump调试函数
查看>>