You can use functions available in JavaScript and Python mode to create multiple session objects of your chosen types and assign them to variables. These session objects let you establish and manage concurrent connections to work with multiple MySQL Server instances, or with the same instance in multiple ways, from a single MySQL Shell instance.
Functions to create session objects are available in themysqlx andmysql JavaScript and Python modules. These modules must be imported before use, which is done automatically when MySQL Shell is used in interactive mode. The functionmysqlx.getSession() opens an X Protocol connection to a MySQL Server instance using the specified connection data, and returns aSession object to represent the connection. The functionsmysql.getClassicSession() andmysql.getSession() open a classic MySQL protocol connection to a MySQL Server instance using the specified connection data, and return aClassicSession object to represent the connection. With these functions, the connection protocol that MySQL Shell uses is built into the function rather than being selected using a separate option, so you must choose the appropriate function to match the correct protocol for the port.
From MySQL Shell 8.0.20, MySQL Shell provides its ownopenSession() method in theshell global object, which can be used in either JavaScript or Python mode.shell.openSession() works with both X Protocol and classic MySQL protocol. You specify the connection protocol as part of the connection data, or let MySQL Shell automatically detect it based on your other connection parameters (such as the default port number for the protocol).
The connection data for all these functions can be specified as a URI-like connection string, or as a dictionary of key-value pairs. You can access the returned session object using the variable to which you assign it. This example shows how to open a classic MySQL protocol connection using themysql.getClassicSession() function, which returns aClassicSession object to represent the connection:
mysql-js> var s1 = mysql.getClassicSession('user@localhost:3306', 'password');mysql-js> s1<ClassicSession:user@localhost:3306> This example shows how to useshell.openSession() in Python mode to open an X Protocol connection with compression required for the connection. ASession object is returned:
mysql-py> s2 = shell.open_session('mysqlx://user@localhost:33060?compression=required', 'password')mysql-py> s2<Session:user@localhost:33060> Session objects that you create in JavaScript mode using these functions can only be used in JavaScript mode, and the same happens if the session object is created in Python mode. You cannot create multiple session objects in SQL mode. Although you can only reference session objects using their assigned variables in the mode where you created them, you can use theshell.setSession() method in any mode to set as thesession global object a session object that you have created and assigned to a variable. For example:
mysql-js> var s3 = mysqlx.getSession('user@localhost:33060', 'password');mysql-js> s3<Session:user@localhost:33060>mysql-js> shell.setSession(s3);<Session:user@localhost:33060>mysql-js> session<Session:user@localhost:33060>mysql-js> shell.status();MySQL Shell version 8.0.18Session type: X ProtocolConnection Id: 5Current schema:Current user:user@localhost...TCP port: 33060... The session objects3 is now available using thesession global object, so the X Protocol connection it represents can be accessed from any of MySQL Shell's modes: SQL mode, JavaScript mode, and Python mode. Details of this connection can also now be displayed using theshell.status() method, which only displays the details for the connection represented by thesession global object. If the MySQL Shell instance has one or more open connections but none of them are set as thesession global object, theshell.status() method returns“Not Connected”.
A session object that you set usingshell.setSession() replaces any existing session object that was set as thesession global object. If the replaced session object was originally created and assigned to a variable using one of themysqlx ormysql functions orshell.openSession(), it still exists and its connection remains open. You can continue to use this connection in the MySQL Shell mode where it was originally created, and you can make it into thesession global object again at any time usingshell.setSession(). If the replaced session object was created with theshell.connect() method and assigned to a variable, the same is true. If the replaced session object was created while starting MySQL Shell, or using the\connect command, or using theshell.connect() method but without assigning it to a variable, its connection is closed, and you must recreate the session object if you want to use it again.