mysql清除数据痕迹_MySQL使用痕迹清理~/.mysql_history - milantgh
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會被記錄
2.1 用linux用戶mysql, 使用mysql命令行工具登錄MySQL server. 添加用戶" his_user@localhost
",并設置密碼
mysql> grant select on rep.* to his_user@localhost identified by '123';
Query OK, 0 rows affected (0.00 sec)
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
查看軟連接創建成功。
-bash-3.2$ ls -al | grep mysql_
lrwxrwxrwx 1 mysql mysql 9 Apr 10 20:30? .mysql_history -> /dev/null
測試是否生效:連接mysql, 操作,斷開連接,查看操作是否還被記錄。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| backup |
-------------------------
mysql> exit
Bye
-bash-3.2$ cat ~/.mysql_history
可見,上面的show databases;操作命令沒有被記錄,同時當你斷開ssh后,重新連接mysql, 此時按“向上”鍵已無歷史操作記錄提示,說明生效了。
想要重新生效, 你直接刪除掉~/.mysql_history,當你下次再連接,退出后,就會重新創建此文件了。
2.2 只清除敏感信息
;如果不想完全禁用此功能,只是想每次做一些敏感操作后,把此文件清空便可以了。
-bash-3.2$? cat /dev/null > ~/.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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
----------------------
//退出mysql
mysql> exit
Bye
//查看隱藏文件是否創建
-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。
總結
以上是生活随笔為你收集整理的mysql清除数据痕迹_MySQL使用痕迹清理~/.mysql_history - milantgh的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 5.6升级8.0_Mysql
- 下一篇: 原创:江苏拟设置一所本科师范院校,网友: