Documentation Home
MySQL Shell 8.0
Related Documentation Download this Manual
PDF (US Ltr) - 491.7Kb
PDF (A4) - 492.0Kb
HTML Download (TGZ) - 98.8Kb
HTML Download (Zip) - 110.5Kb


MySQL Shell 8.0  /  MySQL Shell Code Execution  /  Active Language

5.1 Active Language

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.

Note

From version 8.0.18, 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. The default mode if none is specified is JavaScript.

Use MySQL Shell to execute the content of the file code.sql as SQL.

shell> mysqlsh --sql < code.sql

Use MySQL Shell to execute the content of the file code.js as JavaScript code.

shell> mysqlsh < code.js

Use MySQL Shell to execute the content of the file code.py as Python code.

shell> mysqlsh --py < code.py

From MySQL Shell 8.0.16, 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.

From MySQL Shell 8.0.18, 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.