MySQL Shell 9.1  /  MySQL Shell Code Execution  /  Code History

5.5 Code History

Code which you issue in MySQL Shell is stored in the history, which can then be accessed using the up and down arrow keys. You can also search the history using the incremental history search feature. To search the history, use Ctrl+R to search backwards, or Ctrl+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 Ctrl+S or Ctrl+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 Ctrl+C to cancel the search.

The history.maxSize MySQL Shell configuration 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 and discarded. If the maximum is set to 0, no history entries are stored.

By default the history is not saved between sessions, so when you exit MySQL Shell the history of what you issued during the current session is lost. You can save your history between sessions by enabling the MySQL Shell history.autoSave option. For example, to make this change permanent issue:

mysqlsh-js> \option --persist history.autoSave=1

When the history.autoSave option is enabled the history is stored in the MySQL Shell configuration path, which is the ~/.mysqlsh directory on Linux and macOS, or the %AppData%\MySQL\mysqlsh folder on Windows. This path can be overridden on all platforms by defining the environment variable MYSQLSH_USER_CONFIG_HOME. The saved history is created automatically by MySQL Shell and is readable only by the owner user. If the history file cannot be read or written to, MySQL Shell logs an error message and skips the read or write operation. History is split per active language and the files are named history.sql, history.js and history.py.

Issuing the MySQL Shell \history command shows history entries in the order that they were issued, together with their history entry number, which can be used with the \history delete entry_number command. You can manually delete individual history entries, a specified numeric range of history entries, or the tail of the history.  You can also use \history clear to delete the entire history manually.  When you exit MySQL Shell, if the history.autoSave configuration option has been set to true, the history entries that remain in the history file are saved, and their numbering is reset to start at 1. If the shell.options["history.autoSave"] configuration option is set to false, which is the default, the history file is cleared.

Only code which you type interactively at the MySQL Shell prompt is added 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.

You can customize the entries that are added to the history using the --histignore command option. Additionally, when using MySQL Shell in SQL mode, you can configure strings which should not be added to the history. This history ignore list is also applied when you use the \sql command with a query to execute single SQL statements while another language is active.

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 "*IDENTIFIED*:*PASSWORD*".

The most recent executed statement is always available by pressing the Up arrow, even if the history ignore list applies to it. This is so that you can make corrections without retyping all the input. If filtering applies to the last executed statement, it is removed from the history as soon as another statement is entered, or if you exit MySQL Shell immediately after executing the statement.