Documentation Home
MySQL Shell 8.0
Related Documentation Download this Manual
PDF (US Ltr) - 0.5Mb
PDF (A4) - 0.5Mb
HTML Download (TGZ) - 109.6Kb
HTML Download (Zip) - 121.1Kb


MySQL Shell 8.0  /  ...  /  Scripting Sessions in JavaScript and Python Mode

4.2.3 Scripting Sessions in JavaScript and Python Mode

You can use functions available in the mysqlx and mysql JavaScript and Python modules 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. The mysqlx and mysql modules must be imported before use, which is done automatically when MySQL Shell is used in interactive mode.

The function mysqlx.getSession() opens an X Protocol connection to a MySQL Server instance using the specified connection data, and returns a Session object to represent the connection. The functions mysql.getClassicSession() and mysql.getSession() open a classic MySQL protocol connection to a MySQL Server instance using the specified connection data, and return a ClassicSession 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.

The connection data for 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 with compression enabled for the connection, which returns a ClassicSession object to represent the connection:

mysql-js> var s1 = mysql.getClassicSession('user@localhost:3306?compression=true', 'password');
mysql-js> s1
<ClassicSession:user@localhost:3306>

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. However, you can use the shell.setSession() method in any mode to set as the session global object a session object that you have created and assigned to a variable. For example:

mysql-js> var mysqlx = require('mysqlx'); 
mysql-js> var s2 = mysqlx.getSession('user@localhost:33060', 'password');
mysql-js> s2
<Session:user@localhost:33060>
mysql-js> shell.setSession(s2);
<Session:user@localhost:33060>
mysql-js> session
<Session:user@localhost:33060>
mysql-js> shell.status();
MySQL Shell version 8.0.18

Session type:                 X Protocol
Connection Id:                5
Current schema:
Current user:                 user@localhost
...
TCP port:                     33060
...

The session object s2 is now available using the session 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 the shell.status() method, which only displays the details for the connection represented by the session global object. If the MySQL Shell instance has one or more open connections but none of them are set as the session global object, the shell.status() method returns Not Connected.

A session object that you set using shell.setSession() replaces any existing session object that was set as the session global object. If the replaced session object was originally created and assigned to a variable using one of the mysqlx or mysql functions, 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 the session global object again at any time using shell.setSession(). If the replaced session object was created with the shell.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 the shell.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.