为 NDB 集群进行复制准备包括以下步骤:Preparing the NDB Cluster for replication consists of the following steps:
检查所有 MySQL 服务器的版本兼容性(参见 第 25.7.2 节,“NDB 集群复制的一般要求”)。Check all MySQL servers for version compatibility (see Section 25.7.2, “General Requirements for NDB Cluster Replication”).
在源集群上使用具有相应权限的账户创建复制账户,可使用以下两个 SQL 语句:Create a replication account on the source Cluster with the appropriate privileges, using the following two SQL statements:
mysql
S>CREATE USER '->replica_user'@'replica_host'IDENTIFIED BY 'mysqlreplica_password';S>GRANT REPLICATION SLAVE ON *.*->TO 'replica_user'@'replica_host';在前面的语句中,
replica_user是复制账户的用户名,replica_host是副本的主机名或 IP 地址,replica_password是为该账户分配的密码。In the previous statement,replica_useris the replication account user name,replica_hostis the host name or IP address of the replica, andreplica_passwordis the password to assign to this account.例如,要创建一个名为
myreplica的复制用户账户,从名为replica-host的主机登录,并使用密码53cr37,请使用以下CREATE USER和GRANT语句:For example, to create a replica user account with the namemyreplica, logging in from the host namedreplica-host, and using the password53cr37, use the followingCREATE USERandGRANTstatements:mysql
S>CREATE USER 'myreplica'@'replica-host'->IDENTIFIED BY '53cr37';mysqlS>GRANT REPLICATION SLAVE ON *.*->TO 'myreplica'@'replica-host';出于安全原因,最好为复制账户使用唯一的用户账户——不要用于任何其他目的。For security reasons, it is preferable to use a unique user account—not employed for any other purpose—for the replication account.
设置副本以使用源。使用 mysql 客户端,可以通过
CHANGE REPLICATION SOURCE TO语句来完成: Set up the replica to use the source. Using the mysql client, this can be accomplished with theCHANGE REPLICATION SOURCE TOstatement:mysql
R>CHANGE REPLICATION SOURCE TO->SOURCE_HOST='->source_host',SOURCE_PORT=->source_port,SOURCE_USER='->replica_user',SOURCE_PASSWORD='replica_password';在前述语句中,
source_host是复制源的主机名或 IP 地址,source_port是副本连接到源时使用的端口,replica_user是在源上为副本设置的用户名,而replica_password是在前一步中为该用户帐户设置的密码。In the previous statement,source_hostis the host name or IP address of the replication source,source_portis the port for the replica to use when connecting to the source,replica_useris the user name set up for the replica on the source, andreplica_passwordis the password set for that user account in the previous step.例如,要告诉副本使用主机名为
rep-source的 MySQL 服务器,并使用在上一步创建的复制账户,请使用以下语句:For example, to tell the replica to use the MySQL server whose host name isrep-sourcewith the replication account created in the previous step, use the following statement:mysql
R>CHANGE REPLICATION SOURCE TO->SOURCE_HOST='rep-source',->SOURCE_PORT=3306,->SOURCE_USER='myreplica',->SOURCE_PASSWORD='53cr37';有关此语句中可使用的完整选项列表,请参阅 第 15.4.2.2 节,“CHANGE REPLICATION SOURCE TO 语句”。For a complete list of options that can be used with this statement, see Section 15.4.2.2, “CHANGE REPLICATION SOURCE TO Statement”.
为了提供复制备份功能,您还需要在启动复制过程之前,在从库的
my.cnf文件中添加--ndb-connectstring选项。详情请参见第 25.7.9 节,“使用 NDB 集群复制的 NDB 集群备份”。To provide replication backup capability, you also need to add an--ndb-connectstringoption to the replica'smy.cnffile prior to starting the replication process. See Section 25.7.9, “NDB Cluster Backups With NDB Cluster Replication”, for details.有关可以在从库的
my.cnf中设置的其他选项,请参见第 19.1.6 节,“复制和二进制日志选项及变量”。For additional options that can be set inmy.cnffor replicas, see Section 19.1.6, “Replication and Binary Logging Options and Variables”.如果源集群已经在使用中,您可以创建源的备份,并将其加载到副本上,以减少副本与源同步所需的时间。如果副本也运行 NDB 集群,可以使用在第 25.7.9 节,“使用 NDB 集群复制的 NDB 集群备份” 中描述的备份和恢复过程来实现这一点。If the source cluster is already in use, you can create a backup of the source and load this onto the replica to cut down on the amount of time required for the replica to synchronize itself with the source. If the replica is also running NDB Cluster, this can be accomplished using the backup and restore procedure described in Section 25.7.9, “NDB Cluster Backups With NDB Cluster Replication”.
ndb-connectstring=
management_host[:port]如果您在副本上未使用 NDB 集群,可以在源上使用此命令创建备份:In the event that you are not using NDB Cluster on the replica, you can create a backup with this command on the source:
shell
S>mysqldump --source-data=1然后通过将数据转储文件复制到副本上来导入生成的数据转储文件。之后,您可以使用 mysql 客户端将转储文件中的数据导入到副本数据库,如下所示,其中
dump_file是在源端使用 mysqldump 生成的文件名,db_name是要复制的数据库名称:Then import the resulting data dump onto the replica by copying the dump file over to it. After this, you can use the mysql client to import the data from the dumpfile into the replica database as shown here, wheredump_fileis the name of the file that was generated using mysqldump on the source, anddb_nameis the name of the database to be replicated:shell
R>mysql -u root -pdb_name<dump_file有关 mysqldump 可用选项的完整列表,请参阅 第 6.5.4 节,“mysqldump — 数据库备份程序”。For a complete list of options to use with mysqldump, see Section 6.5.4, “mysqldump — A Database Backup Program”.
Note如果以这种方式将数据复制到副本,请确保在所有数据加载完成之前,阻止副本尝试连接源以开始复制。可以通过使用
--skip-replica-start启动副本来实现。数据加载完成后,请按照接下来两个部分中概述的附加步骤进行操作。If you copy the data to the replica in this fashion, make sure that you stop the replica from trying to connect to the source to begin replicating before all the data has been loaded. You can do this by starting the replica with--skip-replica-start. Once the data loading has completed, follow the additional steps outlined in the next two sections.确保每个作为复制源的 MySQL 服务器都分配了唯一的服务器 ID,并启用了基于行的二进制日志记录。(参见 第 19.2.1 节,“复制格式”。)此外,我们强烈建议启用
replica_allow_batching系统变量(默认值)。Ensure that each MySQL server acting as a replication source is assigned a unique server ID, and has binary logging enabled, using the row-based format. (See Section 19.2.1, “Replication Formats”.) In addition, we strongly recommend enabling thereplica_allow_batchingsystem variable (the default).使用
--ndb-replica-batch-size来设置副本写操作的批量大小,而不是使用--ndb-batch-size,以及使用--ndb-replica-blob-write-batch-bytes来代替--ndb-blob-write-batch-bytes以确定复制应用程序用于写入 BLOB 数据的批量大小。所有这些选项都可以在源服务器的my.cnf文件中设置,或者在启动源 mysqld 进程时通过命令行设置。更多信息,请参见 第 25.7.6 节,“启动 NDB 集群复制(单复制通道)”。Use--ndb-replica-batch-sizeto set the batch size used for writes on the replica instead of--ndb-batch-size, and--ndb-replica-blob-write-batch-bytesrather than--ndb-blob-write-batch-bytesto determine the batch size used by the replication applier for writing blob data. All of these options can be set either in the source server'smy.cnffile, or on the command line when starting the source mysqld process. See Section 25.7.6, “Starting NDB Cluster Replication (Single Replication Channel)”, for more information.