How to Search in the MySQL Command History
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
MySQL command history is a valuable resource for developers and DBAs, providing quick access to previously executed queries. This guide outlines how to effectively search and navigate through the MySQL command history.
MySQL stores a history of commands executed in the MySQL command-line tool. This history is typically saved in a hidden file in the user’s home directory, named .mysql_history.
To find the command history file, navigate to your home directory:
cd ~
ls -a | grep .mysql_history
The most common way to search through the command history is by using the grep command. For example, to find all instances of SELECT queries, use:
grep "SELECT" ~/.mysql_history
For a more interactive search, less can be used:
less ~/.mysql_history
In less, type / followed by the search term, and press Enter. Use n to jump to the next match and Shift + N to go to the previous match.
Combining cat and grep can also be useful:
cat ~/.mysql_history | grep "UPDATE"
Within the MySQL shell, you can navigate through the command history using the arrow keys. Press the Up arrow to scroll back and the Down arrow to move forward.
For a reverse search, press Ctrl + R and start typing the command. MySQL shell will autocomplete with matching commands from the history.
To list the most recent commands, use:
\\# or
\\history
For frequently used searches, consider creating aliases in your .bashrc or .bash_profile:
alias mysqlhist="cat ~/.mysql_history | grep"
To clear the MySQL command history:
> ~/.mysql_history
To limit the size of the history file, set the MYSQL_HISTFILE environment variable in your .bashrc or .bash_profile:
export MYSQL_HISTFILE=~/.mysql_history
export HISTSIZE=1000
Effectively searching the MySQL command history enhances productivity and offers a quick reference to previous commands. By using tools like grep, less, and MySQL shell features, developers can efficiently navigate and manage their command history.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.