MySQL Shell 8.0  /  Configuring MySQL Shell  /  MySQL Shell Commands

4.1 MySQL Shell Commands

MySQL Shell provides commands which enable you to modify the execution environment of the code editor, for example to configure the active programming language or a MySQL Server connection. The following table lists the commands that are available regardless of the currently selected language. As commands need to be available independent of the execution mode, they start with an escape sequence, the \ character.

Command Alias/Shortcut Description

\help

\h or \?

Prints help about MySQL Shell, and can be used to search the online help.

\quit

\q or \exit

Exit MySQL Shell.

\

In SQL mode, begin multiple-line mode. Code is cached and executed when an empty line is entered.

\status

\s

Show the current MySQL Shell status.

\js

Switch execution mode to JavaScript.

\py

Switch execution mode to Python.

\sql

Switch execution mode to SQL.

\connect

\c

Connect to a MySQL Server

\reconnect

Reconnect to the same MySQL Server

\use

\u

Specify the schema to use.

\source

\.

Execute a script file using the active language.

\warnings

\W

Show any warnings generated by a statement.

\nowarnings

\w

Do not show any warnings generated by a statement.

\history

View and edit command line history.

\rehash

Manually update the autocomplete name cache

Help Command

The \help command can be used with or without a parameter. When used without a parameter a general help message is printed including information about the available MySQL Shell commands, global objects and main help categories.

When used with a parameter, the parameter is used to search the available help based on the mode which the MySQL Shell is currently running in. The parameter can be a word, a command, an API function, or part of an SQL statement. The following categories exist:

  • AdminAPI - introduces the dba global object and the InnoDB cluster AdminAPI.

  • Shell Commands - provides details about the available built-in MySQL 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

To search for help on a topic, for example an API function, use the function name as a pattern. You can use the wildcard characters ? to match any single character and * to match multiple characters in a search. The wildcard characters can be used one or more times in the pattern. The following namespaces can also be used when searching for help:

  • dba for AdminAPI

  • mysqlx for X DevAPI

  • mysql for ShellAPI for classic protocol

  • shell for other ShellAPI classes: Shell, Sys, Options

  • commands for MySQL Shell commands

For example to search for help on a topic, issue \help pattern and:

  • use x devapi to search for help on the X DevAPI

  • use \c to search for help on the MySQL Shell \connect command

  • use Cluster or dba.Cluster to search for help on the AdminAPI dba.Cluster() operation

  • use Table or mysqlx.Table to search for help on the X DevAPI Table class

  • when MySQL Shell is running in JavaScript mode, use isView, Table.isView or mysqlx.Table.isView to search for help on the isView function of the Table object

  • when MySQL Shell is running in Python mode, use is_view, Table.is_view or mysqlx.Table.is_view to search for help on the isView function of the Table object

  • when MySQL Shell is running in SQL mode, if a global session to a MySQL server exists SQL help is displayed. For an overview use sql syntax as the search pattern.

Depending on the search pattern provided one or more results could be found. If a single topic matches the pattern searched for, the help is displayed. If multiple topics match the pattern searched for a list of possible help topics is displayed, and you can view the topic using the search pattern displayed.

Connect and Reconnect Commands

The \connect command is used to connect to a MySQL Server using an URI type string. See Connecting using a URI String.

For example:

\connect root@localhost:3306

If a password is required you are prompted for it.

Use the --mysqlx(-mx) option to create a session using the X Protocol to connect to MySQL server instance. For example:

\connect --mysqlx root@localhost:33060

Use the --mysql(-mc) option to create a ClassicSession, enabling you to use the MySQL protocol to issue SQL directly on a server. For example:

\connect --mysql root@localhost:3306

The \reconnect command is specified without any parameters or options. If the connection to the server is lost, you can use the \reconnect command, which makes MySQL Shell try several reconnection attempts for the session using the existing connection parameters. If those attempts are unsuccessful, you can make a fresh connection using the \connect command and specifying the connection parameters.

Status Command

The \status command displays information about the current global connection. This includes information about the server connected to, the character set in use, uptime, and so on.

Source Command

The \source command is used to execute code from a script at a given path. For example:

\source /tmp/mydata.sql

You can execute either SQL, JavaScript or Python code. The code in the file is executed using the active language, so to process SQL code the MySQL Shell must be in SQL mode.

Warning

As the code is executed using the active language, executing a script in a different language than the currently selected execution mode language could lead to unexpected results.

Use Command

The \use command enables you to choose which schema is active, for example:

\use schema_name

The \use command requires a global development session to be active. The \use command sets the current schema to the specified schema_name and updates the db variable to the object that represents the selected schema.

History Command

The \history command lists the commands you have issued previously in MySQL Shell. Issuing \history shows history entries in the order that they were issued with their history entry number, which can be used with the \history delete entry_number command.

The \history command provides the following:

  • Use \history save to save the history manually.

  • Use \history delete entrynumber to delete a history entry with the given number.

  • Use \history delete firstnumber-[lastnumber] to delete history entries within the range of the given entry numbers. If lastnumber is ommitted, history entries are deleted from firstnumber up to the last entry. If lastnumber goes past the last found history entry number, it deletes up to the last entry.

  • Use \history clear to delete the entire history.

For more information, see Section 3.3, “MySQL Shell Code History”.

Rehash Command

When you have disabled the autocomplete name cache feature, use the \rehash command to manually update the cache. For example, after you load a new schema by issuing the \use schema command, issue \rehash to update the autocomplete name cache. After this autocomplete is aware of the names used in the database, and you can autocomplete text such as table names and so on.