標籤:直接 mysq 進程 多個 關係 命令 檔案大小 grep 日誌
今天客戶要求提供mongo的日誌清理方案,客戶提供了kill -SIGUSR1的方式,在此記錄學習以及測試過程,以及其他幾種日誌清理方式。
方法1:在mongo的shell 裡使用db.runCommand({logRotate:1})來進行日誌的整理:
操作如下:
shard1:PRIMARY> use admin
switched to db admin
shard1:PRIMARY> db.runCommand({logRotate:1})
{ "ok" : 1 }
操作前後日誌目錄狀態:
前:
[[email protected] log]# ll
total 600
-rw-rw-r-- 1 mongo mongo 156703 May 8 22:43 arbiter.log
-rw-rw-r-- 1 mongo mongo 446123 May 8 22:29 mongod.log
後:
[[email protected] log]# ll
total 604
-rw-rw-r-- 1 mongo mongo 156703 May 8 22:43 arbiter.log
-rw-r--r-- 1 root root 1114 May 8 22:44 mongod.log
-rw-rw-r-- 1 mongo mongo 446123 May 8 22:29 mongod.log.2018-05-08T14-44-56
執行命令後,複製出一份新的以時間命名的記錄檔。
方法2:使用客戶提供的命令kill -SIGUSR1的命令。
首先查詢mongod 的進程號碼:
[[email protected] log]# ps -ef|grep mongod
root 1335 1 1 22:15 ? 00:00:53 /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/mongod.conf
root 1620 1416 0 23:09 pts/1 00:00:00 grep mongod
找到mongod的進程號碼 1335,然後執行:
[[email protected] log]# kill -SIGUSR1 1335
前:
[[email protected] log]# ll
total 12
-rw-r--r-- 1 root root 1306 May 8 23:13 arbiter.log
-rwxr-xr-x 1 root root 356 May 8 23:08 clear-log.sh
-rw-r--r-- 1 root root 1540 May 8 23:23 mongod.log
後:
[[email protected] log]# ll
total 16
-rw-r--r-- 1 root root 1306 May 8 23:13 arbiter.log
-rwxr-xr-x 1 root root 356 May 8 23:08 clear-log.sh
-rw-r--r-- 1 root root 1301 May 8 23:30 mongod.log
-rw-r--r-- 1 root root 1540 May 8 23:23 mongod.log.2018-05-08T15-30-42
執行完命令kill -SIGUSR1 1335(進程號)後,產生了原日誌mongod.log檔案大小相同,以時間命名的新的記錄檔。
方法3:使用命令killall -SIGUSR1 mongod
執行命令:[[email protected] log]# killall -SIGUSR1 mongod
前:
[[email protected] log]# ll
total 12
-rw-r--r-- 1 root root 1306 May 8 23:13 arbiter.log
-rwxr-xr-x 1 root root 356 May 8 23:08 clear-log.sh
-rw-r--r-- 1 root root 1301 May 8 23:30 mongod.log
後:
[[email protected] log]# ll
total 20
-rw-r--r-- 1 root root 1306 May 8 23:33 arbiter.log
-rw-r--r-- 1 root root 1306 May 8 23:13 arbiter.log.2018-05-08T15-33-49
-rwxr-xr-x 1 root root 356 May 8 23:08 clear-log.sh
-rw-r--r-- 1 root root 1301 May 8 23:33 mongod.log
-rw-r--r-- 1 root root 1301 May 8 23:30 mongod.log.2018-05-08T15-33-49
這個命令可以清理一台伺服器上部署多個mongod的情況。
方法4:
CP,>,最原始的辦法。
cp mongodb17.log mongodb17.log.bak.2013-05-21.10-40
> mongodb17.log
可根據實際需要去選擇,對於mongo 資料庫,個人推薦使用kill -SIGUSR1 這個命令,這個命令在redis上嘗試使用,直接把redis的進程給殺死了。使用在mysql上,雖然進程沒死,但是也和日誌沒關係。所以這個命令只適合mongo。
mongodb的日誌清理學習整理