在设置新的 NDB Cluster 实例时,通常需要从现有的 NDB Cluster、MySQL 实例或其他来源导入数据。这些数据最常以以下一种或多种格式存在:It is common when setting up a new instance of NDB Cluster to need to import data from an existing NDB Cluster, instance of MySQL, or other source. This data is most often available in one or more of the following formats:
一个 SQL 转储文件,例如由 mysqldump 生成的文件。可以使用 mysql 客户端导入,如本节后面所示。An SQL dump file such as produced by mysqldump. This can be imported using the mysql client, as shown later in this section.
CSV 文件由 mysqldump 或其他导出程序生成。此类文件可以使用 mysql 客户端中的
NDB导入到LOAD DATA INFILE中,或者使用 NDB Cluster 发行版提供的 ndb_import 工具导入。有关后者的更多信息,请参阅 第 25.5.13 节,“ndb_import — 将 CSV 数据导入 NDB”。A CSV file produced by mysqldump or other export program. Such files can be imported intoNDBusingLOAD DATA INFILEin the mysql client, or with the ndb_import utility provided with the NDB Cluster distribution. For more information about the latter, see Section 25.5.13, “ndb_import — Import CSV Data Into NDB”.使用
NDB管理客户端中的START BACKUP创建的原生NDB备份。要导入原生备份,必须使用随 NDB Cluster 提供的 ndb_restore 程序。有关使用此程序的更多信息,请参见 第 25.5.23 节,“ndb_restore — 恢复 NDB Cluster 备份”。A nativeNDBbackup produced usingSTART BACKUPin theNDBmanagement client. To import a native backup, you must use the ndb_restore program that comes as part of NDB Cluster. See Section 25.5.23, “ndb_restore — Restore an NDB Cluster Backup”, for more about using this program.
从 SQL 文件导入数据时,通常不需要强制使用事务或外键,临时禁用这些功能可以大大加快导入过程。可以使用 mysql 客户端来完成,无论是在客户端会话中,还是通过命令行调用。在 mysql 客户端会话中,你可以使用以下 SQL 语句来执行导入:When importing data from an SQL file, it is often not necessary to enforce transactions or foreign keys, and temporarily disabling these features can speed up the import process greatly. This can be done using the mysql client, either from a client session, or by invoking it on the command line. Within a mysql client session, you can perform the import using the following SQL statements:
SETndb_use_transactions=0; SETforeign_key_checks=0; sourcepath/to/dumpfile; SET ndb_use_transactions=1; SET foreign_key_checks=1;
以这种方式执行导入时,你必须在执行mysql客户端的source命令后重新启用ndb_use_transaction和foreign_key_checks。否则,同一会话中的后续语句可能在没有强制使用事务或外键约束的情况下执行,这可能导致数据不一致。When performing the import in this fashion, you
must enable
ndb_use_transaction and
foreign_key_checks again following execution of
the mysql client's
source command. Otherwise, it is possible for
later statements in same session may also be executed without
enforcing transactions or foreign key constraints, and which could
lead to data inconcsistency.
在系统终端中,你可以使用 mysql 客户端并带上 --init-command 选项来导入 SQL 文件,同时禁用事务和外键的强制执行,方法如下:From the system shell, you can import the SQL file while disabling
enforcement of transaction and foreign keys by using the
mysql client with the
--init-command option, like this:
$> mysql --init-command='SET ndb_use_transactions=0; SET foreign_key_checks=0' < path/to/dumpfile
也可以将数据加载到一个 InnoDB 表中,然后使用 ALTER TABLE ... ENGINE NDB 将其转换为使用 NDB 存储引擎。你应该考虑到,特别是对于许多表,这可能需要进行多次此类操作;此外,如果使用了外键,你必须仔细注意 ALTER TABLE 语句的顺序,因为外键在使用不同 MySQL 存储引擎的表之间不起作用。It is also possible to load the data into an
InnoDB table, and convert it to use
the NDB storage engine afterwards using ALTER
TABLE ... ENGINE NDB). You should take into account, especially
for many tables, that this may require a number of such
operations; in addition, if foreign keys are used, you must mind
the order of the ALTER TABLE statements
carefully, due to the fact that foreign keys do not work between
tables using different MySQL storage engines.
您应该注意,本节之前描述的方法并未针对非常大的数据集或大事务进行优化。如果一个应用程序确实需要在正常操作中处理大事务或许多并发事务,您可能希望增加 MaxNoOfConcurrentOperations 数据节点配置参数的值,该参数会保留更多内存,以允许数据节点在其事务协调器意外停止时接管事务。You should be aware that the methods described previously in this
section are not optimized for very large data sets or large
transactions. Should an application really need big transactions
or many concurrent transactions as part of normal operation, you
may wish to increase the value of the
MaxNoOfConcurrentOperations
data node configuration parameter, which reserves more memory to
allow a data node to take over a transaction if its transaction
coordinator stops unexpectedly.
当对 NDB Cluster 表执行批量 DELETE 或 UPDATE 操作时,你也可能希望这样做。如果可能,尝试让应用程序分块执行这些操作,例如,通过在此类语句中添加 LIMIT。You may also wish to do this when performing bulk
DELETE or
UPDATE operations on NDB Cluster
tables. If possible, try to have applications perform these
operations in chunks, for example, by adding
LIMIT to such statements.
如果数据导入操作由于任何原因未成功完成,您应准备执行任何必要的清理工作,包括可能执行一个或多个 DROP
TABLE 语句、DROP
DATABASE 语句,或两者兼施。不这样做可能会导致数据库处于不一致状态。If a data import operation does not complete successfully, for
whatever reason, you should be prepared to perform any necessary
cleanup including possibly one or more DROP
TABLE statements, DROP
DATABASE statements, or both. Failing to do so may leave
the database in an inconsistent state.