MySQL Shell 8.0  /  MySQL Shell Code Execution  /  MySQL Shell Code History

3.3 MySQL Shell Code History

Code which you issue in MySQL Shell is saved to the history, which can then be accessed using the up and down arrow keys. The shell.options["history.autoSave"] option must be set to ensure history is automatically saved.

History entries are saved to the ~/.mysqlsh/history file on Linux and Mac, or the %AppData%\MySQL\mysqlsh\history file on Windows. The user configuration path can be overridden on all platforms by defining the environment variable MYSQL_USER_CONFIG_HOME. The value of this variable replaces %AppData%\MySQL\mysqlsh\ on Windows or ~/.mysqlsh/ on Unix.

Issuing \history shows history entries in the order that they were issued with their history entry number, which can be used with the \history delete entry_number command. The numbering of history entries only resets after restarting MySQL Shell (or after issuing \history clear), meaning each entry keeps its entry number even when the oldest entries are discarded.

You can customize the entries added to the history using the --histignore command option.

You can search the history using the incremental history search feature. To search the history, use control-r to search backwards, or control-s to search forwards through the history. Once the search is active, typing characters searches for any strings that match them in the history and displays the first match. Use control-s or control-r to search for further matches to the current search term. Typing more characters further refines the search. During a search you can press the arrow keys to continue stepping through the history from the current search result. Press Enter to accept the displayed match. Use control-c to cancel the search.

History entries are saved to the history file, which is readable only by the owner user and is created automatically by MySQL Shell. Only code which you type interactively at the MySQL Shell prompt is saved to the history. Code that is executed indirectly or internally, for example when the \source command is executed, is not added to the history. When you issue multi-line code, the new line characters are stripped in the history entry. If the same code is issued multiple times it is only stored in the history once, reducing duplication. The shell.options["history.maxSize"]=number option sets the maximum number of entries to store in the history, the default is 1000. If the number of history entries exceeds the configured maximum, the oldest entries are removed. If the maximum is set to 0, no history entries are saved. Old history entries are also deleted when MySQL Shell exits. If the history file cannot be read or written to, MySQL Shell logs an error message and skips the read or write operation.

Additionally, when using MySQL Shell in SQL mode, you can configure strings which should not be added to the history. By default strings that match the glob patterns IDENTIFIED, or PASSWORD are not added to the history. To configure further strings to match use either the --histignore command option, or shell.options["history.sql.ignorePattern"]. Multiple strings can be specified, separated by a colon (:). The history matching uses case insensitive glob pattern like matching. Supported wildcards are * (match any 0 or more characters) and ? (match exactly 1 character). The default strings are specified as PASSWORD:IDENTIFIED.