Documentation Home
MySQL Shell for VS Code


MySQL Shell for VS Code  /  MySQL Shell Consoles  /  Connect to MySQL Shell Sessions

Pre-General Availability: 2023-01-18

5.4 Connect to MySQL Shell Sessions

The following types of session objects are available:

  • Session: Use this session object type for new application development to communicate with MySQL Server instances where X Protocol is available. X Protocol offers the best integration with MySQL Server. For X Protocol to be available, X Plugin must be installed and enabled on the MySQL Server instance, which it is by default. X Plugin listens to the port specified by mysqlx_port, which defaults to 33060, so specify this port with connections using a session.

  • ClassicSession: Use this session object type to interact with MySQL Server instances that do not have X Protocol available. Use this session object for running SQL against servers using classic MySQL protocol.

Use MySQL Shell's \connect command or the shell.connect() method to initiate a connection and create the session global object. Related connection topics in this section are:

The most basic syntax to start a MySQL Shell session using the X Protocol is:

\connect mysqlx://<user>@{<{host name> | <IPv4 address> | <IPv6 address>}:<port>

In this example, to connect the user demo-user to MySQL Shell localhost that is listening on port 33060 using the classic session, type:

\connect mysqlx://demo-user@localhost:33060

The most basic syntax to start a MySQL Shell session using the classic MySQL protocol is:

\connect mysql://<user>@{<{host name> | <IPv4 address> | <IPv6 address>}:<port>

In this example, to connect the user demo-user to MySQL Server localhost that is listening on port 3306 using the classic session, type:

\connect mysql://demo-user@localhost:3306

The \connect (or \c) command is valid whether SQL, JavaScript, or Python is the active language.

The shell.connect() method, available for Python and JavaScript, 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. The basic syntax of the shell.connect method is:

shell.connect(<user>@{<{host name> | <IPv4 address> | <IPv6 address>}:<port>)

In this example, to connect the user demo-user to MySQL Server localhost that is listening on port 3306 using the classic session, type:

shell.connect('demo-user@localhost:3306')

The shell.connect method is valid for both JavaScript and Python.

Important

It is important to note the difference in port numbers: the port for the X Protocol is 33060, and the port for the classic MySQL protocol is 3306.

For example:

  • shell.connect('demo-user@localhost:3306') will connect to the classic MySQL protocol.

  • shell.connect('demo-user@localhost:33060') will connect to the X Protocol.

For more information, see MySQL Shell Sessions.

Connection parameters

When you start MySQL Shell, you can specify connection parameters using separate command options for each value, such as the user name, host, and port.

If you do not specify parameters for a connection the following defaults are used:

  • user defaults to the current system user name.

  • port defaults to the X Plugin port 33060 when using an X Protocol connection, and port 3306 when using a classic MySQL protocol connection.

You must supply the host as a minimum.

You can also specify the schema to be used:

For example, for the user demo-user to connect to MySQL Server running on localhost, listening on port 3306 for the classic MySQL protocol, using the schema world, and using the \connect command, enter:

\connect mysql://demo-user@localhost:3306/world

For example, for the user demo-user to connect to MySQL Server running on localhost, listening on port 3306 for the classic MySQL protocol, using the schema world, and using the shell.connect method, enter:

shell.connect('root@localhost:3306/world')

Password store

After you have used your preferred connection method, the Open MySQL Connection in Shell Session dialog opens.

Enter your password, and click OK.

The Feedback Requested dialog opens, you have the choice to save the password for your instance, with the choices:

  • Y: Yes

  • N: No

  • v: Never

The default value is No.

Enter your required value, and click OK.

Closing MySQL Shell session

To close and quit a MySQL Shell session, type:

\quit

Or, type:

\q
Note

These commands can be run when the active language is:

  • Python: py>

  • JavaScript: js>

  • SQL: sql>

These commands close the MySQL Shell GUI Console session and the tab.

You can also close the session tab by clicking the X on the open tab, or using Ctrl+F4.