MySQL Shell

MySQL Shell是MySQL服务器的统一脚本接口。它支持JavaScript和Python脚本。JavaScript是默认的处理模式。在大多数情况下,你需要一个账户来连接到本地的MySQL服务器实例。

启动MySQL Shell
安装并启动MySQL server后,将MySQL Shell连接到server实例。默认情况下,MySQL Shell使用X协议连接。

在运行server实例的系统上,打开一个终端窗口,并使用以下命令启动MySQL Shell:

mysqlsh name@localhost/world_x
Creating a Session to 'root@localhost/world_x'
Enter password: ****

您可能需要指定适当的路径。

另外:
.name表示MySQL帐户的用户名。
.MySQL Shell提示您输入密码。
.这个会话的默认模式是world_x数据库。

mysql-js>提示符表明此会话的活动语言是JavaScript。

[root@localhost ~]# mysqlsh root@localhost/world_x
Please provide the password for 'root@localhost': ******
Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No): N
MySQL Shell 8.0.41

Copyright (c) 2016, 2025, 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.
Creating a session to 'root@localhost/world_x'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 61 (X protocol)
Server version: 5.7.26-log Source distribution
Default schema `world_x` accessible through db.
 MySQL  localhost:33060+  world_x  JS > 

当不带host参数运行mysqlsh时,MySQL Shell会尝试连接运行在localhost接口上的33060端口上的服务器实例。

MySQL Shell支持如下的输入行编辑:
.左箭头键和右箭头键在当前输入行内水平移动。
.向上箭头键和向下箭头键在先前输入的行集合中向上和向下移动。
.退格键删除光标之前的字符,输入新的字符将在光标位置输入。
.Enter输入当前输入行。

获取MySQL Shell的帮助
在命令解释器的提示符下输入mysqlsh –help以获取命令行选项列表。

[root@localhost ~]# mysqlsh --help
MySQL Shell 8.0.41

Copyright (c) 2016, 2025, 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.

Usage: mysqlsh [OPTIONS] [URI]
       mysqlsh [OPTIONS] [URI] -f  [

在MySQL Shell提示符下输入\help,查看可用命令及其描述。

 MySQL  localhost:33060+  world_x  JS > \help
The Shell Help is organized in categories and topics. To get help for a
specific category or topic use: \? 

The  argument should be the name of a category or a topic.

The pattern is a filter to identify topics for which help is required, it can
use the following wildcards:

- ? matches any single character.
- * matches any character sequence.

The following are the main help categories:

 - AdminAPI       The AdminAPI is an API that enables configuring and managing
                  InnoDB Clusters, ReplicaSets, ClusterSets, among other
                  things.
 - Shell Commands Provides details about the available built-in shell commands.
 - ShellAPI       Contains information about the shell and util global objects
                  as well as the mysql module that enables executing SQL on
                  MySQL Servers.
 - SQL Syntax     Entry point to retrieve syntax help on SQL statements.
 - X DevAPI       Details the mysqlx module as well as the capabilities of the
                  X DevAPI which enable working with MySQL as a Document Store

The available topics include:

- The dba global object and the classes available at the AdminAPI.
- The mysqlx module and the classes available at the X DevAPI.
- The mysql module and the global objects and classes available at the
  ShellAPI.
- The functions and properties of the classes exposed by the APIs.
- The available shell commands.
- Any word that is part of an SQL statement.
- Command Line - invoking built-in shell functions without entering interactive
  mode.

SHELL COMMANDS

The shell commands allow executing specific operations including updating the
shell configuration.

The following shell commands are available:

 - \                   Start multi-line input when in SQL mode.
 - \connect    (\c)    Connects the shell to a MySQL server and assigns the
                       global session.
 - \disconnect         Disconnects the global session.
 - \edit       (\e)    Launch a system editor to edit a command to be executed.
 - \exit               Exits the MySQL Shell, same as \quit.
 - \help       (\?,\h) Prints help information about a specific topic.
 - \history            View and edit command line history.
 - \js                 Switches to JavaScript processing mode.
 - \nopager            Disables the current pager.
 - \nowarnings (\w)    Don't show warnings after every statement.
 - \option             Allows working with the available shell options.
 - \pager      (\P)    Sets the current pager.
 - \py                 Switches to Python processing mode.
 - \quit       (\q)    Exits the MySQL Shell.
 - \reconnect          Reconnects the global session.
 - \rehash             Refresh the autocompletion cache.
 - \show               Executes the given report with provided options and
                       arguments.
 - \source     (\.)    Loads and executes a script from a file.
 - \sql                Executes SQL statement or switches to SQL processing
                       mode when no statement is given.
 - \status     (\s)    Print information about the current global session.
 - \system     (\!)    Execute a system shell command.
 - \use        (\u)    Sets the active schema.
 - \warnings   (\W)    Show warnings after every statement.
 - \watch              Executes the given report with provided options and
                       arguments in a loop.

GLOBAL OBJECTS

The following modules and objects are ready for use when the shell starts:

 - db      Used to work with database schema objects.
 - dba     Used for InnoDB Cluster, ReplicaSet, and ClusterSet administration.
 - mysql   Support for connecting to MySQL servers using the classic MySQL
           protocol.
 - mysqlx  Used to work with X Protocol sessions using the MySQL X DevAPI.
 - os      Gives access to functions which allow to interact with the operating
           system.
 - plugins Plugin to manage MySQL Shell plugins
 - session Represents the currently open MySQL session.
 - shell   Gives access to general purpose functions and properties.
 - sys     Gives access to system specific parameters.
 - util    Global object that groups miscellaneous tools like upgrade checker
           and JSON import.

For additional information on these global objects use: .help()

EXAMPLES
\? AdminAPI
      Displays information about the AdminAPI.

\? \connect
      Displays usage details for the \connect command.

\? checkInstanceConfiguration
      Displays usage details for the dba.checkInstanceConfiguration function.

\? sql syntax
      Displays the main SQL help categories.
 MySQL  localhost:33060+  world_x  JS > 

请输入\help和命令名,获取MySQL Shell命令的详细帮助。例如,要查看\connect命令的帮助信息,输入:

 MySQL  localhost:33060+  world_x  JS > \help \connect
NAME
      \connect - Connects the shell to a MySQL server and assigns the global
      session.

SYNTAX
      \connect [] 
      \c [] 

DESCRIPTION
      TYPE is an optional parameter to specify the session type. Accepts the
      following values:

      - --mc, --mysql: create a classic MySQL protocol session (default port
        3306)
      - --mx, --mysqlx: create an X protocol session (default port 33060)
      - --ssh : create an SSH tunnel to use as a gateway for db
        connection. This requires that db port is specified in advance.

      If TYPE is omitted, automatic protocol detection is done, unless the
      protocol is given in the URI.

      URI format is: [user[:password]@]hostname[:port] and SSHURI format is:
      [user@]hostname[:port]

EXAMPLE
      \connect --mx root@localhost
            Creates a global session using the X protocol to the indicated URI.
 MySQL  localhost:33060+  world_x  JS > 

退出MySQL Shell
输入如下命令退出MySQL Shell:

 MySQL  localhost:33060+  world_x  JS > \quit