MySQL Shell 8.0  /  ...  /  Creating the Session Global Object After Starting MySQL Shell

4.2.2 Creating the Session Global Object After Starting MySQL Shell

If you started MySQL Shell without connecting to a MySQL Server instance, you can use MySQL Shell's \connect command or the shell.connect() method to initiate a connection and create the session global object. Alternatively, the shell.getSession() method returns the session global object.

MySQL Shell's \connect command is used with a URI-like connection string, as described above and in Connecting to the Server Using URI-Like Strings or Key-Value Pairs. You can include the scheme element at the start of the URI-like connection string to select the type of session object to create, for example:

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

Alternatively, you can omit the scheme element and use the command's --mysqlx (--mx) option to create a Session object using X Protocol, or --mysql (--mc) to create a ClassicSession object using classic MySQL protocol. For example:

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

The shell.connect() method can be used in MySQL Shell as an alternative to the \connect command to create the session global object. This connection method can use a URI-like connection string, with the selected protocol specified as the scheme element. For example:

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

With the shell.connect() method, you can also specify the connection parameters using key-value pairs, supplied as a JSON object in JavaScript or as a dictionary in Python. The selected protocol (mysqlx or mysql) is specified as the value for the scheme key. For example:

mysql-js> shell.connect( {scheme:'mysqlx', user:'user', host:'localhost', port:33060} )

For instructions and examples to connect to a MySQL Server instance in these ways, see Connecting to the Server Using URI-Like Strings or Key-Value Pairs.

You may omit the connection protocol and let MySQL Shell automatically detect it based on your other connection parameters, such as specifying the default port for the protocol. To verify the protocol that was used for a connection, use MySQL Shell's \status command or the shell.status() method.

If you use the \connect command or the shell.connect() method to create a new connection when the session global object already exists (either created during startup or afterwards), MySQL Shell closes the existing connection represented by the session global object. This is the case even if you assign the new session object created by the shell.connect() method to a different variable. The value of the session global object (referenced by the session variable) is still updated with the new connection details. If you want to have multiple concurrent connections available, create these using the alternative functions described in Section 4.2.3, “Scripting Sessions in JavaScript and Python Mode”.