Chapter 19 MySQL Shell User Guide

Table of Contents

19.1 MySQL Shell Features
19.2 Getting Started with MySQL Shell
19.2.1 MySQL Shell Connections
19.2.2 MySQL Shell Sessions
19.2.3 MySQL Shell Global Variables
19.2.4 MySQL Shell Code History
19.3 MySQL Shell Code Execution
19.3.1 Interactive Code Execution
19.3.2 Code Autocompletion
19.3.3 Batch Code Execution
19.3.4 Output Formats
19.3.5 Active Language
19.3.6 Batch Mode Made Interactive
19.4 Configuring MySQL Shell
19.4.1 MySQL Shell Commands
19.5 MySQL Shell Utilities
19.6 MySQL Shell Application Log
19.6.1 Application Log
19.7 Customizing MySQL Shell
19.7.1 Working With Start-Up Scripts
19.7.2 Adding Module Search Paths
19.7.3 Customizing the Prompt
19.7.4 Configuring MySQL Shell

This section provides user documentation for MySQL Shell, an advanced client and code editor for MySQL Server. In addition to the provided SQL functionality, similar to mysql, MySQL Shell provides scripting capabilities for JavaScript and Python and includes APIs for working with MySQL. X DevAPI enables you to work with both relational and document data, see Chapter 20, Using MySQL as a Document Store. AdminAPI enables you to work with InnoDB cluster, see Chapter 21, InnoDB Cluster.

This section describes core features of MySQL Shell.

19.1 MySQL Shell Features

The following features are available in MySQL Shell.

Interactive Code Execution

MySQL Shell provides an interactive code execution mode, where you type code at the MySQL Shell prompt and each entered statement is processed, with the result of the processing printed onscreen. Unicode text input is supported if the terminal in use supports it. Color terminals are supported.

Supported Languages

MySQL Shell processes code in the following languages: JavaScript, Python and SQL. Any entered code is processed as one of these languages, based on the language that is currently active. There are also specific MySQL Shell commands, prefixed with \, which enable you to configure MySQL Shell regardless of the currently selected language. For more information see Section 19.4.1, “MySQL Shell Commands”.

Batch Code Execution

In addition to the interactive execution of code, MySQL Shell can also take code from different sources and process it. This method of processing code in a non-interactive way is called Batch Execution.

As batch execution mode is intended for script processing of a single language, it is limited to having minimal non-formatted output and disabling the execution of commands. To avoid these limitations, use the --interactive command-line option, which tells MySQL Shell to execute the input as if it were an interactive session. In this mode the input is processed line by line just as if each line were typed in an interactive session. For more information see Section 19.3.6, “Batch Mode Made Interactive”.

Output Formats

MySQL Shell provides output in different formats depending on how it is used: Tabbed, Table and JSON. For more information see Section 19.3.4, “Output Formats”.

Multiple-line Support

Multiple-line code can be written using a command, enabling MySQL Shell to cache multiple lines and then execute them as a single statement. For more information see Section 19.3.6.1, “Multiple-line Support”.

Application Log

MySQL Shell can be configured to log information about the execution process. For more information see Section 19.6, “MySQL Shell Application Log”.

Supported APIs

MySQL Shell includes the following APIs implemented in JavaScript and Python which you can use to develop code that interacts with MySQL.

  • The X DevAPI enables you to work with both relational and document data when MySQL Shell is connected to a MySQL server using the X Protocol. For more information, see Chapter 20, Using MySQL as a Document Store. For documentation on the concepts and usage of X DevAPI, see X DevAPI User Guide.

  • The AdminAPI enables you to work with InnoDB cluster, which provides an integrated solution for high availability and scalability using InnoDB based MySQL databases, without requiring advanced MySQL expertise. See Chapter 21, InnoDB Cluster.

For specific documentation on the implementation of the APIs see JavaScript and Python.

X Protocol Support

MySQL Shell is designed to provide an integrated command-line client for all MySQL products which support X Protocol. The development features of MySQL Shell are designed for sessions using the X Protocol. MySQL Shell can also connect to MySQL Servers that do not support the X Protocol using the legacy MySQL Protocol. A minimal set of features from the X DevAPI are available for sessions created using the legacy MySQL protocol.

Global Session

Interaction with a MySQL Server is done through a Session object. For Python and JavaScript, a Session can be created through the getSession function of the mysqlx module. If a session is created in JavaScript mode using any of these methods, it is available only in JavaScript mode. The same happens if the session is created in Python mode. These sessions cannot be used in SQL mode.

For SQL Mode, the concept of Global Session is supported by the MySQL Shell. A Global Session is created when the connection information is passed to MySQL Shell using command options, or by using the \connect command.

The Global Session is used to execute statements in SQL mode and the same session is available in both Python or JavaScript modes. When a Global Session is created, a variable called session is set in the scripting languages, so you can execute code in the different languages by switching the active mode.

For more information, see Section 19.2.2, “MySQL Shell Sessions”.

19.2 Getting Started with MySQL Shell

This section describes how to get started with MySQL Shell, explaining how to connect to a MySQL server instance, and how to choose a session type.

19.2.1 MySQL Shell Connections

MySQL Shell can connect to MySQL Server using both the X Protocol and the classic MySQL protocol. The address of the MySQL Server which you want to connect to can be specified using individual parameters, such as user, hostname and port, or using a Uniform Resource Identifier (URI) type string. The following sections describe these connection methods. See Section 4.2.2, “Connecting to the MySQL Server” for more background information.

You can configure the MySQL server instance that MySQL Shell is connected to in the following ways:

These methods all support Section 19.2.1.2, “Connecting using a URI String”.

Important

Regardless of the method you choose to connect it is important to understand how passwords are handled by MySQL Shell. By default connections are assumed to require a password. The password is requested at the login prompt. If the user has a password-less account, which is insecure and not recommended, or if socket peer-credential authentication is in use (for Unix socket connections), you must explicitly specify that no password is provided and the password prompt is not required. To do this, use one of the following methods:

  • If you are connecting using a URI type string, place a : after the user in the URI type string but do not specify a password after it.

  • If you are connecting using individual parameters, either specify the --no-password option, or specify the --password= option with an empty value.

If you do not specify parameters for a connection the following defaults are used:

  • user defaults to the current system user name

  • host defaults to localhost

  • port defaults to the X Plugin port 33060 when using an X Protocol session, and port 3306 when using a Classic session

MySQL Shell connections using X Protocol can use either TCP or Unix sockets as of version 8.0.4. Previous versions use only TCP for X Protocol connections and using Unix sockets is not supported. MySQL Shell connections using MySQL Protocol default to using Unix sockets when the following conditions are met:

  • A TCP port is not specified

  • A host name is not specified or it is equal to localhost

  • A socket is provided with a path to a socket file

  • A classic session is specified

If a host name is specified but it is not localhost, a TCP connection is established. In this case, if a TCP port is not specified the default value of 3306 is used. If the conditions are met for a socket connection but a path to a socket file is not specified then the default socket is used. See Section 4.2.2, “Connecting to the MySQL Server”.

If the connection to the server is lost, MySQL Shell does not attempt to reconnect automatically. You can use the MySQL Shell \reconnect command, which makes MySQL Shell try several reconnection attempts for the current global session with the previously supplied parameters.

19.2.1.1 Valid Connection Options

This section describes the options available when connecting to MySQL. The options can be specified as:

  • a URI type string, such as myuser@example.com:3306/main-schema

  • a data dictionary of key and value pairs, such as {user:myuser, host:example.com, port:3306, schema:main-schema}

The following options are valid for use in either a URI type string or in a dictionary:

  • scheme: specifies the connection protocol to use, currently either mysql for classic MySQL protocol and mysqlx for X Protocol.

  • user: specifies the MySQL user account to be used for the authentication process.

  • password: specifies the password to be used for the authentication process.

    Warning

    Storing the password in the URI type string is insecure and not recommended.

  • host: specifies the server instance the connection refers to. Can be either an IPv4 address, an IPv6 address or a hostname. If not specified, localhost is used by default.

  • port: specifies a network port which the target MySQL server is listening on for connections. If not specified, 33060 is used by default for X Protocol connections, and 3306 is the default for classic MySQL protocol connections.

  • socket: path to a Unix socket or Windows named-pipe. Values are local file paths and must be encoded in URI type strings, using percent encoding or surrounding the path with parentheses, which removes the need to percent encode characters such as the common directory separator /. For example, to connect as root@localhost using the Unix socket /tmp/mysqld.sock either specify the path using parenthesis as root@localhost?socket=(/tmp/mysqld.sock) or using percent encoding as root@localhost?socket=%2Ftmp%2Fmysqld.sock%2F.

  • schema: specifies the database to be set as default when the connection is established.

  • ?attribute=value: specifies a data dictionary that contains options.

The connection options are case insensitive and can only be defined once. If an option is defined more than once, an error is generated.

In addition to the options listed in the previous list, the following options are also valid when a dictionary is used:

  • dbUser: alias for user.

  • dbPassword: alias for password.

  • ssl-mode: the SSL mode to be used for the connection.

  • ssl-ca: the path to the X509 certificate authority in PEM format.

  • ssl-capath: the path to the directory that contains the X509 certificates authorities in PEM format.

  • ssl-cert: The path to the X509 certificate in PEM format.

  • ssl-key: The path to the X509 key in PEM format.

  • ssl-crl: The path to file that contains certificate revocation lists.

  • ssl-crlpath: The path to the directory that contains certificate revocation list files.

  • ssl-cipher: the SSL cipher to use.

  • tls-version: List of protocols permitted for secure connections

  • auth-method: Authentication method

  • get-server-public-key: Request public key from the server required for RSA key pair-based password exchange. Use when connecting to MySQL 8.0 servers over classic MySQL sessions with SSL mode DISABLED.

  • server-public-key-path: The path name to a file containing a client-side copy of the public key required by the server for RSA key pair-based password exchange. Use when connecting to MySQL 8.0 servers over classic MySQL sessions with SSL mode DISABLED.

The ?attribute=value options dictionary can contain the following options:

  • mycnfPath: the path to the MySQL configuration file of the instance.

  • outputMycnfPath: alternative output path to write the MySQL configuration file of the instance.

  • password: the password to be used by the connection.

  • clusterAdmin: the name of the InnoDB cluster administrator user to be created. The supported format is the standard MySQL account name format.

  • clusterAdminPassword: the password for the InnoDB cluster administrator account.

  • clearReadOnly: a boolean value used to confirm that super_read_only must be disabled.

  • interactive: a boolean value used to disable the interactive wizards which start after issuing commands. For example prompts provided when global variables are not populated, confirmation prompts after changing a instance's configuration.

  • restart: a boolean value used to indicate that a remote restart of the target instance should be performed to finalize the operation.

19.2.1.2 Connecting using a URI String

You can configure the MySQL Server which MySQL Shell connects to by passing the connection data in URI type string format. Such strings can be used with the --uri command option, the MySQL Shell \connect command, and methods such as shell.connect() or dba.createCluster()

The URI type string should use the following format:

scheme://[user[:[password]]@]target[:port][/schema][?attribute1=value1&attribute2=value2...
Important

Percent encoding must be used for reserved characters in the elements of the URI type string. For example, if you specify a password that includes the @ character, the character must be replaced by %40.

The options you can use in a URI type string for a MySQL Shell connection are described at Section 19.2.1.1, “Valid Connection Options”.

If no password is specified using the URI type string, which is recommended, then the password is prompted for. The following examples show how to specify URI type strings with the user name user, in each case the password is prompted for:

  • An X Protocol connection to a local server instance listening at port 33065.

    mysqlx://user@localhost:33065
    
  • A classic MySQL protocol X Protocol connection to a local server instance listening at port 3333.

    mysql://user@localhost:3333
    
  • An X Protocol connection to a remote server instance, using a host name, an IPv4 address and an IPv6 address.

    mysqlx://user@server.example.com/
    mysqlx://user@198.51.100.14:123
    mysqlx://user@[2001:db8:85a3:8d3:1319:8a2e:370:7348]
    
  • An optional path can be specified, which represents a database schema.

    mysqlx://user@198.51.100.1/world%5Fx
    mysqlx://user@198.51.100.2:33060/world
    
  • An optional query can be specified, consisting of values in the form of a key=value pair or as a single key. The , character is used as a separator for values, a combination of multiple pairs and keys can be specified. Values can be of type list, list values are ordered by appearance. Strings must be percent encoded.

    ssluser@127.0.0.1?ssl-ca%3D%2Froot%2Fclientcert%2Fca-cert.pem%26ssl-cert%3D%2Fro\
    ot%2Fclientcert%2Fclient-cert.pem%26ssl-key%3D%2Froot%2Fclientcert%2Fclient-key
    .pem
    

By default, MySQL Shell assumes that connections require a password. The password is requested at the login prompt. If the user has a password-less account, which is insecure and not recommended, or if socket peer-credential authentication is in use (for Unix socket connections), you must explicitly specify that no password is provided and the password prompt is not required. To do this, place a : after the user in the URI type string but do not specify a password after it. For example:

mysqlx://user:@localhost

19.2.1.3 Connecting using a Data Dictionary

You can configure the MySQL Server which MySQL Shell connects to by passing the connection data in a data dictionary of key and value pairs. Such data dictionaries can be used with methods such as shell.connect() or dba.createCluster().

Data dictionaries are surrounded by { and } characters and the , character is used as a separator between key and value pairs. The : character is used between keys and values, and strings must be delimited, for example using the ' chracter. It is not necessary to encoding strings in data dictionaries, unlike URI type strings.

The options you can use in a data dictionary for a MySQL Shell connection are described at Section 19.2.1.1, “Valid Connection Options”.

If no password is specified using a data dicntionary, which is recommended, then the password is prompted for. The following examples show how to specify data dictionaries with the user name user, in each case the password is prompted for:

  • An X Protocol connection to a local server instance listening at port 33065.

    {user:'user', host:'localhost', port:33065}
    
  • A classic MySQL protocol X Protocol connection to a local server instance listening at port 3333.

    {user:'user', host:'localhost', port:3333}
    
  • An X Protocol connection to a remote server instance, using a host name, an IPv4 address and an IPv6 address.

    {user:'user', host:'server.example.com'}
    {user:'user', host:198.51.100.14:123}
    {user:'user', host:[2001:db8:85a3:8d3:1319:8a2e:370:7348]}
    
  • An optional schema can be specified, which represents a database.

    {user:'user', host:'localhost', schema:'world'}
    

By default, MySQL Shell assumes that connections require a password. The password is requested at the login prompt. If the user has a password-less account, which is insecure and not recommended, or if socket peer-credential authentication is in use (for Unix socket connections), you must explicitly specify that no password is provided and the password prompt is not required. To do this, place a : after the user in the URI type string but do not specify a password after it. For example:

mysqlx://user:@localhost

19.2.1.4 Connecting using Individual Parameters

In addition to specifying connection parameters using a URI type string, it is also possible to define the connection data when starting MySQL Shell using separate command parameters for each value. For a full reference of MySQL Shell command options see Section 4.5.7, “mysqlsh — The MySQL Shell”.

Use the following connection related parameters:

The first 5 parameters match the elements used in the URI type string format described at Section 19.2.1.2, “Connecting using a URI String”.

By default, MySQL Shell assumes that connections require a password. The password is requested at the login prompt. If the user has a password-less account, which is insecure and not recommended, or if socket peer-credential authentication is in use (for Unix socket connections), you must explicitly specify that no password is provided and the password prompt is not required. To do this, either specify the --no-password option, or specify the --password= option with an empty value.

For consistency, the following aliases are supported for some parameters:

When parameters are specified in multiple ways, for example using both the --uri option and specifying individual parameters such as --user, the following rules apply:

  • If an argument is specified more than once the value of the last appearance is used.

  • If both individual connection arguments and --uri are specified, the value of --uri is taken as the base and the values of the individual arguments override the specific component from the base URI.

    For example to override user from the URI:

    shell> mysqlsh --uri user@localhost:33065 --user otheruser
    

The following examples show how to use command parameters to specify connections. Attempt to establish an X Protocol connection with a specified user at port 33065.

shell> mysqlsh --mysqlx -u user -h localhost -P 33065

Attempt to establish a classic MySQL protocol connection with a specified user.

shell> mysqlsh --mysql -u user -h localhost

19.2.1.5 Using Encrypted Connections

Using encrypted connections is possible when connecting to a TLS (sometimes referred to as SSL) enabled MySQL server. Much of the configuration of MySQL Shell is based on the options used by MySQL server, see Section 6.4, “Using Encrypted Connections” for more information.

To configure an encrypted connection at startup of MySQL Shell, use the following command options:

  • --ssl : Deprecated, to be removed in a future version. This option enables or disables encrypted connections.

  • --ssl-mode : This option specifies the security state of the connection to the server.

  • --ssl-ca=filename: The path to a file in PEM format that contains a list of trusted SSL Certificate Authorities.

  • --ssl-capath=directory: The path to a directory that contains trusted SSL Certificate Authority certificates in PEM format.

  • --ssl-cert=filename: The name of the SSL certificate file in PEM format to use for establishing an encrypted connection.

  • --ssl-cipher=name: The name of the SSL cipher to use for establishing an encrypted connection.

  • --ssl-key=filename: The name of the SSL key file in PEM format to use for establishing an encrypted connection.

  • --ssl-crl=name: The path to a file containing certificate revocation lists in PEM format.

  • --ssl-crlpath=directory: The path to a directory that contains files containing certificate revocation lists in PEM format.

  • --tls-version=version: The TLS protocols permitted for encrypted connections.

Alternatively, the SSL options can be encoded as part of a URI type string as part of the query element. The available SSL options are the same as those listed above, but written without the preceding hyphens. For example, ssl-ca is the equivalent of --ssl-ca.

Paths specified in a URI type string must be percent encoded, for example:

ssluser@127.0.0.1?ssl-ca%3D%2Froot%2Fclientcert%2Fca-cert.pem%26ssl-cert%3D%2Fro\
ot%2Fclientcert%2Fclient-cert.pem%26ssl-key%3D%2Froot%2Fclientcert%2Fclient-key
.pem

See Section 19.2.1.2, “Connecting using a URI String” for more information.

19.2.1.6 Connections in JavaScript and Python

When a connection is made using the command options or by using any of the MySQL Shell commands, a global session object is created. This session is global because once created, it can be used in any of the MySQL Shell execution modes.

Any global session object is available in JavaScript or Python modes because a variable called session holds a reference to it.

In addition to the global session object, sessions can be established and assigned to a different variable by using the functions available in the mysql and mysqlx JavaScript and Python modules.

For example, the following functions are provided by these modules:

  • mysqlx.getSession(connectionData[, password])

    The returned object can be Session if the object was created or retrieved using a Session instance, and ClassicSession if the object was created or retrieved using a ClassicSession instance.

  • mysql.getClassicSession(connectionData[, password])

    The returned object is a ClassicSession which uses the traditional MySQL protocol and has a limited development API.

connectionData can be either a URI type string as specified at Section 19.2.1.2, “Connecting using a URI String” or a dictionary containing the connection parameters.

Sessions created using either mysql.getClassicSession(connection_data) or mysqlx.getSession(connection_data) use ssl-mode=REQUIRED as the default if no ssl-mode is provided, and neither ssl-ca nor ssl-capath is provided. If no ssl-mode is provided and any of ssl-ca or ssl-capath is provided, created sessions default to ssl-mode=VERIFY_CA.

The following example shows how to create a Session using the X Protocol:

mysql-js> var mysession1=mysqlx.getSession('root@localhost:33060', 'password');
mysql-js> session
<Session:root@localhost>
mysql-js>

The following example shows how to create a ClassicSession:

mysql-js> var mysession2=mysql.getClassicSession('root@localhost:3306', 'password');
mysql-js> session
<ClassicSession:root@localhost:3306>
mysql-js>
19.2.1.6.1 Using Encrypted Connections in Code

To establish an encrypted connection, set the SSL information in the connectionData dictionary. For example:

mysql-js> var session=mysqlx.getSession({host: 'localhost',
                                             user: 'root',
                                             password: 'password',
                                             ssl_ca: "path_to_ca_file",
                                             ssl_cert: "path_to_cert_file",
                                             ssl_key: "path_to_key_file"});

19.2.2 MySQL Shell Sessions

This section explains the different types of sessions in MySQL Shell and how to create and configure them.

19.2.2.1 MySQL Shell Sessions Explained

MySQL Shell is a unified interface to operate MySQL Server through scripting languages such as JavaScript or Python. To maintain compatibility with previous versions, SQL can also be executed in certain modes. A connection to a MySQL server is required. In MySQL Shell these connections are handled by a Session object.

The following types of Session object are available:

  • Session: Use this session type for new application development to communicate with MySQL server instances which have the X Protocol enabled. It offers the best integration with MySQL Server, and therefore, it is used by default.

  • ClassicSession Use this session type to interact with MySQL Servers that do not have the X Protocol enabled. The development API available for this type of session is very limited. For example, there are no CRUD operations, no collection handling, and binding is not supported.

Important

ClassicSession is specific to MySQL Shell and cannot be used with other implementations of X DevAPI, such as MySQL Connectors.

Choosing a MySQL Shell Session Type

MySQL Shell creates a Session object by default. You can either configure the session type using MySQL Shell command options, the scheme element of a URI type string, or provide an option to the \connect command. To choose which type of session should be created when starting MySQL Shell, use one of these options:

  • --mysqlx creates a Session, connected using X Protocol.

  • --mysql creates a ClassicSession, connected using MySQL protocol.

To choose which type of session to use when defining a URI type string use one of these options:

  • Specify mysqlx to create an X Protocol session. The X Plugin must be installed on the server instance, see Chapter 20, Using MySQL as a Document Store for more information.

  • Specify mysql to create a classic MySQL protocol session.

For more information, see Section 19.2.1.2, “Connecting using a URI String”.

Creating a Session Using Shell Commands

If you open MySQL Shell without specifying connection parameters, MySQL Shell opens without an established global session. It is possible to establish a global session once MySQL Shell has been started using the MySQL Shell \connect URI command, where URI is a URI type string as defined at Section 19.2.1.2, “Connecting using a URI String”. For example:

  • \connect -ma URI: Creates a Session, automatically detecting the protocol. Attempts to use X Protocol by default, and falls back to MySQL protocol.

  • \connect -mx | --mysqlx URI: Creates a Session using X Protocol.

  • \connect -mc | --mysql URI: Creates a ClassicSession using MySQL protocol.

For example:

mysql-js> \connect mysqlx://user@localhost

Alternatively, use the shell.connect('URI' method. For example this is equivalent to the above \connect> command:

mysql-js> shell.connect('mysqlx://user@localhost')

19.2.3 MySQL Shell Global Variables

MySQL Shell reserves certain variables as global variables, which are assigned to commonly used objects in scripting. This section describes the available global variables and provides examples of working with them. The global variables are:

  • session represents the global session if one has been established.

  • db represents a schema if one has been defined, for example by a URI type string.

  • dba represents the AdminAPI, a component of InnoDB cluster which enables you to administer clusters of server instances. See Chapter 21, InnoDB Cluster.

  • shell provides general purpose functions, for example to configure MySQL Shell.

  • util provides utility functions, for example to check server instances before an upgrade.

Important

These words are reserved and cannot be used, for example as names of variables.

By using these global objects, MySQL Shell provides interactive error resolution for common situations. For example:

  • Attempting to use an undefined session global variable.

  • Attempting to retrieve an nonexistent schema using session.

  • Attempting to use an undefined db global variable.

Undefined Global Session

The global session variable is set when a global session is established. When a global session is established, issuing a session statement in MySQL Shell displays the session type and its URI as follows:

mysql-js> session
<Session:root@localhost:33060>
mysql-js>

If no global session has been established, MySQL Shell displays the following:

mysql-js> session
<Undefined>
mysql-js>

If you attempt to use the session variable when no global session is established, interactive error resolution starts and you are prompted to provide the required information to establish a global session. If the session is successfully established, it is assigned to the session variable. The prompts are:

  • An initial prompt explains that no global session is established and asks if one should be established.

  • If you choose to set a global session, the session type is requested.

  • The URI type string to connect to the server instance is requested. See Section 19.2.1.2, “Connecting using a URI String”.

  • If required, a password is requested.

For example:

mysql-js> session.uri
The global session is not set, do you want to establish a session?

   1) MySQL Document Store Session through X Protocol
   2) Classic MySQL Session

Please select the session type or ENTER to cancel: 2
Please specify the MySQL server URI: root@localhost
Enter password: 
root@localhost:
mysql-js> session
<ClassicSession:root@localhost:>

Undefined db Variable

The global db variable is set when a global session is established and a default schema is configured. For example, using a URI type string such as root@localhost/sakila to establish a global session connected to the MySQL Server at localhost, on port 33060, as the user root, assigns the schema sakila to the global variable db. Once a schema is defined, issuing db at the MySQL Shell prompt prints the schema name as follows:

mysql-js> db
<Schema:world_x>
mysql-js>

If there is no global session established, the following is displayed:

mysql-js> db
<Undefined>
mysql-js>

If you attempt to use the db variable when no global session has been established, the following error is displayed:

mysql-js> db.getCollections()
LogicError: The db variable is not set, establish a global session first.
at (shell):1:2
in db.getCollections()
^

If a global session has been established but you attempt to use an undefined db, interactive error resolution begins and you are prompted to define an active schema by providing the schema name. If this succeeds the db variable is set to the defined schema. For example:

mysql-js> db.getCollections()
The db variable is not set, do you want to set the active schema? [y/N]:y
Please specify the schema:world_x
[
<Collection:countryinfo>
]
mysql-js> db
<Schema:world_x>
mysql-js>

Retrieving an Nonexistent Schema

If you attempt to use session to retrieve an nonexistent schema, interactive error resolution provides the option to create the schema.

mysql-js> var mySchema = session.getSchema('my_test')
The schema my_test does not exist, do you want to create it? [y/N]: y

mysql-js> mySchema
<Schema:my_test>
mysql-js>

In all cases, if you do not provide the information required to resolve each situation, a proper result of executing the requested statement on an undefined variable is displayed.

19.2.4 MySQL Shell Code History

Code which you issue in MySQL Shell is saved to the history, which can then be accessed using the up and down arrow keys. The shell.options["history.autoSave"] option must be set to ensure history is automatically saved.

History entries are saved to the ~/.mysqlsh/history file on Linux and Mac, or the %AppData%\MySQL\mysqlsh\history file on Windows. The user configuration path can be overridden on all platforms by defining the environment variable MYSQL_USER_CONFIG_HOME. The value of this variable replaces %AppData%\MySQL\mysqlsh\ on Windows or ~/.mysqlsh/ on Unix.

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 numbering of history entries only resets after restarting MySQL Shell (or after issuing \history clear), meaning each entry keeps its entry number even when the oldest entries are discarded.

You can customize the entries added to the history using the --histignore command option.

You can search the history using the incremental history search feature. To search the history, use control-r to search backwards, or control-s to search forwards through the history. Once the search is active, typing characters searches for any strings that match them in the history and displays the first match. Use control-s or control-r to search for further matches to the current search term. Typing more characters further refines the search. During a search you can press the arrow keys to continue stepping through the history from the current search result. Press Enter to accept the displayed match. Use control-c to cancel the search.

History entries are saved to the history file, which is readable only by the owner user and is created automatically by MySQL Shell. Only code which you type interactively at the MySQL Shell prompt is saved to the history. Code that is executed indirectly or internally, for example when the \source command is executed, is not added to the history. When you issue multi-line code, the new line characters are stripped in the history entry. If the same code is issued multiple times it is only stored in the history once, reducing duplication. The shell.options["history.maxSize"]=number option sets the maximum number of entries to store in the history, the default is 1000. If the number of history entries exceeds the configured maximum, the oldest entries are removed. If the maximum is set to 0, no history entries are saved. Old history entries are also deleted when MySQL Shell exits. If the history file cannot be read or written to, MySQL Shell logs an error message and skips the read or write operation.

Additionally, when using MySQL Shell in SQL mode, you can configure strings which should not be added to the history. By default strings that match the glob patterns IDENTIFIED, or PASSWORD are not added to the history. To configure further strings to match use either the --histignore command option, or shell.options["history.sql.ignorePattern"]. Multiple strings can be specified, separated by a colon (:). The history matching uses case insensitive glob pattern like matching. Supported wildcards are * (match any 0 or more characters) and ? (match exactly 1 character). The default strings are specified as PASSWORD:IDENTIFIED.

19.3 MySQL Shell Code Execution

This section explains how code execution works in MySQL Shell.

19.3.1 Interactive Code Execution

The default mode of MySQL Shell provides interactive execution of database operations that you type at the command prompt. These operations can be written in JavaScript, Python or SQL depending on the curent Section 19.3.5, “Active Language”. When executed, the results of the operation are displayed on-screen.

As with any other language interpreter, MySQL Shell is very strict regarding syntax. For example, the following JavaScript snippet reads and prints the documents in a collection:

var mysqlx = require('mysqlx').mysqlx;
var mySession = mysqlx.getSession('user:pwd@localhost');
var result = mySession.world_x.countryinfo.find().execute();
var record = result.fetchOne();
while(record){
  print(record);
  record = result.fetchOne();
}

As seen above, the call to find() is followed by the execute() function. CRUD database commands are only actually executed on the MySQL Server when execute() is called. However, when working with MySQL Shell interactively, execute() is implicitly called whenever you press Return on a statement. Then the results of the operation are fetched and displayed on-screen. The rules for when you need to call execute() or not are as follows:

  • When using MySQL Shell in this way, calling execute() becomes optional on:

    • Collection.add()

    • Collection.find()

    • Collection.remove()

    • Collection.modify()

    • Table.insert()

    • Table.select()

    • Table.delete()

    • Table.update()

  • Automatic execution is disabled if the object is assigned to a variable. In such a case calling execute() is mandatory to perform the operation.

  • When a line is processed and the function returns any of the available Result objects, the information contained in the Result object is automatically displayed on screen. The functions that return a Result object include:

    • The SQL execution and CRUD operations (listed above)

    • Transaction handling and drop functions of the session objects in both mysql and mysqlx modules: -

      • startTransaction()

      • commit()

      • rollback()

      • dropSchema()

      • dropCollection()

      • ClassicSession.runSql()

Based on the above rules, the statements needed in the MySQL Shell in interactive mode to establish a session, query, and print the documents in a collection are:

mysql-js> var mysqlx = require('mysqlx').mysqlx;
mysql-js> var mySession = mysqlx.getSession('user:pwd@localhost');

No call to execute() is needed and the Result object is automatically printed.

mysql-js> mySession.world_x.countryinfo.find();

19.3.2 Code Autocompletion

MySQL Shell supports autocompletion of text preceding the cursor by pressing the Tab key. The Section 19.4.1, “MySQL Shell Commands” can be autocompleted in any of the language modes. For example typing \con and pressing the Tab key autocompletes to \connect. Autocompletion is available for SQL, JavaScript and Python language keywords depending on the current Section 19.3.5, “Active Language”.

Autocompletion supports the following text objects:

  • In SQL mode - autocompletion is aware of schema names, table names, column names of the current active schema.

  • In JavaScript and Python modes - autocompletion is aware of object members, for example:

    • global object names such as session, db, dba, shell, mysql, mysqlx, and so on.

    • members of global objects such as session.connect(), dba.configureLocalInstance(), and so on.

    • global user defined variables

    • chained object property references such as shell.options.verbose.

    • chained X DevAPI method calls such as col.find().where().execute().fetchOne().

By default autocompletion is enabled, to change this behavior see Configuring Autocompletion.

Once you activate autocompletion, if the text preceding the cursor has exactly one possible match, the text is automatically completed. If autocompletion finds multiple possible matches, it beeps or flashes the terminal. If the Tab key is pressed again, a list of the possible completions is displayed. If no match is found then no autocompletion happens.

Autocompleting SQL

When MySQL Shell is in SQL mode, autocompletion tries to complete any word with all possible completions that match. In SQL mode the following can be autocompleted:

  • SQL keywords - List of known SQL keywords. Matching is case-insensitive.

  • SQL snippets - Certain common snippets, such as SHOW CREATE TABLE, ALTER TABLE, CREATE TABLE, and so on.

  • Table names - If there is an active schema and database name caching is not disabled, all the tables of the active schema are used as possible completions.

As a special exception, if a backtick is found, only table names are considered for completion. In SQL mode, autocompletion is not context aware, meaning there is no filtering of completions based on the SQL grammar. In other words, autocompleting SEL returns SELECT, but it could also include a table called selfies.

Autocompleting JavaScript and Python

In both JavaScript and Python modes, the string to be completed is determined from right to left, beginning at the current cursor position when Tab is pressed. Contents inside method calls are ignored, but must be syntactically correct. This means that strings, comments and nested method calls must all be properly closed and balanced. This allows chained methods to be handled properly. For example, when you are issuing:

print(db.user.select().where("user in ('foo', 'bar')").e

Pressing the Tab key would cause autocompletion to try to complete the text db.user.select().where().e but this invalid code yields undefined behavior. Any whitespace, including newlines, between tokens separated by a . is ignored.

Configuring Autocompletion

By default the autocompletion engine is enabled. This section explains how to disable autocompletion and how to use the \rehash MySQL Shell command. Autocompletion uses a cache of database name objects that MySQL Shell is aware of. When autocompletion is enabled, this name cache is automatically updated. For example whenever you load a schema, the autocompletion engine updates the name cache based on the text objects found in the schema, so that you can autocomplete table names and so on.

To disable this behavior you can:

  • Start MySQL Shell with the --no-name-cache command option.

  • Modify the autocomplete.nameCache and devapi.dbObjectHandles keys of the shell.options to disable the autocompletion while MySQL Shell is running.

When the autocompletion name cache is disabled, you can manually update the text objects autocompletion is aware of by issuing \rehash. This forces a reload of the name cache based on the current active schema.

To disable autocompletion while MySQL Shell is running use the following shell.options keys:

  • autocomplete.nameCache: boolean toggles autocompletion name caching for use by SQL.

  • devapi.dbObjectHandles: boolean toggles autocompletion name caching for use by the X DevAPI db object, for example db.mytable, db.mycollection.

Both keys are set to true by default, and set to false if the --no-name-cache command option is used. To change the autocompletion name caching for SQL while MySQL Shell is running, issue:

shell.options['autocomplete.nameCache']=true

Use the \rehash command to update the name cache manually.

To change the autocompletion name caching for JavaScript and Python while MySQL Shell is running, issue:

shell.options['devapi.dbObjectHandles']=true

Again you can use the \rehash command to update the name cache manually.

19.3.3 Batch Code Execution

As well as interactive code execution, MySQL Shell provides batch code execution from:

  • A file loaded for processing.

  • A file containing code that is redirected to the standard input for execution.

  • Code from a different source that is redirected to the standard input for execution.

The input is processed based on the current programming language selected in MySQL Shell, which defaults to JavaScript. You can change the default programming language using the defaultMode MySQL Shell configuration option. Files with the extensions .js, .py, and .sql are always processed in the appropriate language mode, regardless of the default programming language.

This example shows how to load JavaScript code from a file for batch processing:

shell> mysqlsh --file code.js

Here, a JavaScript file is redirected to standard input for execution:

shell> mysqlsh < code.js

This example shows how to redirect SQL code to standard input for execution:

shell> echo "show databases;" | mysqlsh --sql --uri root@198.51.100.141:33060

Executable Scripts

Starting with version 1.0.4, on Linux you can create executable scripts that run with MySQL Shell by including a #! line as the first line of the script. This line should provide the full path to MySQL Shell and include the --file option. For example:

#!/usr/local/mysql-shell/bin/mysqlsh --file
print("Hello World\n");

The script file must be marked as executable in the filesystem. Running the script invokes MySQL Shell and it executes the contents of the script.

19.3.4 Output Formats

The output of the commands processed on the server can be formatted in different ways. This section details the different available output formats.

19.3.4.1 Table Format

The table format is used by default when MySQL Shell is in interactive mode. The output is presented as a formatted table for a better view and to aid analysis.

mysql-sql> select * from sakila.actor limit 3;
+----------+-------------+----------------+----------------------+
| actor_id | first_name  | last_name      | last_update          |
+----------+-------------+----------------+----------------------+
|        1 | PENELOPE    | GUINESS        | 2006-02-15 4:34:33   |
|        2 | NICK        | WAHLBERG       | 2006-02-15 4:34:33   |
|        3 | ED          | CHASE          | 2006-02-15 4:34:33   |
+----------+-------------+----------------+----------------------+
3 rows in set (0.00 sec)

mysql-sql>

To get this output format when running in batch mode, use the --table command-line option.

19.3.4.2 Tab Separated Format

This format is used by default when running MySQL Shell in batch mode, to have better output for automated analysis.

>echo "select * from sakila.actor limit 3;" | mysqlsh --classic --uri root@198.51.100.141:33460
actor_id        first_name      last_name       last_update
1       PENELOPE        GUINESS 2006-02-15 4:34:33
2       NICK    WAHLBERG        2006-02-15 4:34:33
3       ED      CHASE   2006-02-15 4:34:33

19.3.4.3 JSON Format Output

MySQL Shell supports the JSON format for output and it is available both in interactive and batch mode. This output format can be enabled using the --json command-line option:

JSON Format in Batch Mode

shell>echo "select * from sakila.actor limit 3;" | mysqlsh --json --sqlc --uri root@198.51.100.141:3306
{"duration":"0.00 sec","info":"","row_count":3,"rows":[[1,"PENELOPE","GUINESS",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}],[2,"NICK","WAHLBERG",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}],[3,"ED","CHASE",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}]],"warning_count":0}

shell>echo "select * from sakila.actor limit 3;" | mysqlsh --json=raw --sqlc --uri root@198.51.100.141:3306
{"duration":"0.00 sec","info":"","row_count":3,"rows":[[1,"PENELOPE","GUINESS",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}],[2,"NICK","WAHLBERG",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}],[3,"ED","CHASE",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}]],"warning_count":0}

shell>echo "select * from sakila.actor limit 3;" | mysqlsh --json=pretty --sqlc --uri root@198.51.100.141:3306
{
    "duration": "0.00 sec",
    "info": "",
    "row_count": 3,
    "rows": [
        [
            1,
            "PENELOPE",
            "GUINESS",
            {
                "year": 2006,
                "month": 1,
                "day": 15,
                "hour": 4,
                "minute": 34,
                "second": 33.0
            }
        ],
        [
            2,
            "NICK",
            "WAHLBERG",
            {
                "year": 2006,
                "month": 1,
                "day": 15,
                "hour": 4,
                "minute": 34,
                "second": 33.0
            }
        ],
        [
            3,
            "ED",
            "CHASE",
            {
                "year": 2006,
                "month": 1,
                "day": 15,
                "hour": 4,
                "minute": 34,
                "second": 33.0
            }
        ]
    ],
    "warning_count": 0
}
shell>

JSON Format in Interactive Mode (started with --json=raw)

mysql-sql> select * from sakila.actor limit 3;
{"duration":"0.00 sec","info":"","row_count":3,"rows":[[1,"PENELOPE","GUINESS",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}],[2,"NICK","WAHLBERG",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}],[3,"ED","CHASE",{"year":2006,"month":1,"day":15,"hour":4,"minute":34,"second":33.0}]],"warning_count":0}

mysql-sql>

JSON Format in Interactive Mode (started with --json=pretty)

mysql-sql> select * from sakila.actor limit 3;
{
    "duration": "0.00 sec",
    "info": "",
    "row_count": 3,
    "rows": [
        [
            1,
            "PENELOPE",
            "GUINESS",
            {
                "year": 2006,
                "month": 1,
                "day": 15,
                "hour": 4,
                "minute": 34,
                "second": 33.0
            }
        ],
        [
            2,
            "NICK",
            "WAHLBERG",
            {
                "year": 2006,
                "month": 1,
                "day": 15,
                "hour": 4,
                "minute": 34,
                "second": 33.0
            }
        ],
        [
            3,
            "ED",
            "CHASE",
            {
                "year": 2006,
                "month": 1,
                "day": 15,
                "hour": 4,
                "minute": 34,
                "second": 33.0
            }
        ]
    ],
    "warning_count": 0
}

mysql-sql>

19.3.4.4 Result Metadata

When an operation is executed, in addition to any results returned, some additional information is available. This includes information such as the number of affected rows, warnings, duration, and so on, when any of these conditions is true:

  • JSON format is being used for the output

  • MySQL Shell is running in interactive mode.

19.3.5 Active Language

MySQL Shell can execute SQL, JavaScript or Python code, but only one language can be active at a time. The active mode determines how the executed statements are processed:

  • If using SQL mode, statements are processed as SQL which means they are sent to the MySQL server for execution.

  • If using JavaScript mode, statements are processed as JavaScript code.

  • If using Python mode, statements are processed as Python code.

When running MySQL Shell in interactive mode, activate a specific language by entering the commands: \sql, \js, \py.

When running MySQL Shell in batch mode, activate a specific language by passing any of these command-line options: --js, --py or --sql. The default mode if none is specified is JavaScript.

Use MySQL Shell to execute the content of the file code.sql as SQL.

shell> mysqlsh --sql < code.sql

Use MySQL Shell to execute the content of the file code.js as JavaScript code.

shell> mysqlsh < code.js

Use MySQL Shell to execute the content of the file code.py as Python code.

shell> mysqlsh --py < code.py

19.3.6 Batch Mode Made Interactive

This section describes code execution in batch mode.

  • In batch mode, all the command logic described above is not available, only valid code for the active language can be executed.

  • When processing SQL code, it is executed statement by statement using the following logic: read/process/print result.

  • When processing non-SQL code, it is loaded entirely from the input source and executed as a unit.

Use the --interactive (or -i) command-line option to configure MySQL Shell to process the input source as if it were being issued in interactive mode; this enables all the features provided by the Interactive mode to be used in batch processing.

Note

In this case, whatever the source is, it is read line by line and processed using the interactive pipeline.

19.3.6.1 Multiple-line Support

It is possible to specify statements over multiple lines. When in Python or JavaScript mode, multiple-line mode is automatically enabled when a block of statements starts like in function definitions, if/then statements, for loops, and so on. In SQL mode multiple line mode starts when the command \ is issued.

Once multiple-line mode is started, the subsequently entered statements are cached.

For example:

mysql-sql> \
... create procedure get_actors()
... begin
...   select first_name from sakila.actor;
... end
...
mysql-sql>

19.4 Configuring MySQL Shell

This section explains how to configure MySQL Shell using commands executable from the interactive code editor and command options. For a description of MySQL Shell command options, see Section 4.5.7, “mysqlsh — The MySQL Shell”.

19.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 commands.

\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 parameters. When used without parameters a general help is printed including information about:

  • Available commands.

  • Available commands for the active mode.

When used with a parameter, the parameter must be a valid command. If that is the case, help for that specific command is printed including:

  • Description

  • Supported aliases if any

  • Additional help if any

For example:

\help connect

If the parameter is not a valid command, the general help is printed.

Connect and Reconnect Commands

The \connect command is used to connect to a MySQL Server using an URI type string. See Section 19.2.1.2, “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 19.2.4, “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.

19.5 MySQL Shell Utilities

MySQL Shell includes utilities for working with MySQL. To access the utilities use the util global object, which includes the checkForServerUpgrade(instance [,password]) operation that enables you to verify server instances are suitable for upgrade to MySQL 8.

If you issue checkForServerUpgrade() without specifying an instance, the instance currently connected to the global session is checked. checkForServerUpgrade() uses a classic MySQL protocol connection, which you can create beforehand using the mysql option, or specify as arguments to the function. Provide the password for the user either as part of the connection details or as the second element passed to the operation. For example, to verify the server at URI type string user@example.com:3306 issue:

mysqlsh> util.checkForServerUpgrade('user@example.com:3306', 'password')

MySQL Shell connects to the server instance and tests the settings described at Verifying Upgrade Prerequisites for Your MySQL 5.7 Installation. For example:

The MySQL server at example.com:3306 will now be checked for compatibility issues for upgrade to MySQL 8.0...
MySQL version: 5.7.21 - MySQL Community Server (GPL)

1) Usage of db objects with names conflicting with reserved keywords in 8.0
  No issues found

2) Usage of utf8mb3 charset
  No issues found

3) Usage of use ZEROFILL/display length type attributes
  No issues found

4) Issues reported by 'check table x for upgrade' command
  No issues found

5) Table names in the mysql schema conflicting with new tables in 8.0
  No issues found

6) Usage of old temporal type
  No issues found

7) Foreign key constraint names longer than 64 characters
  No issues found

8) Usage of obsolete MAXDB sql_mode flag
  No issues found

9) Usage of obsolete sql_mode flags
  No issues found

10) Usage of partitioned tables in shared tablespaces
  No issues found

11) Usage of removed functions
  No issues found

No known compatibility errors or issues for upgrading the target server to MySQL 8 were found.
  

In this case the server instance being checked passed all of the tests and can be upgraded to MySQL 8. A server instance that fails any of the tests requires changes before it can be upgraded to MySQL 8. A server that generates warnings can be upgraded to MySQL 8, but the configuration could be improved to make the server more compatible with MySQL 8. The return value of util.checkForServerUpgrade() is:

  • 0 if no issues found

  • 1 if warnings were found

  • 2 if errors were found

19.6 MySQL Shell Application Log

This section explains the application log.

19.6.1 Application Log

MySQL Shell can be configured to generate an application log file with information about issues of varying severity. You can use this information to verify the state of MySQL Shell while it is running.

The location of the log file is the user configuration path and the file is named mysqlsh.log.

Log File on Windows

On Windows, the default path to the log file is %APPDATA%\MySQL\mysqlsh\mysqlsh.log

To find the location of %APPDATA% on your system, echo it from the comand-line. For example:

C:>echo %APPDATA%

C:\Users\exampleuser\AppData\Roaming

On Windows, the path is determined by the result of gathering the %APPDATA% folder specific to that user, and then appending MySQL\mysqlsh. Using the above example, we end up with:

C:\Users\exampleuser\AppData\Roaming\MySQL\mysqlsh\mysqlsh.log

Log File on Unix-based Systems

For a machine running Unix, the default path is ~/.mysqlsh/mysqlsh.log where ~ represents the user's home directory. The environment variable HOME also represents the user's home directory. Appending .mysqlsh to the user's home directory determines the default path to the logs. For example:

C:>echo $HOME

/home/exampleuser

shell> less /home/exampleuser/.mysqlsh/mysqlsh.log

The default user configuration path can be overridden on all platforms by defining the environment variable MYSQL_USER_CONFIG_HOME. The value of this variable replaces %AppData%\MySQL\mysqlsh\ on Windows or ~/.mysqlsh/ on Unix.

By default, logging is disabled in MySQL Shell. To enable logging use the --log-level command-line option when starting MySQL Shell. For example:

shell> mysqlsh --log-level=4

The number assigned to --log-level controls the level of detail in the log. The following logging levels are supported:

Log Level Meaning

1

None, the default

2

Internal Error

3

Error

4

Warning

5

Info

6

Debug

7

Debug2

8

Debug3

The log format is plain text and entries contain a timestamp and description of the problem, along with the log level from the above list. For example:

2016-04-05 22:23:01: Error: Default Domain: (shell):1:8: MySQLError: You have an error
in your SQL syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near '' at line 1 (1064) in session.sql("select * from t
limit").execute().all();

19.7 Customizing MySQL Shell

MySQL Shell offers the ability to customize the behavior and code execution environment through startup scripts, which are executed when the application is first run. Using such scripts enables you to:

  • Add additional search paths for Python or JavaScript modules.

  • Override the default prompt used by the Python and JavaScript modes.

  • Define global functions or variables.

  • Any other possible initialization through JavaScript or Python.

19.7.1 Working With Start-Up Scripts

When MySQL Shell enters either into JavaScript or Python mode, it searches for startup scripts to be executed. The startup scripts are JavaScript or Python specific scripts containing the instructions to be executed when the corresponding mode is initialized.

Startup scripts must be named as follows:

  • For JavaScript mode: mysqlshrc.js

  • For Python mode: mysqlshrc.py

MySQL Shell searches the following paths for these files (in order of execution).

On Windows:

  1. %PROGRAMDATA%\MySQL\mysqlsh\mysqlshrc.[js|py]

  2. %MYSQLSH_HOME%\shared\mysqlsh\mysqlshrc.[js|py]

  3. <mysqlsh binary path>\mysqlshrc.[js|py]

  4. %APPDATA%\MySQL\mysqlsh\mysqlshrc.[js|py]

On Linux and OSX:

  1. /etc/mysql/mysqlsh/mysqlshrc.[js|py]

  2. $MYSQLSH_HOME/shared/mysqlsh/mysqlshrc.[js|py]

  3. <mysqlsh binary path>/mysqlshrc.[js|py]

  4. $HOME/.mysqlsh/mysqlshrc.[js|py]

Warning

The lists above also define the order of searching the paths, so if something is defined in two different scripts, the script executed later takes precedence.

The environment variable MYSQLSH_HOME, used in option 2, defines the root folder of a standard setup of MySQL Shell. If MYSQLSH_HOME is not defined it is automatically calculated based on the location of the MySQL Shell binary, therefore on many standard setups it is not required to define MYSQLSH_HOME.

If MYSQLSH_HOME is not defined and the MySQL Shell binary is not in a standard install folder structure, then the path defined in option 3 in the above lists is used. If using a standard install or if MYSQLSH_HOME points to a standard install folder structure, then the path defined in option 3 is not used.

The user configuration path in option 4 can be overridden on all platforms by defining the environment variable MYSQL_USER_CONFIG_HOME. The value of this variable replaces %AppData%\MySQL\mysqlsh\ on Windows or ~/.mysqlsh/ on Unix.

19.7.2 Adding Module Search Paths

There are two ways to add additional module search paths:

  • Through environment variables

  • Through startup scripts

19.7.2.1 Environment Variables

Python uses the PYTHONPATH environment variable to allow extending the search paths for python modules. The value of this variable is a list of paths separated by:

  • A colon character in Linux and OSX

  • A semicolon character in Windows

To achieve this in JavaScript, MySQL Shell supports defining additional JavaScript module paths using the MYSQLSH_JS_MODULE_PATH environment variable. The value of this variable is a list of semicolon separated paths.

19.7.2.2 Startup Scripts

The addition of module search paths can be achieved for both languages through the corresponding startup script.

For Python modify the mysqlshrc.py file and append the required paths into the sys.path array.

# Import the sys module
import sys

# Append the additional module paths
sys.path.append('~/custom/python')
sys.path.append('~/other/custom/modules')

For JavaScript the same task is achieved by adding code into the mysqlshrc.js file to append the required paths into the predefined shell.js_module_paths array.

// Append the additional module paths
shell.js.module_paths[shell.js.module_paths.length] = '~/custom/js';
shell.js.module_paths[shell.js.module_paths.length] = '~/other/custom/modules';

19.7.3 Customizing the Prompt

The prompt of MySQL Shell can be customized using prompt theme files. To customze the prompt theme file, either set the MYSQLSH_PROMPT_THEME environment variable to a prompt theme file name, or copy a theme file to the ~/.mysqlsh/prompt.json directory on Linux and Mac, or the %AppData%\MySQL\mysqlsh\prompt.json directory on Windows.

The user configuration path for the directory can be overridden on all platforms by defining the environment variable MYSQL_USER_CONFIG_HOME. The value of this variable replaces %AppData%\MySQL\mysqlsh\ on Windows or ~/.mysqlsh/ on Unix.

The format of the prompt theme file is described in the README.prompt file, and some sample prompt theme files are included. Some of the sample prompt theme files require a special font (for example SourceCodePro+Powerline+Awesome+Regular.ttf), or support from the terminal for color display. Most terminals support 256 colors in Linux and Mac. In Windows, color support requires either a 3rd party terminal program with support for ANSI/VT100 escapes, or Windows 10. On startup, if an error is found in the prompt theme file, an error message is printed and a default prompt theme is used.

19.7.4 Configuring MySQL Shell

You can configure MySQL Shell to match your preferences, for example to start up to a certain programming language or to customize output and so on. Configuration options can be set for only the current session, or options can be set permanently by persisting changes to the MySQL Shell configuration file. Online help for all options is provided. You can configure options using either MySQL Shell command, which is available in all MySQL Shell modes for querying and changing configuration options. Alternatively in JavaScript and Python modes, use the shell.options object.

This section describes which options are available and how to configure them.

Valid Configuration Options

The following configuration options can be set using either the \option command or shell.options scripting interface:

optionName DefaultValue Type
autocomplete.nameCache true boolean
batchContinueOnError false boolean (READ ONLY)
devapi.dbObjectHandles true boolean
history.autoSave false boolean
history.maxSize 1000 integer
history.sql.ignorePattern *IDENTIFIED* : *PASSWORD* string
interactive true boolean (READ ONLY)
logLevel 5 integer ranging from 1 to 8
outputFormat table string (table, vertical, json, json/raw, tabbed)
passwordsFromStdin false boolean
sandboxDir C:\Users\MyUser\MySQL\mysql-sandboxes or $HOME/mysql-sandboxes string
showWarnings true boolean
useWizards true boolean
defaultMode none string (sql, js or py)
Note

string values are case sensitive.

Using the \option Command

The MySQL Shell \option command enables you to query and change configuration options in all modes, enabling configuration from SQL mode in addition to JavaScript and Python modes.

The command is used as follows:

  • \option -h, --help [filter] - print help for options matching filter.

  • \option -l, --list [--show-origin] - list all the options. --show-origin augments the list with information about how the value was last changed, possible values are:

    • Command line

    • Compiled default

    • Configuration file

    • Environment variable

    • User defined

  • \option option_name - print the current value of the option.

  • \option [--persist] option_name value or name=value - set the value of the option and if --persist is specified save it to the configuration file.

  • \option --unset [--persist] <option_name> - reset option's value to default and if --persist is specified, removes the option from the MySQL Shell configuration file.

Note

the value of option_name and filter are case sensitive.

See Valid Configuration Options for a list of possible values for option_name.

Using the shell.options Configuration Interface

The shell.options object is available in JavaScript and Python mode to change MySQL Shell option values. You can use specific methods to configure the options, or a data dictionary. Use the data dictionary as follows:

MySQL JS > shell.options['history.autoSave']=1

In addition to the dictionary like interface, the following methods are available:

  • shell.options.set(optionName, value) - sets the optionName to value for this session, the change is not saved to the configuration file

  • shell.options.set_persist(optionName, value) - sets the optionName to value for this session, and saves the change to the configuration file

  • shell.options.unset(optionName) - resets the optionName to the default value for this session, the change is not saved to the configuration file

  • shell.options.unset_persist(optionName) - resets the optionName to the default value for this session, and saves the change to the configuration file

Option names are treated as strings, and as such should be surrounded by ' characters. See Valid Configuration Options for a list of possible values for optionName.

Use the commands to configure MySQL Shell options as follows:

MySQL JS > shell.options.set('history.maxSize', 5000)
MySQL JS > shell.options.set_persist('useWizards', 'true')
MySQL JS > shell.options.set_persist('history.autoSave', 1)

Return options to their default values as follows:

MySQL JS > shell.options.unset('history.maxSize')
MySQL JS > shell.options.unset_persist('useWizards')

Configuration File

The MySQL Shell configuration file stores the values of the option to ensure they are persisted across sessions. Values are read at startup and when you use the persist feature, settings are saved to the configuration file.

The location of the configuration file is the user configuration path and the file is named options.json. Assuming that the default user configuration path has not been overridden by defining the environment variable MYSQL_USER_CONFIG_HOME, the path to the configuration file is:

  • on Windows %APPDATA%\MySQL\mysqlsh

  • on Unix ~/.mysqlsh where ~ represents the user's home directory.

The configuration file is created the first time you customize a configuration option. This file is internally maintained by MySQL Shell and should not be edited manually. If an unrecognized option or an option with an incorrect value is found in the configuration file on startup, MySQL Shell exits with an error.