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:
sessionrepresents the global session if one has been established.dbrepresents a schema if one has been defined, for example by a URI.
MySQL Shell provides interactive error resolution for common situations related to using the global variables. For example:
Attempting to use an undefined
sessionglobal variable.Attempting to retrieve an nonexistent schema using
session.Attempting to use an undefined
dbglobal variable.
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
<XSession: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 the you choose to set a global session, the session type is requested.
Either the URI or the alias of a stored session is requested.
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? [y/N]: y
Please specify the session type:
1) X
2) Node
3) Classic
Type: 2
Please specify the MySQL server URI (or $slias): root@localhost
Enter password:*******
root@localhost:33060
mysql-js>
The global db variable is set when a global
session is established and a default schema is configured. For
example, using a URI 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>
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.