iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL Shell的介绍以及安装
  • 291
分享到

MySQL Shell的介绍以及安装

MySQLshellMySQLshell安装 2022-05-17 14:05:51 291人浏览 安东尼
摘要

01 ReplicaSet的架构     前面的文章中,我们说了ReplicaSet的基本概念和限制以及部署前的基本知识。今天我们来看InnoDB ReplicaSet部署过程中的两个重要组件之一

01 ReplicaSet的架构

    前面的文章中,我们说了ReplicaSet的基本概念和限制以及部署前的基本知识。今天我们来看InnoDB ReplicaSet部署过程中的两个重要组件之一的Mysql shell,为了更好的理解mysql Shell,画了一张图,如下:  

通过上面的图,不难看出,Mysql Shell是运维人员管理底层MySQL节点的入口,也就是DBA执行管理命令的地方,而MySQL Router是应用程序连接的入口,它的存在,让底层的架构对应用程序透明,应用程序只需要连接MySQL Router就可以和底层的数据库打交道,而数据库的主从架构,都是记录在MySQL Router的原信息里面的。

今天,我们主要来看MySQL Shell的搭建过程。

02 MySQL Shell的介绍以及安装

   MySQL Shel是一个客户端工具,用于管理Innodb Cluster或者Innodb ReplicaSet,可以简单理解成ReplicaSet的一个入口。

    它的安装过程比较简单:在MySQL官网下载对应版本的MySQL Shell即可。地址如下:

https://downloads.mysql.com/arcHives/shell/

这里使用8.0.20版本

下载完毕之后,在linux服务器进行解压,然后就可以通过这个MySQL Shell来连接线上的MySQL服务了。

我的线上MySQL地址分别是:

192.168.1.10  5607

192.168.1.20  5607

可以直接通过下面的命令来连接MySQL服务:


/usr/local/mysql-shell-8.0.20/bin/mysqlsh '$user'@'$host':$port --passWord=$pass

成功连接之后的日志如下:


MySQL Shell 8.0.20

Copyright (c) 2016, 2020, oracle and/or its affiliates. All rights reserved.
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.
WARNING: Using a password on the command line interface can be insecure.
Creating a session to 'superdba@10.185.13.195:5607'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 831
Server version: 8.0.19 MySQL CommUnity Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  192.168.1.10:5607 ssl  js > 
 MySQL  192.168.1.10:5607 ssl  JS > 
 MySQL  192.168.1.10:5607 ssl  JS > 
 MySQL  192.168.1.10:5607 ssl  JS > 

03 MySQL Shell连接数据库并创建ReplicaSet

   上面已经介绍了使用MySQL Shell连接数据库的方法了,现在我们来看利用MySQL Shell来创建ReplicaSet的方法:

首先使用dba.configureReplicaSetInstance命令来配置副本集,并创建副本集的管理员。


MySQL  192.168.1.10:5607 ssl  JS > dba.configureReplicaSetInstance('root@192.168.1.10:5607',{clusterAdmin:"'rsadmin'@'%'"})
Configuring MySQL instance at 192.168.1.10:5607 for use in an InnoDB ReplicaSet...

This instance reports its own address as 192.168.1.10:5607
WARNING: User 'rsadmin'@'%' already exists and will not be created. However, it is missing privileges.
The account 'rsadmin'@'%' is missing privileges required to manage an InnoDB cluster:
GRANT REPLICATION_APPLIER ON *.* TO 'rsadmin'@'%' WITH GRANT OPTION;
Dba.configureReplicaSetInstance: The account 'root'@'192.168.1.10' is missing privileges required to manage an InnoDB cluster. (RuntimeError)

可以看到,上面的命令中,我们配置了副本集的一个实例:192.168.1.10:5607,并创建了一个管理员账号rsadmin,同时这个管理员拥有clusterAdmin的权限。

返回的结果中,有一个报错信息,它提示我们登陆的root账号少了replication_applier的权限,因此无法使用root账号对rsadmin账号授权。我们给root账号补充replication_applier权限之后,重新执行上面的命令,结果如下:


MySQL  192.168.1.10:5607 ssl  JS > dba.configureReplicaSetInstance('root@192.168.1.10:5607',{clusterAdmin:"'rsadmin'@'%'"})
Configuring MySQL instance at 192.168.1.10:5607 for use in an InnoDB ReplicaSet...

This instance reports its own address as 192.168.1.10:5607
User 'rsadmin'@'%' already exists and will not be created.

The instance '192.168.1.10:5607' is valid to be used in an InnoDB ReplicaSet.

The instance '192.168.1.10:5607' is already ready to be used in an InnoDB ReplicaSet.

这次执行成功了。

我们登陆到底层的192.168.1.10上,查看rsadmin账号,可以发现,账号已经生成了,信息如下:


select user,host,concat(user,"@'",host,"'"),authentication_string from mysql.user where user like "%%rsadmin";
+---------+------+----------------------------+-------------------------------------------+
| user    | host | concat(user,"@'",host,"'") | authentication_string                     |
+---------+------+----------------------------+-------------------------------------------+
| rsadmin | %    | rsadmin@'%'                | *2090992BE9B9B27D89906C6CB13A8512DF49E439 |
+---------+------+----------------------------+-------------------------------------------+
1 row in set (0.00 sec)

show grants for  rsadmin@'%';
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for rsadmin@%                                                                                                                                                                                                                                                        |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, RELOAD, SHUTDOWN, PROCESS, FILE, SUPER, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE USER ON *.* TO `rsadmin`@`%` WITH GRANT OPTION                                                                                                                 |
| GRANT BACKUP_ADMIN,CLONE_ADMIN,PERSIST_RO_VARIABLES_ADMIN,SYSTEM_VARIABLES_ADMIN ON *.* TO `rsadmin`@`%` WITH GRANT OPTION                                                                                                                                                  |
| GRANT INSERT, UPDATE, DELETE ON `mysql`.* TO `rsadmin`@`%` WITH GRANT OPTION                                                                                                                                                                                                |
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `mysql_innodb_cluster_metadata`.* TO `rsadmin`@`%` WITH GRANT OPTION          |
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `mysql_innodb_cluster_metadata_bkp`.* TO `rsadmin`@`%` WITH GRANT OPTION      |
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `mysql_innodb_cluster_metadata_previous`.* TO `rsadmin`@`%` WITH GRANT OPTION |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
6 rows in set (0.00 sec)

注意,如果我们加入的副本集实例是当前连接的实例,那么也可以使用简单的写法:

dba.configureReplicaSetInstance('',{clusterAdmin:"'rsadmin'@'%'"})

使用dba.createReplicaSet命令创建副本集,并将结果保存在一个变量里面,如下:


MySQL  192.168.1.10:5607 ssl  JS > var rs = dba.createReplicaSet("yeyz_test")
A new replicaset with instance '192.168.1.10:5607' will be created.

* Checking MySQL instance at 192.168.1.10:5607

This instance reports its own address as 192.168.1.10:5607
192.168.1.10:5607: Instance configuration is suitable.

* Updating metadata...

ReplicaSet object successfully created for 192.168.1.10:5607.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.

可以看到,我们创建了一个yeyz_test的副本集,并将结果保存在变量rs当中。

使用rs.status()查看当前的副本集成员


MySQL  192.168.1.10:5607 ssl  JS > rs.status()
{
    "replicaSet": {
        "name": "yeyz_test",
        "primary": "192.168.1.10:5607",
        "status": "AVaiLABLE",
        "statusText": "All instances available.",
        "topology": {
            "192.168.1.10:5607": {
                "address": "192.168.1.10:5607",
                "instanceRole": "PRIMARY",
                "mode": "R/W",
                "status": "ONLINE"
            }
        },
        "type": "ASYNC"
    }
}

这里面,可以看到,当前ReplicaSet里面已经有192.168.1.10:5607这个实例的,他的状态是available,他的角色是Primary。

此时我们使用rs.addInstance命令加入第2个节点,并使用rs.status查看状态。

这里需要注意,加入第二个节点的时候,有一个数据同步的过程,这个数据同步有2中策略:

策略一:全量恢复

使用MySQL Clone组件,然后使用克隆快照来覆盖新实例上面的所有数据。这种方法非常适合空白实例加入到Innodb 副本集中。

策略二:增量恢复

它依赖MySQL的复制功能,将所有的丢失的事务复制到新实例上,如果新实例上的事务很少,则这个过程会很快。这个方法需要保证集群中至少存在一个实例,它保存了这些缺失事务的binlog,如果缺失的事务的binlog已经清理,则这个方法不能使用。

当一个实例加入一个集群的时候,MySQL Shell会自动尝试挑选一个合适的策略来同步数据,不需要人为干预,如果它无法安全的选择同步方法,则会提供给DBA一个选项,让你选择是通过Clone或者增量同步的方法来实现数据同步。

下面的例子中,就是通过自动选择增量同步的方法来同步数据的:


MySQL  192.168.1.10:5607 ssl  JS > rs.addInstance("192.168.1.20:5607")
WARNING: Concurrent execution of ReplicaSet operations is not supported because the required MySQL lock service UDFs could not be installed on instance '10.41.28.127:5607'.
Make sure the MySQL lock service plugin is available on all instances if you want to be able to execute some operations at the same time. The operation will continue without concurrent execution support.

Adding instance to the replicaset...

* PerfORMing validation checks

This instance reports its own address as 192.168.1.20:5607
192.168.1.20:5607: Instance configuration is suitable.

* Checking async replication topology...

* Checking transaction state of the instance...
The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of '192.168.1.20:5607' 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'.

Incremental state recovery was selected because it seems to be safely usable.

* Updating topology
** Configuring 192.168.1.20:5607 to replicate from 192.168.1.10:5607
** Waiting for new instance to synchronize with PRIMARY...

The instance '192.168.1.20:5607' was added to the replicaset and is replicating from 192.168.1.20:5607.

MySQL  192.168.1.10:5607 ssl  JS >
MySQL  192.168.1.10:5607 ssl  JS > rs.status()
{
    "replicaSet": {
        "name": "yeyz_test",
        "primary": "192.168.1.10:5607",
        "status": "AVAILABLE",
        "statusText": "All instances available.",
        "topology": {
            "192.168.1.10:5607": {
                "address": "192.168.1.10:5607",
                "instanceRole": "PRIMARY",
                "mode": "R/W",
                "status": "ONLINE"
            },
            "192.168.1.20:5607": {
                "address": "192.168.1.20:5607",
                "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"
    }
}

加入第二个节点之后,可以看到,再次使用rs.status来查看副本集的结构,可以看到Secondary节点已经出现了,就是我们新加入的192.168.1.20:5607

当然我们可以分别使用下面的命令查看更详细的输出:

rs.status({extended:0})

rs.status({extended:1})

rs.status({extended:2})

不同的级别,显示的信息有所不同,等级越高,信息约详细。

这里不得不说一个小的bug,官方文档建议写法是:

ReplicaSet.status(extended=1)

原文如下:

The output of ReplicaSet.status(extended=1) is very similar to Cluster.status(extended=1), but the main difference is that the replication field is always available because InnoDB ReplicaSet relies on MySQL Replication all of the time, unlike InnoDB Cluster which uses it during incremental recovery. For more information on the fields, see Checking a cluster's Status with Cluster.status().

但是实际操作过程中,这种写法会报错,如下:


MySQL  192.168.1.10:5607 ssl  JS > sh.status(extended=1)
You are connected to a member of replicaset 'yeyz_test'.
ReplicaSet.status: Argument #1 is expected to be a map (ArgumentError)

不知道算不算一个bug。

搭建好副本集之后,查看primary节点的元信息库表,并在primary写入数据,查看数据是否可以同步。


[(none)] 17:41:10>show databases;
+-------------------------------+
| Database                      |
+-------------------------------+
| information_schema            |
| mysql                         |
| mysql_innodb_cluster_metadata |
| performance_schema            |
| sys                           |
| zjmdmm                        |
+-------------------------------+
6 rows in set (0.01 sec)

[(none)] 17:41:29>use mysql_innodb_cluster_metadata
Database changed
[mysql_innodb_cluster_metadata] 17:45:12>show tables;
+-----------------------------------------+
| Tables_in_mysql_innodb_cluster_metadata |
+-----------------------------------------+
| async_cluster_members                   |
| async_cluster_views                     |
| clusters                                |
| instances                               |
| router_rest_accounts                    |
| routers                                 |
| schema_version                          |
| v2_ar_clusters                          |
| v2_ar_members                           |
| v2_clusters                             |
| v2_gr_clusters                          |
| v2_instances                            |
| v2_router_rest_accounts                 |
| v2_routers                              |
| v2_this_instance                        |
+-----------------------------------------+
15 rows in set (0.00 sec)

[mysql_innodb_cluster_metadata] 17:45:45>select * from routers;
Empty set (0.00 sec)

[(none)] 17:45:52>create database yeyazhou;
Query OK, 1 row affected (0.00 sec)

可以看到,Primary节点上有一个元信息数据库mysql_innodb_cluster_metadata,里面保存了一些原信息,我们查看了router表,发现里面没有数据,原因是我们没有配置MySQL Router。后面的文章中会写到MySQL Router的配置过程。

    在Primary上创建一个数据库yeyazhou,可以发现,在从库上也已经出现了对应的DB,


192.168.1.20 [(none)] 17:41:41>show databases;
+-------------------------------+
| Database                      |
+-------------------------------+
| information_schema            |
| mysql                         |
| mysql_innodb_cluster_metadata |
| performance_schema            |
| sys                           |
| yeyazhou                      |
| zjmdmm                        |
+-------------------------------+
7 rows in set (0.00 sec)

说明副本集的复制关系无误。

至此,整个MySQL Shell连接MySQL实例并创建ReplicatSet的过程搭建完毕。

下一篇文章讲述MySQL Router的搭建过程,以及如何使用MySQL Router来访问底层的数据库。

以上就是MySQL Shell的介绍以及安装的详细内容,更多关于MySQL Shell的资料请关注自学编程网其它相关文章!

您可能感兴趣的文档:

--结束END--

本文标题: MySQL Shell的介绍以及安装

本文链接: https://www.lsjlt.com/news/10311.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MySQLShell的介绍以及安装
    目录01 ReplicaSet的架构02 MySQL Shell的介绍以及安装03 MySQL Shell连接数据库并创建ReplicaSet01 ReplicaSet的架...
    99+
    2024-04-02
  • XpmJS的介绍以及安装过程
    XpmJS的介绍以及安装过程,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、XpmJS 是啥XpmJS可以链接任何云端资源,为小程序、移动应用提供云资源通道和后端能力。降...
    99+
    2023-06-19
  • MySQL的安装介绍
    本篇内容介绍了“MySQL的安装介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!MySQL 安装本教程的...
    99+
    2024-04-02
  • miniconda3介绍、安装以及使用教程
    目录简单来说conda有什么用?对于pip、conda、anaconda和miniconda的区别。安装环境配置测试安装第三方库Pycharm使用conda环境vscode使用con...
    99+
    2023-02-14
    miniconda安装 miniconda的安装与配置 miniconda安装教程
  • pycocotools介绍以及在windows10下的安装过程
    目录前言pycocotools库的介绍pycocotools库的安装pycocotools安装失败 compiler_compat/ld项目场景问题描述原因分析解决方案总结前言 最近...
    99+
    2023-02-22
    pycocotools介绍 windows10下安装pycocotools pycocotools安装
  • mongodb的介绍以及安装用法是怎样的
    本篇文章为大家展示了mongodb的介绍以及安装方法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。一、mongodb的介绍1.什么是MongoDBMongoDB是由...
    99+
    2024-04-02
  • MySQL数据库的介绍及安装方法
    本篇内容介绍了“MySQL数据库的介绍及安装方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!  一、My...
    99+
    2024-04-02
  • 数据库介绍以及mysql的安装配置,超详细教程
    数据库–基础 数据库相关概念 数据库 存储数据的仓库,数据是有组织的进行存储英文:DataBase,简称DB 数据库管理系统 管理数据库的大型软件英文:DataBase Management,简称DBMS SQL 英文:Structured...
    99+
    2023-12-25
    数据库 mysql microsoft
  • Pandas的介绍及安装方法
    这篇文章主要介绍“Pandas的介绍及安装方法”,在日常操作中,相信很多人在Pandas的介绍及安装方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Pandas的介绍及安装方法”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-01
  • mysql死锁介绍以及解决
    什么是死锁 死锁是2+个线程在执行过程中, 因争夺资源而造成的相互等待的现象,若无外力作用,它们将无法推进下去。 死锁产生的4个必要条件 互斥条件 指进程对所分配的资源进行排他性使用,即一段时间内某资源只有一个进程占用,其他的进程请求资源只...
    99+
    2023-08-17
    mysql java 微服务
  • Jupyter 介绍、安装及使用
    Jupyter 介绍、安装及使用 一.Jupyter介绍 Jupyter Notebook是一个开源的web应用程序,可以使用它来创建和共享包含实时代码、方程、可视化和文本的文档。 Jupyter ...
    99+
    2023-09-05
    python
  • MySQL事务的介绍以及示例分析
    这篇文章给大家介绍MySQL事务的介绍以及示例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1.什么是事务:事务是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时事务是做为...
    99+
    2024-04-02
  • Redis数据库的安装部署以及基本操作介绍
    本篇内容介绍了“Redis数据库的安装部署以及基本操作介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Redis数据库概述Redis是一个...
    99+
    2023-06-20
  • mysql中的MMM的介绍以及部署方法
    这期内容当中小编将会给大家带来有关mysql中的MMM的介绍以及部署方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。一、 MMM 简介:MMM 即 Multi-Mast...
    99+
    2024-04-02
  • MySQL源码安装的方法介绍
    本篇内容主要讲解“MySQL源码安装的方法介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL源码安装的方法介绍”吧! M...
    99+
    2024-04-02
  • jsonp的简单介绍以及其安全风险
    目录JSONP介绍JSONP劫持利用JSONP绕过token防护进行csrf攻击JSONP劫持挖掘与防御参考总结JSONP介绍 说起跨域请求资源的方法,最常见的方法是JSONP/CO...
    99+
    2024-04-02
  • Tushare介绍、安装及使用教程
            本人是一个二本大数据的学生,想未来从事数据分析师的岗位。虽然说路漫漫道阻且长,但是我还是想跟大家分享一下平时做一些数据分析喜欢用的数据源,如果大家看完我的文章后,有什么不好的地方欢迎大家在评论区写下宝贵的意见,我看到都会积极...
    99+
    2023-10-08
    python
  • MySQL - 安装、连接、简单介绍
    1、安装 MySQL8.0 安装MySQL 8.0的步骤,以 Windows 为例: 1.1 下载MySQL Installer: 需要从MySQL官方网站下载MySQL Installer。在下载页面中,选择适用于Windows的MySQ...
    99+
    2023-08-16
    mysql 数据库
  • fedora 12下安装mysql的步骤及相关目录介绍
    本篇内容主要讲解“fedora 12下安装mysql的步骤及相关目录介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“fedora 12下安装mysql的步骤及...
    99+
    2024-04-02
  • mysql三种安装方式介绍
    这篇文章主要讲解了“mysql三种安装方式介绍”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql三种安装方式介绍”吧! MYSQL版本介绍 ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作