When a connection is made using the command options or by using any of the 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 SQL mode, both Node and Classic sessions can be used because they both expose SQL execution. In SQL mode an XSession cannot be used.
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:
mysql.getSession(connectionData[, password])
mysqlx.getNodeSession(connectionData[, password])
mysql.getClassicSession(connectionData[, password])
The first of these functions is used to create an XSession which features the most comprehensive development API and supports X Protocol.
The second creates a Node Session which connects to a X Protocol enabled MySQL Server and allows SQL Execution.
The latter returns a Classic Session object which uses the traditional MySQL protocol and has a very limited development API.
connectionData can be either a URI as specified above or a dictionary containing the connection parameters. See Section 18.2.2.1, “Connecting using a URI String”.
The following example shows how to create a Node Session using the X Protocol:
mysql-js> var mysqlx=require('mysqlx').mysqlx;
mysql-js> var session=mysqlx.getNodeSession('root@localhost');
mysql-js> print(session)
<NodeSession:root@localhost>
mysql-js>The following example shows how to create a Node Session using the X Protocol so that you can execute SQL:
mysql-js> var mysqlx=require('mysqlx').mysqlx;
mysql-js> var session=mysqlx.getNodeSession({host: 'localhost', dbUser: 'root'});
mysql-js> print(session)
<NodeSession:root@localhost>
mysql-js>The following example shows how to create a Classic Session:
mysql-js> var mysql=require('mysql').mysql;
mysql-js> var session = mysql.getClassicSession('root@localhost:3307');
mysql-js> print(session)
<ClassicSession:root@localhost:3307>
mysql-js>
To establish an SSL connection, set the SSL information in the connectionData dictionary. For example:
mysql-js> var mysqlx=require('mysqlx').mysqlx;
mysql-js> var session=mysqlx.getNodeSession({host: 'localhost',
dbUser: 'root',
dbPassword: 'mypasswd',
ssl_ca: "path_to_ca_file",
ssl_cert: "path_to_cert_file",
ssl_key: "path_to_key_file"});
mysql-js> print(session)
<NodeSession:root@localhost>