Clear mysql command line history stored in ~/.mysql_history file

Q. How do I clear the mysql command line history stored in ~/.mysql_history file?

ADVERTISEMENTS

A. mysql is a simple SQL shell. It supports interactive and non-interactive use.

On Unix and Linux, the mysql client writes a record of executed statements to a history file. By default, the history file is named .mysql_history and is created in your home directory. To specify a different file, set the value of the MYSQL_HISTFILE environment variable.

Task: CLEAR MYSQL COMMAND LINE HISTORY

Remove ~/.mysql_history by typing following command:
$ > ~/.mysql_history

If you do not want to maintain a history file, first remove .mysql_history if it exists, and then use either of the following techniques:

Set the MYSQL_HISTFILE variable to /dev/null. To cause this setting to take effect each time you log in, put the setting in one of your shell’s startup files.

Create ~/.mysql_history as a symbolic link to /dev/null. You need run following command only once:
$ rm $HOME/.mysql_history
$ ln -s /dev/null $HOME/.mysql_history

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • charon Jun 8, 2007 @ 7:36

    Setting MYSQL_HISTFILE to /dev/null is not such a good idea as mysql tries to creates the file on startup. Thus it replaces /dev/null by a regular file accessible only by root – not something you want to do on your server system!
    In case you already tried and hosed your /dev/null, the following command will restore it:
    mknod -m 666 /dev/null c 1 3
    charon

  • 🐧 nixCraft Jun 8, 2007 @ 18:40

    charon,

    This a sym link, so it will not remove /dev/null and you should never use root account to connect to mysql as root. You can do that from normal user too..

    Appreciate your comment.

  • charon Jun 9, 2007 @ 19:01

    nixcraft,
    if you set the environment variable MYSQL_HISTFILE to /dev/null, there is no symlink but the file is accessed directly. If you create the symlink, you don’t run into the troubles I described. Of course you’re right, you should never ever work as root, but… you know…
    charon

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.