MySQL Shell 8.0  /  ...  /  Connections in JavaScript and Python

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 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>