• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 将实例添加到副本集

    创建副本集后,可以使用该ReplicaSet.addInstance()操作将实例添加为副本集当前主副本的只读辅助副本。因此,在此操作过程中,副本集的主要对象必须可访问且可用。使用自动创建的带有随机密码的MySQL帐户,在添加的实例和主实例之间配置MySQL复制。将实例添加到副本集后,该操作将等待新添加的实例应用所有未决的事务。此过程称为分布式恢复,MySQL Shell支持使用该recoveryMethod选项配置的不同方法。

    MySQL Shell使用用于获取ReplicaSethandle对象的相同用户名和密码连接到目标实例。副本集的所有实例均应具有相同的管理员帐户,并具有相同的授予和密码。使用配置实例时,可以创建具有所需授权的自定义管理员帐户dba.configureReplicaSetInstance()。请参阅配置InnoDB副本集实例。

    当实例加入副本集时,使用分布式恢复的方式与InnoDB集群中使用的方式几乎相同。这意味着您可以在MySQL克隆和增量恢复之间进行选择。有关更多信息,请参见“将MySQL克隆与InnoDB集群一起使用”。本节介绍了将实例添加到副本集时的区别。

    先决条件

    为了使实例能够加入副本集,必须满足以下先决条件。会自动检查它们ReplicaSet.addInstance(),如果发现任何问题,操作将失败。

    • 与二进制日志和复制相关的选项必须至少已通过验证,并且也可能由dba.configureReplicaSetInstance()

    如果选择的恢复方法是增量恢复:

    • 在要添加的实例上设置的事务不能包含主数据库中不存在的事务
    • 在添加实例上设置的事务一定不能丢失
    • 从主数据库的二进制日志中清除的事务

    如果要添加到副本集的主实例和实例上都可以使用MySQL Clone,则可以使用clone作为恢复方法来克服上面列出的先决条件。

    将实例添加到副本集

    创建副本集并将其分配给变量后,请使用该操作将辅助实例添加到副本集。您可以将实例指定为类似URI的连接字符串,请参见“使用类似URI的字符串或键值对连接到服务器”。您指定的用户必须具有必需的特权,并且在副本集中的所有实例上都必须相同,请参阅“配置InnoDB副本集实例”。如果要检查操作,但实际上不做任何更改,请使用该选项。这显示了MySQL Shell将采取什么行动将实例添加到副本集。ReplicaSet.addInstance(instance)dryRun

    例如,rs-2使用user 添加实例rsadmin,发出:

    mysql-js> rs.addInstance('rsadmin@rs-2')
    
    Adding instance to the replicaset...
    
    * Performing validation checks
    
    This instance reports its own address as rsadmin@rs-2
    rsadmin@rs-2: Instance configuration is suitable.
    
    * Checking async replication topology...
    
    * Checking transaction state of the instance...
    
    NOTE: The target instance 'rsadmin@rs-2' has not been pre-provisioned (GTID set
    is empty). The Shell is unable to decide whether replication can completely
    recover its state.  The safest and most convenient way to provision a new
    instance is through automatic clone provisioning, which will completely
    overwrite the state of 'rsadmin@rs-2' with a physical snapshot from an existing
    replicaset member. To use this method by default, set the 'recoveryMethod'
    option to 'clone'.
    
    WARNING: It should be safe to rely on replication to incrementally recover the
    state of the new instance if you are sure all updates ever executed in the
    replicaset were done with GTIDs enabled, there are no purged transactions and
    the new instance contains the same GTID set as the replicaset or a subset of it.
    To use this method by default, set the 'recoveryMethod' option to 'incremental'.
    Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone):
    

    在这种情况下,我们没有指定恢复方法,因此该操作会建议您如何最好地进行。在此示例中,我们选择克隆选项,因为加入副本集的实例上没有任何现有事务。因此,没有从联接实例中删除数据的风险。

    Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone): C
    * Updating topology
    Waiting for clone process of the new member to complete. Press ^C to abort the operation.
    * Waiting for clone to finish...
    NOTE: rsadmin@rs-2 is being cloned from rsadmin@rs-1
    ** Stage DROP DATA: Completed
    ** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed
    ** Stage RECOVERY: \
    NOTE: rsadmin@rs-2 is shutting down...
    
    * Waiting for server restart... ready
    * rsadmin@rs-2 has restarted, waiting for clone to finish...
    * Clone process has finished: 59.63 MB transferred in about 1 second (~1.00 B/s)
    
    ** Configuring rsadmin@rs-2 to replicate from rsadmin@rs-1
    ** Waiting for new instance to synchronize with PRIMARY...
    
    The instance 'rsadmin@rs-2' was added to the replicaset and is replicating from rsadmin@rs-1.
    

    假设实例对于InnoDB ReplicaSet用法有效,则分布式恢复将继续进行。在这种情况下,新加入的实例使用MySQL Clone从主数据库复制它尚未应用的所有事务,然后它将副本集加入为在线实例。要验证,请使用以下ReplicaSet.status()操作:

    mysql-js> rs.status()
    {    
        "replicaSet": {
            "name": "example", 
            "primary": "rs-1:3306", 
            "status": "AVAILABLE", 
            "statusText": "All instances available.", 
            "topology": {
                "rs-1:3306": {
                    "address": "rs-1:3306", 
                    "instanceRole": "PRIMARY", 
                    "mode": "R/W", 
                    "status": "ONLINE"
                }, 
                "rs-2:3306": {
                    "address": "rs-2:3306", 
                    "instanceRole": "SECONDARY", 
                    "mode": "R/O", 
                    "replication": {
                        "applierStatus": "APPLIED_ALL", 
                        "applierThreadState": "Slave has read all relay log; waiting for more updates", 
                        "receiverStatus": "ON", 
                        "receiverThreadState": "Waiting for master to send event", 
                        "replicationLag": null
                    }, 
                    "status": "ONLINE"
                }
            }, 
            "type": "ASYNC"
        }
    }
    

    此输出显示,example现在命名的副本集由两个MySQL实例组成,主要副本为rs-1。当前在处有一个辅助实例rs-2,它是主实例的副本。副本集处于联机状态,这意味着主副本和辅助副本是同步的。此时,副本集已准备就绪,可以处理事务。

    如果要覆盖交互式MySQL Shell模式,尝试选择最合适的恢复方法,请使用该recoveryMethod选项配置实例如何恢复能够加入副本集所需的数据。有关更多信息,请参见“将MySQL克隆与InnoDB集群一起使用”。