MySQL Shell的介绍以及安装

 更新时间:2021年4月23日 15:00  点击:1967

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的方法:

1、首先使用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'@'%'"})

2、使用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当中。

3、使用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。

4、此时我们使用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。

5.搭建好副本集之后,查看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的资料请关注猪先飞其它相关文章!

[!--infotagslink--]

相关文章

  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • php webshell下直接反弹shell的例子

    webshell对于我们站长来讲肯定听到比较多了,我们网站可能经常被人使用期webshell方式注入一些东西了,下面一起来看一个php webshell下直接反弹shell的例子,具体如下。...2016-11-25
  • 详解Mysql中的JSON系列操作函数

    新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
  • Powershell实现编写和运行脚本

    本文为那些对学习 Windows PowerShell 命令行和脚本编写环境感兴趣的系统管理员提供了资源。也请告诉我们本网站如何才能对您更有用处。...2020-06-30
  • 深入研究mysql中的varchar和limit(容易被忽略的知识)

    为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • mysql的3种分表方案

    一、先说一下为什么要分表:当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短查询时间。根据个人经验,mysql执行一个sql的过程如下:1...2014-05-31
  • Shell脚本中让进程休眠的方法(sleep用法)

    这篇文章主要介绍了Shell脚本中让进程休眠的方法,本文讲解的就是sleep的用法,可以实现睡觉若干秒、若干分钟、若干小时,需要的朋友可以参考下...2020-07-11
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • Centos5.5中安装Mysql5.5过程分享

    这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅Mysql5.5.37安装需要cmake,5.6版本开始都需要cmake来编译,5.5以后的版本应该也要装这个。安装cmake复制代码 代码如下: [root@local ~]# wget http://www.cm...2015-03-15
  • 用VirtualBox构建MySQL测试环境

    宿主机使用网线的时候,客户机在Bridged Adapter模式下,使用Atheros AR8131 PCI-E Gigabit Ethernet Controller上网没问题。 宿主机使用无线的时候,客户机在Bridged Adapter模式下,使用可选项里唯一一个WIFI选项,Microsoft Virtual Wifi Miniport Adapter也无法上网,故弃之。...2013-09-19
  • Linux 下使用shell脚本定时维护数据库的案例

    这篇文章主要介绍了Linux 下使用shell脚本定时维护数据库,本文通过案例分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
  • 忘记MYSQL密码的6种常用解决方法总结

    首先要声明一点,大部分情况下,修改MySQL密码是需要有mysql里的root权限的...2013-09-11
  • MySQL数据库备份还原方法

    MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:/Program Files/MySQL/MySQL Server 4.1/bin (或者直接将windows的环境变量path中添加该目录) ...2013-09-26
  • shell脚本实战-while循环语句

    这篇文章主要介绍了shell脚本实战-while循环语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-09
  • Shell字符串比较相等、不相等方法小结

    这篇文章主要介绍了Shell字符串比较相等、不相等方法小结,本文直接给出代码,但代码中含有大量注释,需要的朋友可以参考下...2020-07-11
  • Mysql命令大全(详细篇)

    一、连接Mysql格式: mysql -h主机地址 -u用户名 -p用户密码1、连接到本机上的MYSQL。首先打开DOS窗口,然后进入目录mysql/bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是密...2015-11-08
  • Navicat for MySQL 11注册码\激活码汇总

    Navicat for MySQL注册码用来激活 Navicat for MySQL 软件,只要拥有 Navicat 注册码就能激活相应的 Navicat 产品。这篇文章主要介绍了Navicat for MySQL 11注册码\激活码汇总,需要的朋友可以参考下...2020-11-23
  • 自动设置安卓手机wifi代理的PowerShell脚本

    这篇文章主要介绍了自动设置安卓手机wifi代理的PowerShell脚本,帮助大家进行抓包测试,感兴趣的朋友可以了解下...2020-10-17
  • node.js如何操作MySQL数据库

    这篇文章主要介绍了node.js如何操作MySQL数据库,帮助大家更好的进行web开发,感兴趣的朋友可以了解下...2020-10-29