170

When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command.

How can I delete that history permanently?

Bipul Jaishwal
  • 273
  • 2
  • 15
Frank Vilea
  • 8,323
  • 20
  • 65
  • 86

2 Answers2

346

You can clear your bash history like this:

history -cw

Eddie
  • 12,898
  • 3
  • 25
  • 32
  • 4
    Really like this answer - this takes effect immediately, rather than deleting `.bash_history` which requires the shell to be restarted to take effect. – mikemaccana Sep 11 '14 at 09:18
  • 1
    Does not work on my Ubuntu 14.04 machine. History just appears with new terminal. All that worked is `>~/bash_history`. Have to restart terminal for this though. – Aniket Thakur Jun 18 '15 at 16:53
  • Also works on Mac OS X El Capitan (tested on version 10.11.2), but you have to add that following line to your `~/.bash_profile`: `export SHELL_SESSION_HISTORY=0`, then do a `source ~/.bash_profile` and to finish quit and restart your Terminal app. If you want to understand what this export command does, then you should definitely check this following link: http://superuser.com/questions/950403/bash-history-not-preserved-between-terminal-sessions-on-mac – King-Wizard Jan 04 '16 at 18:33
  • 1
    isn't only -c enough? Manuals says -c clears the history list by deleting all of the entries. -w wirtes the current history to history file and append them to history list. Just -c works fine. – Kahn Feb 09 '17 at 14:43
26

If you use bash, then the terminal history is saved in a file called .bash_history. Delete it, and history will be gone.

However, for MySQL the better approach is not to enter the password in the command line. If you just specify the -p option, without a value, then you will be prompted for the password and it won't be logged.

Another option, if you don't want to enter your password every time, is to store it in a my.cnf file. Create a file named ~/.my.cnf with something like:

[client]
user = <username>
password = <password>

Make sure to change the file permissions so that only you can read the file.

Of course, this way your password is still saved in a plaintext file in your home directory, just like it was previously saved in .bash_history.

sagi
  • 5,619
  • 1
  • 30
  • 31
  • 4
    +1 for specifying `-p`. This is totally the correct approach for this problem – andyb Jul 15 '11 at 15:28
  • 2
    Actually, on my Linux system, bash remembers the history and recreates the file on logout. You'd have to log in, delete the file, log in again, log out the first shell, delete the file, and then log out the second shell. – cHao Jul 15 '11 at 15:30
  • Thanks for pointing that out. I thought I was being smart by using the -p+password option directly in the command because it was so much faster to just press the up arrow as I have a very long password. Time to reconsider.. – Frank Vilea Jul 15 '11 at 15:38
  • 6
    Don't forget to `unset HISTFILE` so that the current history isn't saved. – Ignacio Vazquez-Abrams Jul 15 '11 at 15:38
  • I edited my answer and added a solution that prevents from entering the password every time. – sagi Jul 15 '11 at 15:44
  • The better approach if you are using mysql 5.6 is the use of mysql_config_editor that uses an encrypted configuration file .mylogin.cnf instead the plain text .my.cnf [link](https://dev.mysql.com/doc/refman/5.6/en/mysql-config-editor.html) – PerroVerd May 05 '14 at 13:10