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
$
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. Overview of MySQL information_schema Database With Practical Examples
  2. Get Quick Info On MySQL DB, Table, Column and Index Using mysqlshow
  3. Backup and Restore MySQL Database Using mysqldump
  4. Forgot MySQL Root Password – How To Reset It?
  5. 15 Practical Usages of Mysqladmin Command For Administering MySQL Server
  

Vim 101 Hacks Book

{ 1 comment… read it 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.

Leave a Comment

Previous post:

Next post: