天天看點

MySQL使用痕迹清理~/.mysql_history

<a href="http://www.cnblogs.com/milantgh/p/3602206.html" target="_blank">MySQL使用痕迹清理~/.mysql_history</a>

mysql會給出我們最近執行的SQL指令和腳本;同linux command儲存在~/.bash_history一樣,你用mysql連接配接MySQL server的所有操作也會被記錄到~/.mysql_history檔案中,這樣就會有很大的安全風險了,如添加MySQL使用者的sql也同樣會被明文記錄到此檔案中。

1,檢視你系統的~/.mysql_history隐藏檔案

(我的測試環境下,一般linux的mysql使用者來管理,是以在/home/mysql目錄下會有這個檔案)

-bash-3.2$ ls -al | grep mysql_

-rw------- 1 mysql mysql 5006 Apr 10 18:53 .mysql_history

2,測試MySQL使用者管理的SQL會被記錄

mysql&gt; grant select on rep.* to his_user@localhost identified by '123';

Query OK, 0 rows affected (0.00 sec)

mysql&gt;

2.2 斷開剛才的mysql連接配接,檢視/home/mysql/.mysql_history檔案,可見剛才添加mysql user的操作已被記錄,包括明文密碼123.

-bash-3.2$ tail -1 ~/.mysql_history

grant select on rep.* to his_user@localhost identified by '123';

注意說明:這個.mysql_history不是隻存在于MySQL所在的Server, 任何你能夠遠端用mysql連接配接,都會在此server上的目前使用者的~目錄下建立這個隐藏檔案。

3, 如何清除使用痕迹,如果在生産環境中,一般不會依賴此記錄來作審計,從上面示範可以看出,還存在一定的風險。

2.1 完全清除~/.mysql_history。

2.1.1 删除現在的.mysql_history檔案

-bash-3.2$ rm ~/.mysql_history

2.1.2 建立它的軟連接配接(原因後面說明)

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

檢視軟連接配接建立成功。

lrwxrwxrwx 1 mysql mysql 9 Apr 10 20:30 .mysql_history -&gt; /dev/null

測試是否生效:連接配接mysql, 操作,斷開連接配接,檢視操作是否還被記錄。

mysql&gt; show databases;

+--------------------+

| Database |

| information_schema |

| backup |

-------------------------

mysql&gt; exit

Bye

-bash-3.2$ cat ~/.mysql_history

可見,上面的show databases;操作指令沒有被記錄,同時當你斷開ssh後,重新連接配接mysql, 此時按“向上”鍵已無曆史操作記錄提示,說明生效了。

想要重新生效, 你直接删除掉~/.mysql_history,當你下次再連接配接,退出後,就會重新建立此檔案了。

2.2 隻清除敏感資訊;如果不想完全禁用此功能,隻是想每次做一些敏感操作後,把此檔案清空便可以了。

-bash-3.2$ cat /dev/null &gt; ~/.mysql_history

比如你修改了MySQL使用者資訊,退mysql connection後,執行上操作,把全部操作痕迹清空了。

3 ~/.mysql_history檔案的産生原理

3.1 因為mysql工具本身就是有一個shell, 每次mysql連接配接退出後,都會把此次操作的資訊記錄到~/.mysql_history檔案中,

如果此檔案不存在,會先建立,再記錄(像上面的把它删除後,或才安裝的MySQL)

3.2 此檔案的名字,其實是根據MYSQL_HISTFILE這個linux環境變量來設定了, 預設是這個~/.mysql_history值,那我們測試一下其他值。

3.2.1 在linux使用者的~/.bash_profile 中添加一行export MYSQL_HISTFILE=/home/mysql/.mydb_history

目錄根據你的linux使用者自己設定,我的測試使用者是mysql.

-bash-3.2$ vi ~/.bash_profile

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

PATH=$PATH:/usr/sbin

export MYSQL_HISTFILE=/home/mysql/.mydb_history

3.2.2 退出linux連接配接,重新登入linux; 使用mysql來連接配接資料庫,操作,退出connection, 檢查~/.mydb_history隐藏檔案是否建立,并檢查剛才操作是否被記錄。

----------------------

//退出mysql

//檢視隐藏檔案是否建立

-bash-3.2$ cd ~; ls -tal | grep mydb_history

-rw------- 1 mysql mysql 16 Apr 10 20:55 .mydb_history

// 檢視“show databases"指令是否被正确記錄到檔案中

-bash-3.2$ tail -1 ~/.mydb_history

show databases;

從上可以說明:此檔案的檔案名來自于MYSQL_HISTFILE。

本文轉自 蔡小趙 51CTO部落格,原文連結:http://blog.51cto.com/zhaopeiyan/1972752