美文网首页
MySQL Shell 1:介绍与理解

MySQL Shell 1:介绍与理解

作者: 轻松的鱼 | 来源:发表于2020-12-24 14:43 被阅读0次

    简介

    MySQL Shell 是 MySQL Server 的高级客户端和代码编辑器。除了提供的类似于 MySQL 的 SQL 功能外,MySQL Shell 还提供JavaScript 和 Python 脚本功能,并包括与 MySQL 一起使用的 API。

    MySQL Shell 包含以下用 JavaScript 和 Python 实现的 API:

    • X DevAPI

    当 MySQL Shell 使用X协议连接到MySQL服务器时,X DevAPI 可以让我们使用文档型数据和关系型数据。文档链接:
    Chapter 20, Using MySQL as a Document Store
    X DevAPI User Guide

    • AdminAPI
      AdminAPI 可以让我们管理 InnoDB Cluster、ReplicaSet

    • ShellAPI
      包含有关 shell 和 util 全局对象的信息,以及可在MySQL服务器上执行SQL的 mysql 模块。

    安装

    开箱即用,下载地址:
    https://dev.mysql.com/downloads/shell/

    下载解压后,执行 mysqlsh 就进入 mysql shell 界面了:

    [root@10-186-63-93 ~]# mysqlsh
    MySQL Shell 8.0.19
    
    Copyright (c) 2016, 2019, 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.
     MySQL  JS >
    

    命令

    MySQL shell 提供的命令可以让我们修改代码编辑器的执行环境,比如连接到 MySQL Server。这些命令都以转义符" "开头,因为需要独立于执行模式,与语言无关(sql、js、py)。\help 命令可以列出这些命令:

    MySQL  JS > \help
    

    MySQL shell 中模块对象众多,使用也比较复杂,我当前关注的是 dba、AdminAPI、mysqlx、X DevAPI、mysql 这几个模块、API,足够我用来创建管理 InnoDB Cluster、ReplicaSet、文档型数据就行,下面分别介绍。

    全局对象

    Shell 启动时,可以使用以下模块和对象:

    • dba:用于InnoDB Cluster、ReplicaSet 管理;
    • mysql:支持使用经典 MySQL 协议连接到 MySQL 服务器,允许执行 SQL;
    • mysqlx:用于通过 MySQL X DevAPI 处理 X 协议会话;
    • os:允许访问允许与操作系统交互的功能;
    • session:代表当前打开的MySQL会话;
    • shell:允许访问通用功能和属性;
    • sys:允许访问系统特定的参数;
    • util:对诸如升级检查器和JSON导入之类的各种工具进行了分组。

    AdminAPI

    简单的说 AdminAPI 提供管理 InnoDB cluster、InnoDB ReplicaSet 的功能,包含 dba、Cluster、ReplicaSet 对象。使用 \help 命令查看如何使用:

    JS > \help AdminAPI
    ... 
    For more information about the dba object use: \? dba 
    In the AdminAPI, an InnoDB cluster is represented as an instance of the Cluster class, while replicasets are represented as an instance of the ReplicaSet class. 
    For more information about the Cluster class use: \? Cluster 
    For more information about the ReplicaSet class use: \? ReplicaSet 
    ...
    

    就像帮助信息里说的,可以分别再用 \help xxx 方式查看更具体的帮助文档:

    JS > \help dba 
    JS > \help Cluster 
    JS > \help ReplicaSet
    

    如果要看以上每个对象、类的命令列表,可以用通配符,比如列出所有 dba 对象的命令:

    JS > \help dba*
    Found several entries matching dba*
    
    The following topics were found at the AdminAPI category:
    
    - dba
    - dba.checkInstanceConfiguration
    - dba.configureInstance
    - dba.configureLocalInstance
    - dba.configureReplicaSetInstance
    - dba.createCluster
    - dba.createReplicaSet
    - dba.deleteSandboxInstance
    - dba.deploySandboxInstance
    - dba.dropMetadataSchema
    - dba.getCluster
    - dba.getReplicaSet
    - dba.help
    - dba.killSandboxInstance
    - dba.rebootClusterFromCompleteOutage
    - dba.startSandboxInstance
    - dba.stopSandboxInstance
    - dba.upgradeMetadata
    - dba.verbose
    
    For help on a specific topic use: \? <topic>
    
    e.g.: \? dba
    

    如果要更详细的某个命令的帮助手册,则可以 \help 后接具体的命令:

    JS > \help dba.createCluster
    

    可以看出 dba 对象命令可以创建 Innodb Cluster、ReplicaSet。

    创建后,dba.getCluster() 和 dba.getReplicaSet() 获取管理对象,然后 Cluster、ReplicaSet 对象提供具体的管理 Innodb Cluster、ReplicaSet 的命令,可以用 \help 命令简单查看:

    JS > \help Cluster*
    Found several entries matching Cluster*
    
    The following topics were found at the AdminAPI category:
    
    - Cluster
    - Cluster.addInstance
    - Cluster.checkInstanceState
    - Cluster.describe
    - ...
    JS > \help Replica*
    Found several entries matching Replica*
    
    The following topics were found at the AdminAPI category:
    
    - ReplicaSet
    - ReplicaSet.addInstance
    - ReplicaSet.disconnect
    - ReplicaSet.forcePrimaryInstance
    - ReplicaSet.getName
    ...
    

    X DevAPI

    X DevAPI 是 mysqlx 模块上包含的函数和类的集合,当 mysql shell 启动时,该模块会自动加载。要使用X DevAPI,必须:

    1. MySQL Server 启用 X Protocol
    2. 连接到 X Protocol 定义的端口

    X DevAPI 的简化了将 MySQL 作为文档型数据库存取数据的使用,功能有:

    1. 与启用X协议的MySQL服务器建立会话
    2. 管理 schema
    3. 管理集合
    4. 对集合(文档型数据)和表(关系型数据)的CRUD操作

    使用 \help 可以查看:

    JS > \help X DevAPI
    ...
    To work on a MySQL Server with the X DevAPI, start by creating a session using:
    mysqlx.getSession(...).
    
    For more details about the mysqlx module use: \? mysqlx
    
    For more details about how to create a session with the X DevAPI use: \?
    ...
    
    JS > \help mysqlx
    ...
    CLASSES
     - BaseResult       Base class for the different types of results returned by
                        the server.
     - Collection       A Collection is a container that may be used to store
                        Documents in a MySQL database.
     - CollectionAdd    Operation to insert documents into a Collection.
     - CollectionFind   Operation to retrieve documents from a Collection.
    ...
     - Table            Represents a Table on an Schema, retrieved with a session
                        created using mysqlx module.
     - TableDelete      Operation to delete data from a table.
     - TableInsert      Operation to insert data into a table.
    ... 
    

    mysql模块

    mysql 模块支持使用经典 MySQL 协议连接到 MySQL 服务器,允许执行 SQL(切换到 SQL 模式)。

    比如可以用 help 查看 SQL 命令使用方式:

    JS > \help SQL Syntax/SELECT
    Syntax:
    SELECT
        [ALL | DISTINCT | DISTINCTROW ]
          [HIGH_PRIORITY]
          [STRAIGHT_JOIN]
          [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
          [SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
        select_expr [, select_expr ...]
    ...    
    

    切换到 SQL 模式,可以执行所有 SQL 命令:

    JS > \sql
    Switching to SQL mode... Commands end with ;
    MySQL  172.16.22.2:3306 ssl  SQL > show databases;
    +-------------------------------+
    | Database                      |
    +-------------------------------+
    | hucq                          |
    | information_schema            |
    | mysql                         |
    | mysql_innodb_cluster_metadata |
    | mysqlslap                     |
    | performance_schema            |
    | sys                           |
    | world_x                       |
    +-------------------------------+
    8 rows in set (0.0041 sec)
    

    相关文章

      网友评论

          本文标题:MySQL Shell 1:介绍与理解

          本文链接:https://www.haomeiwen.com/subject/trsmnktx.html