MySQL Shell can execute SQL, JavaScript or Python code, but only one language can be active at a time. The active mode determines how the executed statements are processed:
If using SQL mode, statements are processed as SQL which means they are sent to the MySQL server for execution.
If using JavaScript mode, statements are processed as JavaScript code.
If using Python mode, statements are processed as Python code.
MySQL Shell uses Python 3. For platforms that include a system supported installation of Python 3, MySQL Shell uses the most recent version available, with a minimum supported version of Python 3.4.3. For platforms where Python 3 is not included, MySQL Shell bundles Python 3.7.4. MySQL Shell maintains code compatibility with Python 2.6 and Python 2.7, so if you require one of these older versions, you can build MySQL Shell from source using the appropriate Python version.
When running MySQL Shell in interactive mode, activate a specific
language by entering the commands: \sql
,
\js
, \py
.
When running MySQL Shell in batch mode, activate a specific
language by passing any of these command-line options:
--js
,
--py
or
--sql
. If none is specified, the
default mode is SQL.
Use MySQL Shell to execute the content of the file
code.sql
as SQL.
$> mysqlsh --sql < code.sql
Use MySQL Shell to execute the content of the file
code.js
as JavaScript code.
$> mysqlsh --js < code.js
Use MySQL Shell to execute the content of the file
code.py
as Python code.
$> mysqlsh --py < code.py
You can execute single SQL statements while another language is
active, by entering the \sql
command
immediately followed by the SQL statement. For example:
mysql-py> \sql select * from sakila.actor limit 3;
The SQL statement does not need any additional quoting, and the
statement delimiter is optional. The command only accepts a single
SQL query on a single line. With this format, MySQL Shell does
not switch mode as it would if you entered the
\sql
command. After the SQL statement has been
executed, MySQL Shell remains in JavaScript or Python mode.
You can execute operating system commands while any language is
active, by entering the \system
or
\!
command immediately followed by the command
to execute. For example:
mysql-py> \system echo Hello from MySQL Shell!
MySQL Shell displays the output from the operating system command, or returns an error if it was unable to execute the command.