DB2与MySQL数据转移
2008-02-17 15:49:50 来源:WEB开发网核心提示:MySQL 数据转移MySQL 提供的用于数据转移的实用程序主要有两个,它们是 mysqldump 和 mysqlhotcopy,DB2与MySQL数据转移(2),尽管这两个实用程序主要用来进行备份和恢复,但是它们可以用来建立现有表或整个数据库的拷贝,mysqlhotcopy 不具有 mysqldump 那么大的灵活性
MySQL 数据转移
MySQL 提供的用于数据转移的实用程序主要有两个。它们是 mysqldump 和 mysqlhotcopy。尽管这两个实用程序主要用来进行备份和恢复,但是它们可以用来建立现有表或整个数据库的拷贝,因此能够将数据从一个数据库(常常是中小规模的数据库)转移到同一服务器或其他服务器中的另一个数据库中。选择 mysqldump 还是 mysqlhotcopy 取决于要复制的数据库的规模、设置的成本、锁机制、恢复选项、表的类型等等。表 1 对比了 MySQL 提供的这两个实用程序。
尽管有许多建立数据库或部分数据库的拷贝的方法,比如操作系统级复制、select ... into outfile、mysqlsnapshot 以及 InnoDB 的热备份,但是本文只关注 mysqldump 和 mysqlhotcopy。各种方法之间的比较及其优缺点超出了本文的范围。
例如,对于 mysqldump,要想建立整个数据库的拷贝,最常见的方法可能是发出以下命令:
清单 1. 使用 mysqldump 创建完整的数据库拷贝
mysqldump --opt db_name > backup-file.sql
导入它的方法是发出以下命令:
清单 2. 使用 mysqldump 导入完整的数据库
mysql db_name < backup-file.sql
清单 3 中列出了 mysqldump 支持的一些重要标志。发出 mysqldump -? 命令可以获得完整的列表。
清单 3. mysqldump 选项
-A, --all-databases Dump all the databases. This will be same as --databases
with all databases selected.
--add-drop-database Add a 'DROP DATABASE' before each create.
--add-drop-table Add a 'drop table' before each create.
--add-locks Add locks around insert statements.
--allow-keywords Allow creation of column names that are keywords.
--character-sets-dir=name
Directory where character sets are.
-c, --complete-insert
Use complete insert statements.
-C, --compress Use compression in server/client protocol.
--create-options Include all MySQL specific create options.
-B, --databases To dump several databases. Note the difference in usage;
In this case no tables are given. All name arguments are
regarded as databasenames. 'USE db_name;' will be
included in the output.
Set the default character set.
--delayed-insert Insert rows with INSERT DELAYED;
--delete-master-logs
Delete logs on master after backup. This automatically
enables --master-data.
-e, --extended-insert
Allows utilization of the new, much faster INSERT syntax.
--fields-terminated-by=name
Fields in the textfile are terminated by ...
--fields-enclosed-by=name
Fields in the importfile are enclosed by ...
--fields-optionally-enclosed-by=name
Fields in the i.file are opt. enclosed by ...
--fields-escaped-by=name
Fields in the i.file are escaped by ...
--order-by-primary Sorts each table's rows by primary key, or first unique
key, if such a key exists. Useful when dumping a MyISAM
table to be loaded into an InnoDB table, but will make
the dump itself take considerably longer.
--single-transaction
Creates a consistent snapshot by dumping all tables in a
single transaction. Works ONLY for tables stored in
storage engines which support multiversioning (currently
only InnoDB does); the dump is NOT guaranteed to be
consistent for other storage engines. Option
automatically turns off --lock-tables.
-T, --tab=name Creates tab separated textfile for each table to given
path. (creates .sql and .txt files). NOTE: This only
works if mysqldump is run on the same machine as the
mysqld daemon.
--tables Overrides option --databases (-B).
--triggers Dump triggers for each dumped table
-X, --xml Dump a database as well formed XML.
尽管 mysqldump 适合对中小规模的表和数据库进行备份,但是 mysqlhotcopy 的能力更强。不过要注意,mysqlhotcopy 不具有 mysqldump 那么大的灵活性。要记住一点,它只能在数据库目录所在的机器上运行。
更多精彩
赞助商链接