mongodb的日誌清理學習整理

來源:互聯網
上載者:User

標籤:直接   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的日誌清理學習整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.