InnoDB ReplicaSet

InnoDB ReplicaSet 由一个主节点和多个从节点构成. 可以使用ReplicaSet对象和AdminAPI操作管理复制集, 例如检查InnoDB复制集的状态, 并在发生故障时手动故障转移到新的主服务器.

与InnoDB cluster类似, MySQL Router 支持针对InnoDB ReplicaSet 的引导, 这意味着可以自动配置MySQL Router以使用 InnoDB ReplicaSet, 而无需手动配置文件. 这使得InnoDB ReplicaSet 成为一种快速简便的方法, 可以启动和运行 MySQL 复制和 MySQL Router, 非常适合扩展读取, 并在不需要InnoDB集群提供高可用性的用例中提供手动故障转移功能.

InnoDB ReplicaSet限制

与InnoDB集群相比, InnoDB ReplicaSet 有几个限制, 包括:

  1. 没有故障自动切换功能. 在主库不可用的情况下, 需要使用AdminApi手动触发故障转移
  2. 无法防止由于意外或者不可用而导致的数据丢失, 发生故障时候没有应用的事务可能有丢失现象
  3. 无法防止意外退出之后的数据不一致现象;在主节点因为网络问题而短暂失联时候, 如果某个从节点提升为主节点, 则可能发生脑裂现象
  4. Innodb 副本集不支持多主模式
  5. Innodb副本集基于异步复制, 不能像 MGR 那样进行流控, 因此读的扩展性会一定程度上受限
  6. 所有 Secondary 都要从 Primary复制, 因此可能会对 Primary 的负载产生一定的影响

部署

和部署 InnoDB cluster的方式相似, 首先配置一些MySQL服务器实例, 运行 InnoDB ReplicaSet 的前提条件

  1. 仅支持MySQL8.0及以上版本;
  2. 仅支持GTID的复制方法
  3. 仅支持row格式的binlog,不支持statement格式的binlog
  4. 不支持复制过滤器
  5. 不允许建立额外的复制通道
  6. 副本集primary节点只有一个,secondary节点可以有多个,MySQL Router会对每个节点进行监控
  7. 副本集必须完全由MySQL Shell管理,不支持在MySQL Shell之外对实例进行配置和更改

下载和安装mysql-shell

访问 https://dev.mysql.com/downloads/shell/ 下载 Linux Generice 版本, 因为这个版本自带支持此版本的Python3. 如果下载rpm包, 在Centos8下安装时会告诉你缺libpython3.9, 可是Centos8自带的版本才到3.6, 自己编译的话还要再装一堆东西, 不如直接用Generic版本.

解压并放到/opt/mysql-shell下

tar xvf mysql-shell-8.0.26-linux-glibc2.12-x86-64bit.tar.gz
cd /opt/
mkdir mysql-shell
cd mysql-shell
mv ~/backup/mysql-shell-8.0.26-linux-glibc2.12-x86-64bit .
ln -s mysql-shell-8.0.26-linux-glibc2.12-x86-64bit latest

也可以软链到 /usr/bin 下, 这样就可以直接用命令行运行了

cd /usr/bin
ln -s /opt/mysql-shell/latest/bin/mysqlsh mysqlsh

部署

运行mysqlsh

$ /opt/mysql-shell/latest/bin/mysqlsh
MySQL Shell 8.0.26 Copyright (c) 2016, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit.
MySQL JS >

设置第一个节点

 MySQL  192.168.255.4:33060+ ssl  JS > dba.configureReplicaSetInstance('',{clusterAdmin:"'repladmin'@'192.168.255.0/24'"});
Dba.configureReplicaSetInstance: Argument #1: Invalid URI: empty. (ArgumentError)
MySQL 192.168.255.4:33060+ ssl JS > dba.configureReplicaSetInstance('admin@192.168.255.4:3306',{clusterAdmin:"'repladmin'@'192.168.255.0/24'"});
Please provide the password for 'admin@192.168.255.4:3306': ************
Save password for 'admin@192.168.255.4:3306'? [Y]es/[N]o/Ne[v]er (default No):
Configuring local MySQL instance listening at port 3306 for use in an InnoDB ReplicaSet... This instance reports its own address as db01:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
Password for new account: *********
Confirm password: ********* applierWorkerThreads will be set to the default value of 4. NOTE: Some configuration options need to be fixed:
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| Variable | Current Value | Required Value | Note |
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET | Update the server variable |
| enforce_gtid_consistency | OFF | ON | Update read-only variable and restart the server |
| gtid_mode | OFF | ON | Update read-only variable and restart the server |
| replica_parallel_type | DATABASE | LOGICAL_CLOCK | Update the server variable |
| replica_preserve_commit_order | OFF | ON | Update the server variable |
| server_id | 1 | <unique ID> | Update read-only variable and restart the server |
+----------------------------------------+---------------+----------------+--------------------------------------------------+ Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y
Cluster admin user 'repladmin'@'192.168.255.0/24' created.
Configuring instance...
The instance 'db01:3306' was configured to be used in an InnoDB ReplicaSet.
Restarting MySQL...
NOTE: MySQL server at db01:3306 was restarted.

创建ReplicaSet

-- 第一次执行未成功, 待恢复连接
MySQL 192.168.255.4:33060+ ssl JS > var rs = dba.createReplicaSet("mytest_db")
Dba.createReplicaSet: MySQL server has gone away (MYSQLSH 2006)
The global session got disconnected..
Attempting to reconnect to 'mysqlx://admin@192.168.255.4:33060'..
The global session was successfully reconnected.
MySQL 192.168.255.4:33060+ ssl JS > rs.status()
TypeError: Cannot read property 'status' of undefined
-- 第二次才成功
MySQL 192.168.255.4:33060+ ssl JS > var rs = dba.createReplicaSet("mytest_db")
A new replicaset with instance 'db01:3306' will be created. * Checking MySQL instance at db01:3306 This instance reports its own address as db01:3306
db01:3306: Instance configuration is suitable. * Updating metadata... ReplicaSet object successfully created for db01:3306.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.
-- 查看rs状态
MySQL 192.168.255.4:33060+ ssl JS > rs.status()
{
"replicaSet": {
"name": "mytest_db",
"primary": "db01:3306",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"db01:3306": {
"address": "db01:3306",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}

初始化另一个节点

Dba.configureReplicaSetInstance: Access denied for user 'admin'@'192.168.255.4' (using password: YES) (MySQL Error 1045)
MySQL 192.168.255.4:33060+ ssl JS > dba.configureReplicaSetInstance('admin@192.168.255.5:3306',{clusterAdmin:"'repladmin'@'192.168.255.0/24'"});
Please provide the password for 'admin@192.168.255.5:3306': ************
Save password for 'admin@192.168.255.5:3306'? [Y]es/[N]o/Ne[v]er (default No):
Configuring MySQL instance at db02:3306 for use in an InnoDB ReplicaSet... This instance reports its own address as db02:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
Password for new account: *********
Confirm password: ********* applierWorkerThreads will be set to the default value of 4. NOTE: Some configuration options need to be fixed:
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| Variable | Current Value | Required Value | Note |
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET | Update the server variable |
| enforce_gtid_consistency | OFF | ON | Update read-only variable and restart the server |
| gtid_mode | OFF | ON | Update read-only variable and restart the server |
| replica_parallel_type | DATABASE | LOGICAL_CLOCK | Update the server variable |
| replica_preserve_commit_order | OFF | ON | Update the server variable |
| server_id | 1 | <unique ID> | Update read-only variable and restart the server |
+----------------------------------------+---------------+----------------+--------------------------------------------------+ Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y
Cluster admin user 'repladmin'@'192.168.255.0/24' created.
Configuring instance...
The instance 'db02:3306' was configured to be used in an InnoDB ReplicaSet.
Restarting MySQL...
NOTE: MySQL server at db02:3306 was restarted.

将另一个节点加入

注意, 两个机器的hostname和IP映射关系必须出现在对方的/etc/hosts里, 否则中间会出错, 因为互相默认是使用hostname来连接的.

 MySQL  192.168.255.4:33060+ ssl  JS > rs.addInstance("192.168.255.5:3306")
Adding instance to the replicaset... * Performing validation checks This instance reports its own address as db02:3306
db02:3306: Instance configuration is suitable. * Checking async replication topology... * Checking transaction state of the instance... NOTE: The target instance 'db02:3306' 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 'db02:3306' 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): 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: db02:3306 is being cloned from db01:3306
** Stage DROP DATA: Completed
** Clone Transfer
FILE COPY #################################################### 100% Compl PAGE COPY #################################################### 100% Compl REDO COPY #################################################### 100% Completed
NOTE: db02:3306 is shutting down... * Waiting for server restart... ready
* db02:3306 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 75.35 MB transferred in about 1 second (~75.35 MB/s) ** Configuring db02:3306 to replicate from db01:3306
** Waiting for new instance to synchronize with PRIMARY... The instance 'db02:3306' was added to the replicaset and is replicating from db01:3306.

检查状态

 MySQL  192.168.255.4:33060+ ssl  JS > rs.status()
{
"replicaSet": {
"name": "mytest_db",
"primary": "db01:3306",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"db01:3306": {
"address": "db01:3306",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
},
"db02:3306": {
"address": "db02:3306",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Waiting for an event from Coordinator",
"applierWorkerThreads": 4,
"receiverStatus": "ON",
"receiverThreadState": "Waiting for source to send event",
"replicationLag": null
},
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}

第二次运行

第二次要先连db, 然后执行var rs = dba.getReplicaSet()读取ReplicaSet

[root@db01 ~]# mysqlsh
MySQL Shell 8.0.26 Copyright (c) 2016, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners. Type '\help' or '\?' for help; '\quit' to exit.
MySQL JS > var rs = dba.get
getCluster() getReplicaSet()
MySQL JS > var rs = dba.getReplicaSet()
Dba.getReplicaSet: An open session is required to perform this operation. (RuntimeError)
MySQL JS > \connect admin@192.168.255.4
Creating a session to 'admin@192.168.255.4'
Please provide the password for 'admin@192.168.255.4': ************
Save password for 'admin@192.168.255.4'? [Y]es/[N]o/Ne[v]er (default No):
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 41 (X protocol)
Server version: 8.0.26 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL 192.168.255.4:33060+ ssl JS > var rs = dba.getReplicaSet()
You are connected to a member of replicaset 'mytest_db'.
MySQL 192.168.255.4:33060+ ssl JS > rs.status()
{
"replicaSet": {
"name": "mytest_db",
"primary": "db01:3306",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"db01:3306": {
"address": "db01:3306",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
},
"db02:3306": {
"address": "db02:3306",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Waiting for an event from Coordinator",
"applierWorkerThreads": 4,
"receiverStatus": "ON",
"receiverThreadState": "Waiting for source to send event",
"replicationLag": null
},
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}

参考

MySQL8.0使用mysqlsh配置主从复制 InnoDB ReplicaSet的更多相关文章

  1. Docker部署Mysql8.0.20并配置主从复制

    1. Linux安装Mysql8.0.20并配置主从复制(一主一从,双主双从)   Linux安装Mysql8.0.20并配置主从复制(一主一从,双主双从) 2. 前提准备 # 创建主从数据库文件夹 ...

  2. Linux安装Mysql8.0.20并配置主从复制(一主一从,双主双从)

    1. 主从复制解释   将主数据库的增删改查等操作记录到二进制日志文件中,从库接收主库日志文件,根据最后一次更新的起始位置,同步复制到从数据库中,使得主从数据库保持一致. 2. 主从复制的作用 高可用 ...

  3. Mysql8.0安装与配置

    最近公司在开发项目时用到了mySql8.0版本,总结出了安装步骤,供需要的开发人员来参考 安装mySql8.0的步骤: 1.先去官网下载mySql8.0版本的安装包 一. 点击:https://dev ...

  4. MySQL8.0.20安装配置+用Navicat连接详细教程(win10,Navicat15)

    MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用 ...

  5. mysql8.0.20安装配置教程

    mysql配置安装教程 1.下载mysql8.0.20安装包 下载地址: https://dev.mysql.com/downloads/mysql/.找到安装包后下载.(官网为英文,如果看不懂的小伙 ...

  6. mysql-8.0.12 安装+配置, Navicat Premium 12 安装+激活

    1.解压到安装目录,如:D:\mysql-8.0.12-winx64 2.将以下配置文件粘贴到安装目录,并保存为my.ini,注意:使用ansi 而非utf-8编码格式保存 [mysql] # 设置m ...

  7. mysql8.0.30主从配置

    安装包下载地址: https://downloads.mysql.com/archives/community/ 1. 解压介质包: # tar xf mysql-8.0.30-linux-glibc ...

  8. Django(八)模型:Mysql8.0日志文件配置开启/关闭、查询

    Mysql日志文件开启.配置.查看 mysql.log是mysql的日志文件,里面记录的对MySQL数据库的操作记录.默认情况下mysql的日志文件没有产生,需要修改mysql的配置文件,步骤如下: ...

  9. Linux中(Ubuntu18.04.x/CentOS)mysql8.0.x安装/配置/部署/启动

    The MySQL Connectors and APIs are the drivers and libraries that you use to connect applications in ...

  10. 用myeclipse连接MySQL8.0时没有配置jar包

    先上测试代码 package testJdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql. ...

随机推荐

  1. [转帖]一图胜千言 -- SQL Server 基准测试

    https://blog.51cto.com/ultrasql/2130487 文章标签基准测试文章分类SQL Server数据库阅读数1116  

  2. [转帖]linux之iftop命令

    https://rumenz.com/rumenbiji/linux-iftop.html Linux安装iftop > yum install iftop -y > iftop 界面如下 ...

  3. [转帖]Linux文件权限除了r、w、x外还有s、t、i、a权限

    https://www.cnblogs.com/hiyang/p/15122714.html setuid 是 set user ID upon execution 再次缩写为suid setgid  ...

  4. Jmeter学习之六_进行https证书处理的工作

    Jmeter 进行https证书处理的工作 背景 继续学习中,想着能够抓取一下https相关的信息 所以计划些一下处理过程 但是感觉自己这一块比较薄弱. 场景设计这一块应该是专业人去搞, 我这边先只是 ...

  5. [转帖]resin的安装与配置

    1.安装jdk 之前装过了,就不做演示了 vim /etc/profile   //注意java的配置文件 JAVA_HOME=/usr/local/jdk1.8/ JAVA_BIN=/usr/loc ...

  6. [转帖]Linux中./configure、make、make install命令详解

    简单来说,make 是编译,make install 是安装.   总结:linux编译安装中configure.make和make install各自的作用  • ./configure是用来检测你 ...

  7. [转帖]SPECjvm测试工具详解

    ARM服务器测试大纲中指定了要使用specjvm测试Java虚拟机性能,所以就上网找开源的测试套. 简介 SPECjvm2008(java虚拟机基准测试)是用来测试java运行环境(JRE)性能的基准 ...

  8. [转帖]kubelet 原理解析四:probeManager

    https://segmentfault.com/a/1190000022163835 概述 在Kubernetes 中,系统和应用程序的健康检查任务是由 kubelet 来完成的,本文主要讨论kub ...

  9. 人均瑞数系列,瑞数 4 代 JS 逆向分析

    声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...

  10. CTF&爬虫:掌握这些特征,一秒识别密文加密方式

    关注微信公众号:K哥爬虫,持续分享爬虫进阶.JS/安卓逆向等技术干货! 前言 爬虫工程师在做加密参数逆向的时候,经常会遇到各种各样的加密算法.编码.混淆,每个算法都有其对应的特征,对于一些较小的网站, ...