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). What is contained in that file?

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

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 the Robot would say in Lost in Space)

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

Here is excellent link on how to reattach and .ibd file to ibdata1 in the event of mismatched tablespace ids : http://www.chriscalender.com/?tag=innodb-error-tablespace-id-in-file. 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.

Linux / mysql: is it safe to copy mysql db files with cp command from one db to another?的更多相关文章

  1. 第一次项目上Linux服务器(四:CentOS6下Mysql数据库的安装与配置(转))

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  2. linux centos7 安装常用软件java,node,mysql,Seafile

    linux centos7 安装常用软件java,node,mysql,Seafile 安装压缩解压缩软件 yum install -y unzip zip 安装git yum install -y ...

  3. Linux入门——安装jdk、tomcat、MySQL以及项目部署

    Linux简介     Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和Unix的多用户.多任务. 支持多线程和多CPU的操作系统.伴随着互联网的发展,     Linu ...

  4. linux运维、架构之路-MySQL(二)

    一.SQL语句实战 1.DDL语句——库管理 ①查看数据库 show databases; show databases like 'word%';#模糊查询数据库 ②创建数据库 create dat ...

  5. Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法 + Linux启动/停止/重启Mysql数据库的方法

    启动mysql 报错: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/m ...

  6. Linux系统下 解决Qt5无法连接MySQL数据库的方法

    Linux平台下解决Qt5连接mysql数据库的问题:输入sudo apt-get install libqt5sql5-mysql解决,这种方法只能解决Qt是用sudo apt-get instal ...

  7. linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程

    1.设置任务调度命令crontab 任务调度是指系统在某个时间执行的特定的命令或程序.任务调度分为:1)系统工作:有些重要的工作必须周而复始的执行,如病毒扫描.2)个别用户工作:个别用户可能希望执行某 ...

  8. 【Linux】Zabbix + MPM + msmtp + mutt 监控MySQL + 邮件报警

    Zabbix部署参考博文 http://blog.sina.com.cn/s/blog_5611597901017oe0.html  MPM安装配置参考博文和MPM官网下载地址 http://blog ...

  9. linux Kernell crash dump------kdump 的安装设置+Linux系统崩溃的修复解决过程+mysql+kvm

    http://www.ibm.com/developerworks/cn/linux/l-cn-dumpanalyse/https://www.kernel.org/pub/linux/utils/k ...

随机推荐

  1. Android Fragment向另一个Activity传值

    1.Fragment内: Intent intent=new Intent(getActivity(),ShowDataActivity.class); //参数1:Fragment所依存的Activ ...

  2. Go语言指针

    指针简介 (Pointer)是编程语言中的一个对象,利用地址,它的值直接指向(points to)存在电脑存储器中另一个地方的值.由于通过地址能找到所需的变量单元,可以说,地址指向该变量单元.因此,将 ...

  3. NOIWC 2019 冬眠记【游记】

    在我的blog查看:https://www.wjyyy.top/wc2019 Day -1 上火车了,but手机没电了. Day 0 中午1点左右到了广州东站.接站只有南站和机场有,于是坐了一个多小时 ...

  4. dubbo服务引用与集群容错

    服务引用无非就是做了两件事 将spring的schemas标签信息转换bean,然后通过这个bean的信息,连接.订阅zookeeper节点信息创建一个invoker 将invoker的信息创建一个动 ...

  5. (转)DB2下载地址总结

    原文:https://blog.csdn.net/huozengguang/article/details/58602910 DB2 v8.2,v9.1,v9.5,v9.7下载地址 下列都是完全版包含 ...

  6. MySQL中show语法

    1. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称. 2. show databases; -- 显示mysql中所有数据 ...

  7. 时间格式为yyyymmdd的String类型的时间,计算时间间隔有错误

    时间格式类型为yyyymmdd,并且为String类型,计算时间间隔有误,一直搞不清楚是什么原因.网上百度了许多,时间格式基本都是yyyy-mm-dd这样的时间格式的,但是yyyymmdd这样的时间格 ...

  8. Editplus下载、安装并最佳配色方案(强烈推荐)

    不多说,直接上干货! Editplus下载 第一步:进入官网 https://www.editplus.com/ 第二步:下载 https://www.editplus.com/download.ht ...

  9. Oracle数据库调优总结

    oracle采用物理读和逻辑读,第一次查询数据库采用的是物理读,以后如果使用相同的sql语句查询,那么它会采用逻辑读,直接从内存中读取数据. 采用执行计划查看执行顺序和耗时:一般查看object na ...

  10. 闲话handle和handler

    虽然handle和handler只有一个字符之差,但在计算机的世界里,含义却大相径庭. 1. 先说说handle 北京话说"一边儿玩儿去,玩勺子把儿去","勺子把儿&qu ...