MySQL Shell 8.0  /  Customizing MySQL Shell  /  Working With Startup Scripts

13.1 Working With Startup Scripts

When MySQL Shell is started in JavaScript or Python mode, and also when you switch to JavaScript or Python mode for the first time, MySQL Shell searches for startup scripts to be executed. The startup scripts are JavaScript or Python specific scripts containing the instructions to be executed when MySQL Shell first enters the corresponding language mode. Startup scripts let you customize the JavaScript or Python code execution environment in any of these ways:

  • Adding additional search paths for Python or JavaScript modules.

  • Defining global functions or variables.

  • Carrying out any other possible initialization through JavaScript or Python.

The relevant startup script is loaded when you start or restart MySQL Shell in either JavaScript or Python mode, and also the first time you change to the other one of those modes while MySQL Shell is running. After this, MySQL Shell does not search for startup scripts again, so implementing updates to a startup script requires a restart of MySQL Shell if you have already entered the relevant mode. When MySQL Shell is started in SQL mode or you switch to that mode, no startup script is loaded.

The startup scripts are optional, and you can create them if you want to use them for customization. The startup scripts must be named as follows:

  • For JavaScript mode: mysqlshrc.js

  • For Python mode: mysqlshrc.py

You can place your startup scripts in any of the locations listed below. MySQL Shell searches all of the stated paths, in the order stated, for startup scripts with the file name mysqlshrc and the file extension that matches the scripting mode that is being initialized (.js by default if MySQL Shell is started with no language mode specified). Note that MySQL Shell executes all appropriate startup scripts found for the scripting mode, in the order they are found. If something is defined in two different startup scripts, the script executed later takes precedence.

  1. In the platform's standard global configuration path.

    • On Windows: %PROGRAMDATA%\MySQL\mysqlsh\mysqlshrc.[js|py]

    • On Unix: /etc/mysql/mysqlsh/mysqlshrc.[js|py]

  2. In the share/mysqlsh subdirectory of the MySQL Shell home folder, which can be defined by the environment variable MYSQLSH_HOME, or identified by MySQL Shell. If MYSQLSH_HOME is not defined, MySQL Shell identifies its own home folder as the parent folder of the folder named bin that contains the mysqlsh binary, if such a folder exists. (For many standard installations it is therefore not necessary to define MYSQLSH_HOME.)

    • On Windows: %MYSQLSH_HOME%\share\mysqlsh\mysqlshrc.[js|py]

    • On Unix: $MYSQLSH_HOME/share/mysqlsh/mysqlshrc.[js|py]

  3. In the folder containing the mysqlsh binary, but only if the MySQL Shell home folder described in option 2 is neither specified nor identified by MySQL Shell in the expected standard location.

    • On Windows: <mysqlsh binary path>\mysqlshrc.[js|py]

    • On Unix: <mysqlsh binary path>/mysqlshrc.[js|py]

  4. In the MySQL Shell user configuration path, as defined by the environment variable MYSQLSH_USER_CONFIG_HOME.

    • On Windows: %MYSQLSH_USER_CONFIG_HOME%\mysqlshrc.[js|py]

    • On Unix: $MYSQLSH_USER_CONFIG_HOME/mysqlshrc.[js|py]

  5. In the platform's standard user configuration path, but only if the MySQL Shell user configuration path described in option 4 is not specified.

    • On Windows: %APPDATA%\MySQL\mysqlsh\mysqlshrc.[js|py]

    • On Unix: $HOME/.mysqlsh/mysqlshrc.[js|py]