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

4.4 MySQL Shell 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 shell.options["history.maxSize"]=number 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.

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. The history file 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.

Issuing the MySQL Shell command \history 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 shell.options["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. 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*". Note that regardless of the filters set in the history ignore list, the last executed statement is always available to be recalled by pressing the Up arrow, 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.