Disable MySQL History – Clear ~/.mysql_history and MYSQL_HISTFILE

by Ramesh Natarajan on January 15, 2010

Question: How do I disable mysql history? I don’t want mysql to remember the previous commands that I typed from the mysql> prompt. This is important for me, as when I type some sql commands that contains passwords, I see the clear text password stored in the ~/.mysql_history, which I don’t want to happen.

Answer: Bash history feature stores the Unix commands typed in the command line in the ~/.bash_history file. Similar to bash shell, mysql stores the commands typed in the mysql> prompt in the ~/.mysql_history file.

In this article, let us review how to disable mysql history.

1. Execute some sql commands from mysql> prompt

Connect to mysql from the unix command line and execute few sql commands as shown below.

$ mysql -u root -pyour-password

mysql> show databases;
mysql> use information_schema;
mysql> show tables;
mysql> select table_name, table_rows from tables;

Note: Now, if you press up arrow, you can see all the previous commands you’ve typed from the mysql prompt.

2. ~/.mysql_history file stores the mysql history

Exit from the mysql command prompt and view the ~/.mysql_history file that will contain all the sql commands you executed from the mysql command prompt.

$ cat ~/.mysql_history
select * from versions;
show databases;
use information_schema;
show tables;
select table_name, table_rows from tables;

3. Disable mysql history using MYSQL_HISTFILE environment variable

First, remove the ~/.mysql_history file

$ rm ~/.mysql_history

Next, set the MYSQL_HISTFILE env variable to /dev/null

$ export MYSQL_HISTFILE=/dev/null

$ set | grep MYSQ
MYSQL_HISTFILE=/dev/null

Now, login to the mysql and execute few sql commands. You’ll notice that ~/.mysql_history file is not getting created anymore.

$ mysql -u root -pyour-password
mysql> show databases;
mysql> use information_schema;
mysql> show tables;
mysql> select table_name, table_rows from tables;

$ cat ~/.mysql_history
cat: /home/ramesh/.mysql_history: No such file or directory

4. Disable mysql history by pointing .mysql_history to /dev/null

First, remove the ~/.mysql_history file

$ rm ~/.mysql_history

Next, create a symbolic link of ~/.mysql_history pointing to /dev/null as shown below.

$ ln -s /dev/null ~/.mysql_history

$ ls -l .mysql_history
lrwxrwxrwx 1 ramesh admin 9 Dec 26 19:18 /home/ramesh/.mysql_history -> /dev/null

Now, login to the mysql and execute few sql commands. You’ll notice that ~/.mysql_history file is empty and does not store any previously typed commands.

$ mysql -u root -pyour-password
mysql> show databases;
mysql> use information_schema;
mysql> show tables;
mysql> select table_name, table_rows from tables;

$ cat ~/.mysql_history
$

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 3 comments… read them below or add one }

1 RokP January 15, 2010 at 6:18 am

Or you can use .bash_logout file in your ~/ directory. Commands in this files are executed before logout is called. Just put echo -n > ~/.mysq_history, and when u will logout of the system, mysql_history will be cleared.

2 Uhix Blood June 28, 2011 at 3:47 am

doing the bash_logout is not enough, because every admin with a brain uses screen and these kind of sessions can be very long – so it is important to not create the mysql_history in the first place, not only delete-after-creation. RokP, you would not have passed the exam.

3 Diederik van der Boor June 19, 2012 at 9:13 am

There is no need to run `mysql -u root -pyour-password`, as it will get your password in the .bash_history file. Instead use `mysql -u root -p` without any password, so mysql will prompt for it.

Leave a Comment

Previous post:

Next post: