热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SettingupanInnoDBClusterWithaFewLinesofCode

However,forsomesmallercompanies,thelearningcurvehamperstheimmediateadoptionofautomationsoftware.ThisiswheretheMySQLShellhelps,byallowingyoutodeployanN-nodeInnoDBClusterorReplicaSetinlessthan60linesofc

Setting up an InnoDB Cluster With a Few Lines of Code In this day and age, large enterprise companies make use of Ansible, Puppet, or Chef to provision MySQL servers, be it replica sets or clusters. This eases the burden of deployment and workflow management.

However, for some smaller companies, the learning curve hampers the immediate adoption of automation software. This is where the MySQL Shell helps, by allowing you to deploy an N-node InnoDB Cluster or ReplicaSet in less than 60 lines of code.

The Requirements

  • Percona Server for MySQL version 8.0.17 or later, preferable version 8.0.19, each node started as standalone servers
  • Percona MySQL Shell 8.0 or equivalent upstream version
  • MySQL root user and password or equivalent user with grant option
  • Hostname configured on each node, can be done with /etc/hosts
10.84.148.18 clusternode3
10.84.148.199 clusternode2
10.84.148.214 clusternode1

Execute the script and supply the data it needs:

[root@clientServer mysqlsh]# mysqlsh -f setup_innodb_cluster.js
InnoDB cluster set up
==================================
Setting up a Percona Server for MySQL - InnoDB cluster.
 
Password for the MySQL root account: **********
Number of data nodes: 3
Hostname for node1: clusternode1
Hostname for node2: clusternode2
Hostname for node3: clusternode3

After this, the script will do the rest of the process to configure the 3-node InnoDB cluster and you will just have to wait until it completes.

Here we can see each node is configured by the dba.configureInstance() function. We’re also creating the clusterAdmin user and password at this stage.

Configuring the instances.
=> Configuring MySQL instance at clusternode1:3306 for use in an InnoDB cluster...
 
This instance reports its own address as clusternode1: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.
 
NOTE: Some configuration options need to be fixed:
+-----------------+---------------+----------------+----------------------------+
| Variable        | Current Value | Required Value | Note                       |
+-----------------+---------------+----------------+----------------------------+
| binlog_checksum | CRC32         | NONE           | Update the server variable |
+-----------------+---------------+----------------+----------------------------+
 
Cluster admin user 'pscluster'@'cluster%' created.
Configuring instance...
The instance 'clusternode1:3306' was configured to be used in an InnoDB cluster.
 
=> Configuring MySQL instance at clusternode2:3306 for use in an InnoDB cluster...
 
This instance reports its own address as clusternode2: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.
 
NOTE: Some configuration options need to be fixed:
+-----------------+---------------+----------------+----------------------------+
| Variable        | Current Value | Required Value | Note                       |
+-----------------+---------------+----------------+----------------------------+
| binlog_checksum | CRC32         | NONE           | Update the server variable |
+-----------------+---------------+----------------+----------------------------+
 
Cluster admin user 'pscluster'@'cluster%' created.
Configuring instance...
The instance 'clusternode2:3306' was configured to be used in an InnoDB cluster.
 
=> Configuring MySQL instance at clusternode3:3306 for use in an InnoDB cluster...
 
This instance reports its own address as clusternode3: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.
 
NOTE: Some configuration options need to be fixed:
+-----------------+---------------+----------------+----------------------------+
| Variable        | Current Value | Required Value | Note                       |
+-----------------+---------------+----------------+----------------------------+
| binlog_checksum | CRC32         | NONE           | Update the server variable |
+-----------------+---------------+----------------+----------------------------+
 
Cluster admin user 'pscluster'@'cluster%' created.
Configuring instance...
The instance 'clusternode3:3306' was configured to be used in an InnoDB cluster.
 
Configuring Instances completed.

At this stage, the InnoDB Cluster is initialized from the first node. At this point the script executes the dba.createCluster() function.

Setting up InnoDB Cluster.
 
A new InnoDB cluster will be created on instance 'clusternode1:3306'.
 
Validating instance configuration at clusternode1:3306...
 
This instance reports its own address as clusternode1:3306
 
Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'clusternode1:33061'. Use the localAddress option to override.
 
Creating InnoDB cluster 'InnoDBCluster' on 'clusternode1:3306'...
 
Adding Seed Instance...
Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.

The next step would be to add the other two instances to the cluster. As you will see it uses the CLONE plugin to copy the data from the first node. Here we use the .addInstance() function to add each instance to the cluster.

Adding instance to the cluster...
 
Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.
 
NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.
 
* Waiting for clone to finish...
NOTE: clusternode2:3306 is being cloned from clusternode1:3306
** Stage DROP DATA: Completed
 
 
 
** Clone Transfer      
FILE COPY  ############################################################  100%  Completed    
PAGE COPY  ############################################################  100%  Completed    
REDO COPY  ############################################################  100%  Completed
 
** Stage RECOVERY: \
NOTE: clusternode2:3306 is shutting down...
 
* Waiting for server restart... ready
* clusternode2:3306 has restarted, waiting for clone to finish...
* Clone process has finished: 59.61 MB transferred in about 1 second (~1.00 B/s)
 
Incremental state recovery is now in progress.
 
* Waiting for distributed recovery to finish...
NOTE: 'clusternode2:3306' is being recovered from 'clusternode1:3306'
* Distributed recovery has finished
 
The instance 'clusternode2:3306' was successfully added to the cluster.
 
 
=>
WARNING: A GTID set check of the MySQL instance at 'clusternode3:3306' determined that it contains transactions that do not originate from the cluster, which must be discarded before it can join the cluster.
 
clusternode3:3306 has the following errant GTIDs that do not exist in the cluster:
2442b908-7356-11ea-b46b-00163e1f9fe5:1-2
 
WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of clusternode3:3306 with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'.
 
Having extra GTID events is not expected, and it is recommended to investigate this further and ensure that the data can be removed prior to choosing the clone recovery method.
Clone based recovery selected through the recoveryMethod option
 
NOTE: Group Replication will communicate with other members using 'clusternode3:33061'. Use the localAddress option to override.
 
Validating instance configuration at clusternode3:3306...
 
This instance reports its own address as clusternode3:3306
 
Instance configuration is suitable.
A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.
 
Adding instance to the cluster...
 
Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.
 
NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.
 
* Waiting for clone to finish...
NOTE: clusternode3:3306 is being cloned from clusternode2:3306
** Stage DROP DATA: Completed
 
 
 
** Clone Transfer      
FILE COPY  ############################################################  100%  Completed    
PAGE COPY  ############################################################  100%  Completed    
REDO COPY  ############################################################  100%  Completed
 
** Stage RECOVERY: \
NOTE: clusternode3:3306 is shutting down...
 
* Waiting for server restart... ready
* clusternode3:3306 has restarted, waiting for clone to finish...
* Clone process has finished: 59.62 MB transferred in about 1 second (~1.00 B/s)
 
State recovery already finished for 'clusternode3:3306'
 
The instance 'clusternode3:3306' was successfully added to the cluster.
 
 
Instances successfully added to the cluster.
 
InnoDB cluster deployed successfully.

And there we have it, a 3-node InnoDB Cluster setup in under five minutes and less than sixty lines of code.

Here’s the source code:

print('InnoDB cluster set up\n');
print('==================================\n');
print('Setting up a Percona Server for MySQL - InnoDB cluster.\n\n');
 
var dbPass = shell.prompt('Password for the MySQL root account: ', { type: "password" });
var numNodes = shell.prompt('Number of data nodes: ');
var dbHosts = [];
 
for (let i = 1; i <= numNodes; i++) {
    var hostName = shell.prompt('Hostname for node' + i + ': ');
    dbHosts.push(hostName);
}
 
function sleep(milliseconds) {
    const date = Date.now();
    let currentDate = null;
    do {
        currentDate = Date.now();
    } while (currentDate - date  ');
        dba.configureInstance('root@' + dbHosts[n] + ':3306', { clusterAdmin: "", clusterAdminPassword: '', password: dbPass, interactive: false, restart: true });
    }
    print('\nConfiguring Instances completed.\n\n');
 
    sleep(5000); // source: https://www.sitepoint.com/delay-sleep-pause-wait/
 
    print('Setting up InnoDB Cluster.\n\n');
    shell.connect({ user: 'root', password: dbPass, host: dbHosts[0], port: 3306 });
 
    var cluster = dba.createCluster("InnoDBCluster");
 
    print('Adding instances to the cluster.\n');
    for (let x = 1; x  ');
        cluster.addInstance('root@' + dbHosts[x] + ':3306', { password: dbPass, recoveryMethod: 'clone' });
    }
    print('\nInstances successfully added to the cluster.\n');
}
 
try {
    setupCluster();
 
    print('\nInnoDB cluster deployed successfully.\n');
} catch (e) {
    print('\nThe InnoDB cluster could not be created.\n');
    print(e + '\n');
}

You can also create a similar script using Python, since MySQL shell supports it as well.

For further reference:

MySQL Shell API: AdminAPI

MySQL :: MySQL 8.0 Reference Manual :: 21 InnoDB Cluster

MySQL :: MySQL Shell 8.0

Github :: setup_innodb_cluster.js

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 我们


推荐阅读
  • MySQL初级篇——字符串、日期时间、流程控制函数的相关应用
    文章目录:1.字符串函数2.日期时间函数2.1获取日期时间2.2日期与时间戳的转换2.3获取年月日、时分秒、星期数、天数等函数2.4时间和秒钟的转换2. ... [详细]
  • Spring Data JdbcTemplate 入门指南
    本文将介绍如何使用 Spring JdbcTemplate 进行数据库操作,包括查询和插入数据。我们将通过一个学生表的示例来演示具体步骤。 ... [详细]
  • 本文详细介绍了如何在 Linux 系统上安装 JDK 1.8、MySQL 和 Redis,并提供了相应的环境配置和验证步骤。 ... [详细]
  • 单元测试:使用mocha和should.js搭建nodejs的单元测试
    2019独角兽企业重金招聘Python工程师标准BDD测试利器:mochashould.js众所周知对于任何一个项目来说,做好单元测试都是必不可少 ... [详细]
  • 本文介绍了如何利用Shell脚本高效地部署MHA(MySQL High Availability)高可用集群。通过详细的脚本编写和配置示例,展示了自动化部署过程中的关键步骤和注意事项。该方法不仅简化了集群的部署流程,还提高了系统的稳定性和可用性。 ... [详细]
  • 在Cisco IOS XR系统中,存在提供服务的服务器和使用这些服务的客户端。本文深入探讨了进程与线程状态转换机制,分析了其在系统性能优化中的关键作用,并提出了改进措施,以提高系统的响应速度和资源利用率。通过详细研究状态转换的各个环节,本文为开发人员和系统管理员提供了实用的指导,旨在提升整体系统效率和稳定性。 ... [详细]
  • 自然语言处理(NLP)——LDA模型:对电商购物评论进行情感分析
    目录一、2020数学建模美赛C题简介需求评价内容提供数据二、解题思路三、LDA简介四、代码实现1.数据预处理1.1剔除无用信息1.1.1剔除掉不需要的列1.1.2找出无效评论并剔除 ... [详细]
  • 本文详细介绍了如何在项目中引入和配置KindEditor网页编辑器,包括脚本引用、初始化编辑器以及文件上传功能的实现。 ... [详细]
  • 如果应用程序经常播放密集、急促而又短暂的音效(如游戏音效)那么使用MediaPlayer显得有些不太适合了。因为MediaPlayer存在如下缺点:1)延时时间较长,且资源占用率高 ... [详细]
  • DAO(Data Access Object)模式是一种用于抽象和封装所有对数据库或其他持久化机制访问的方法,它通过提供一个统一的接口来隐藏底层数据访问的复杂性。 ... [详细]
  • 本文介绍如何使用 Python 的 DOM 和 SAX 方法解析 XML 文件,并通过示例展示了如何动态创建数据库表和处理大量数据的实时插入。 ... [详细]
  • 本文介绍了如何使用 Node.js 和 Express(4.x 及以上版本)构建高效的文件上传功能。通过引入 `multer` 中间件,可以轻松实现文件上传。首先,需要通过 `npm install multer` 安装该中间件。接着,在 Express 应用中配置 `multer`,以处理多部分表单数据。本文详细讲解了 `multer` 的基本用法和高级配置,帮助开发者快速搭建稳定可靠的文件上传服务。 ... [详细]
  • Python错误重试让多少开发者头疼?高效解决方案出炉
    ### 优化后的摘要在处理 Python 开发中的错误重试问题时,许多开发者常常感到困扰。为了应对这一挑战,`tenacity` 库提供了一种高效的解决方案。首先,通过 `pip install tenacity` 安装该库。使用时,可以通过简单的规则配置重试策略。例如,可以设置多个重试条件,使用 `|`(或)和 `&`(与)操作符组合不同的参数,从而实现灵活的错误重试机制。此外,`tenacity` 还支持自定义等待时间、重试次数和异常处理,为开发者提供了强大的工具来提高代码的健壮性和可靠性。 ... [详细]
  • 使用 ListView 浏览安卓系统中的回收站文件 ... [详细]
  • Python 伦理黑客技术:深入探讨后门攻击(第三部分)
    在《Python 伦理黑客技术:深入探讨后门攻击(第三部分)》中,作者详细分析了后门攻击中的Socket问题。由于TCP协议基于流,难以确定消息批次的结束点,这给后门攻击的实现带来了挑战。为了解决这一问题,文章提出了一系列有效的技术方案,包括使用特定的分隔符和长度前缀,以确保数据包的准确传输和解析。这些方法不仅提高了攻击的隐蔽性和可靠性,还为安全研究人员提供了宝贵的参考。 ... [详细]
author-avatar
1234
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有