mongod linux上常用操作

來源:互聯網
上載者:User
  1. 使用下面命令可以查看服務的啟動參數
 mongod --help  或者mongod -h 
  1. 其他命令參數
 * 基本配置 
 --quiet                          # 安靜輸出  --port arg                       # 指定服務連接埠號碼,預設連接埠27017  --bind_ip arg                    # 綁定服務IP,若綁定127.0.0.1,則只能本機訪問,不指定預設本地所有IP  --logpath arg                    # 指定MongoDB記錄檔,注意是指定檔案不是目錄  --logappend                      # 使用追加的方式寫日誌  --pidfilepath arg                # PID File 的完整路徑,如果沒有設定,則沒有PID檔案  --keyFile arg                    # 叢集的私密金鑰的完整路徑,只對於Replica Set 架構有效  --unixSocketPrefix arg           # UNIX域通訊端替代目錄,(預設為 /tmp)  --fork                           # 以守護進程的方式運行MongoDB,建立伺服器處理序  --auth                           # 啟用驗證  --cpu                            # 定期顯示CPU的CPU利用率和iowait  --dbpath arg                     # 指定資料庫路徑  --diaglog arg                    # diaglog選項 0=off 1=W 2=R 3=both 7=W+some reads  --directoryperdb                 # 設定每個資料庫將被儲存在一個單獨的目錄  --journal                        # 啟用日誌選項,MongoDB的資料操作將會寫入到journal檔案夾的檔案裡  --journalOptions arg             # 啟用日誌診斷選項  --ipv6                           # 啟用IPv6選項  --jsonp                          # 允許JSONP形式通過HTTP訪問(有安全影響)  --maxConns arg                   # 最大同時串連數 預設2000  --noauth                         # 不啟用驗證  --nohttpinterface                # 關閉http介面,預設關閉27018連接埠訪問  --noprealloc                     # 禁用資料檔案預分配(往往影響效能)  --noscripting                    # 禁用指令碼引擎  --notablescan                    # 不允許表掃描  --nounixsocket                  # 禁用Unix通訊端監聽  --nssize arg (=16)               # 設定信資料庫.ns檔案大小(MB)  --objcheck                       # 在收到客戶資料,檢查的有效性,  --profile arg                    # 檔案參數 0=off 1=slow, 2=all  --quota                          # 限制每個資料庫的檔案數,設定預設為8  --quotaFiles arg                 #  number of files allower per db, requires --quota  --rest                           # 開啟簡單的rest API  --repair                         # 修複所有資料庫run repair on all dbs  --repairpath arg                 # 修複庫產生的檔案的目錄,預設為目錄名稱dbpath  --slowms arg (=100)              # value of slow for profile and console log  --smallfiles                     # 使用較小的預設檔案  --syncdelay arg (=60)            # 資料寫入磁碟的時間秒數(0=never,不推薦)  --sysinfo                        # 列印一些診斷系統資訊  --upgrade                        # 如果需要升級資料庫 
 * Replicaton 參數 
 --fastsync                      # 從一個dbpath裡啟用從庫複製服務,該dbpath的資料庫是主庫的快照,可用於快速啟用同步  --autoresync                    # 如果從庫與主庫同步資料差得多,自動重新同步,  --oplogSize arg                 # 設定oplog的大小(MB) 
 * 主/從參數 
 --master                        # 主庫模式  --slave                         # 從庫模式  --source arg                    # 從庫 連接埠號碼  --only arg                      # 指定單一的資料庫複寫  --slavedelay arg                # 設定從庫同步主庫的延遲時間    * Replica set(複本集)選項: 
 --replSet arg                   # 設定複本集名稱 
 * Sharding(分區)選項 
 --configsvr                    # 聲明這是一個叢集的config服務,預設連接埠27019,預設目錄/data/configdb  --shardsvr                     # 聲明這是一個叢集的分區,預設連接埠27018  --noMoveParanoia               # 關閉偏執為moveChunk資料儲存? 

 

#上述參數都可以寫入mongod.conf配置文檔裡例如:

dbpath = /data/mongodb

logpath = /data/mongodb/mongodb.log

logappend = true

port = 27017

fork = true

auth = true

 

監控(mongostat)

Use the mongostat utility to quickly view statistics on a running mongod instance.

Run mongostat --help for help.

Fields:   insert       - # of inserts per second (* means replicated op)   query        - # of queries per second   update       - # of updates per second   delete       - # of deletes per second   getmore      - # of get mores (cursor batch) per second   command      - # of commands per second (on a slave, it's local|replicated)   flushes      - # of fsync flushes per second   mapped       - amount of data mmaped (total data size) megabytes   vsize        - virtual size of process in megabytes   res          - resident size of process in megabytes   faults       - # of pages faults/sec (linux only)   locked       - percent of time in global write lock   idx miss     - percent of btree page misses (sampled)   qr | qw      - queue lengths for clients waiting (read|write)   ar | aw      - active clients (read|write)   netIn        - network traffic in - bits    netOut       - network traffic out - bits   conn         - number of open connections   set          - replica set name    repl         - replication type                     M    - master                    SEC  - secondary                     REC  - recovering                    UNK  - unknown                    SLV  - slave

multiple servers:

mongostat --host a,b,c

find all connected servers (added in 1.7.2):

mongostat --discover (--host optional)

 

 

備份(mongodump

 用法 :

[root@web3 3]# mongodump --help

options:

--help produce help message

-v [ --verbose ] be more verbose (include multiple times for more

verbosity e.g. -vvvvv)

-h [ --host ] arg mongo host to connect to ("left,right" for pairs)

-d [ --db ] arg database to use

-c [ --collection ] arg collection to use (some commands)

-u [ --username ] arg username

-p [ --password ] arg password

--dbpath arg directly access mongod data files in the given path,

instead of connecting to a mongod instance - needs

to lock the data directory, so cannot be used if a

mongod is currently accessing the same path

--directoryperdb if dbpath specified, each db is in a separate

directory

-o [ --out ] arg (=dump) output directory

 

例子:

[root@web3 ~]# mongodump -h 192.168.1.103 -d citys -o /backup/mongobak/3

connected to: 192.168.1.103

DATABASE: citys to /backup/mongobak/3/citys

citys.building to /backup/mongobak/3/citys/building.bson

13650 objects

citys.system.indexes to /backup/mongobak/3/citys/system.indexes.bson

1 objects

 

備份出來的資料是二進位的,已經經過壓縮。

 

恢複(mongorestore

[root@web3 3]# mongorestore --help

usage: mongorestore [options] [directory or filename to restore from]

options:

--help produce help message

-v [ --verbose ] be more verbose (include multiple times for more

verbosity e.g. -vvvvv)

-h [ --host ] arg mongo host to connect to ("left,right" for pairs)

-d [ --db ] arg database to use

-c [ --collection ] arg collection to use (some commands)

-u [ --username ] arg username

-p [ --password ] arg password

--dbpath arg directly access mongod data files in the given path,

instead of connecting to a mongod instance - needs to

lock the data directory, so cannot be used if a

mongod is currently accessing the same path

--directoryperdb if dbpath specified, each db is in a separate

directory

--drop drop each collection before import

--objcheck validate object before inserting

 

--drop參數可以在匯入之前把collection先刪掉。

例子:

[root@web3 3]# mongorestore -h 127.0.0.1 --directoryperdb /backup/mongobak/3/

connected to: 127.0.0.1

/backup/mongobak/3/citys/building.bson

going into namespace [citys.building]

13667 objects

/backup/mongobak/3/citys/system.indexes.bson

going into namespace [citys.system.indexes]

1 objects

 

另外mongodb還提供了mongoexport和mongoimport這兩個命令來匯出或匯入資料,匯出的資料是json格式的。也可以實現備份和恢複的功能。

例:

mongoexport -d mixi_top_city_prod -c building_45 -q '{ "uid" : "10832545" }' > mongo_10832545.bson

mongoimport -d mixi_top_city -c building_45 --file mongo_10832545.bson

 

關閉

kill -9可能導致癱瘓

建議killall mongod

 

修複
首先停止mongod服務,刪除mongodb.log,也可以備份一下
# rm -rf /data/mongodb/mongodb.log

刪除mongodb進程檔案
# rm -rf /mongodb/mongod.lock

進行修複
# mongod --repair --dbpath /data/mongodb --repairpath /data/mongodb_repair --port=27017

這時mongodb進程會在/mongodb/repair/目錄下儲存臨時的修複資料庫檔案,檔案目錄為“$tmp_repairDatabase_0”所以此目錄空間要足夠大。
生產環境資料庫為100G,修複進行了大約3個半小時,在“/mongodb/repair /$tmp_repairDatabase_0"目錄下產生了近30G的資料庫臨時檔案,修複完成後資料庫臨時檔案自動清除。

主從同步

啟動主伺服器(192.168.61.200)

./mongod -dbpath /data/jobcnresume -port 5555 -master

 

啟動從伺服器

./mongod -slave -source=192.168.61.200:5555 -dbpath=/data/db2 -port 6666 -slavedelay 5

./mongod -slave -source=192.168.61.200:5555 -dbpath=/data/db3 -port 7777 -slavedelay 5

參數:

--source主伺服器ip和連接埠

--autoresync當發現從伺服器的資料不是最新時,開始從主伺服器請求同步資料

--slavedelay同步延遲,單位:秒

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.