MongoDB在Linux下安裝

mongodb安裝方式

1.下載安裝包並解壓

2.規劃數據庫安裝目錄

shell> mkdir -p /db/mongo/data
shell> mkdir -p /db/mongo/bin
shell> mv mongodb-linux-i686-2.4.10/bin/* /db/mongo/bin/
shell> touch /db/mongo/logs

將/db/mongo/bin加入環境變量,方便直接使用

3.啓動mongodb

shell> mongod --logpath=/db/mongo/logs --logappend --journal --dbpath=/db/mongo/data --port=27017 --fork

4.測試mongodb是否安裝成功

shell> mongo
MongoDB shell version: 2.4.10
connecting to: test

> show dbs                   --列出所有db
local        0.03125GB

> db                         --顯示當前db
test

5.將MongoDB服務加入開機啓動
vi /etc/rc.local在最後追加
mongod --logpath=/db/mongo/logs --logappend --dbpath=/db/mongo/data --port=27017 --fork

mongodb啓動方式

  • 命令行方式啓動
  • 配置文件方式啓動
    與命令行方式相同,指定語法“選項=值” 區分大小寫
    命令行中如--fork的開關選項其值設置爲true
shell> mongod --help
Allowed options:

General options:
  -h [ --help ]               show this usage information
  --version                   show version information
  -f [ --config ] arg         configuration file specifying additional options
  -v [ --verbose ]            be more verbose (include multiple times for more 
                              verbosity e.g. -vvvvv)
  --quiet                     quieter output
  --port arg                  specify port number - 27017 by default
  --bind_ip arg               comma separated list of ip addresses to listen on
                              - all local ips by default
  --maxConns arg              max number of simultaneous connections - 20000 by
                              default
  --logpath arg               log file to send write to instead of stdout - has
                              to be a file, not directory
  --logappend                 append to logpath instead of over-writing
  --pidfilepath arg           full path to pidfile (if not set, no pidfile is 
                              created)
  --keyFile arg               private key for cluster authentication
  --setParameter arg          Set a configurable parameter
  --nounixsocket              disable listening on unix sockets
  --unixSocketPrefix arg      alternative directory for UNIX domain sockets 
                              (defaults to /tmp)
  --fork                      fork server process   以守護進程方式運行,創建服務器進程
  --syslog                    log to system's syslog facility instead of file 
                              or stdout
  --auth                      run with security
  --cpu                       periodically show cpu and iowait utilization
  --dbpath arg                directory for datafiles - defaults to /data/db/
  --diaglog arg               0=off 1=W 2=R 3=both 7=W+some reads
  --directoryperdb            each database will be stored in a separate 
                              directory
  --ipv6                      enable IPv6 support (disabled by default)
  --journal                   enable journaling
  --journalCommitInterval arg how often to group/batch commit (ms)
  --journalOptions arg        journal diagnostic options
  --jsonp                     allow JSONP access via http (has security 
                              implications)
  --noauth                    run without security
  --nohttpinterface           disable http interface
  --nojournal                 disable journaling (journaling is on by default 
                              for 64 bit)
  --noprealloc                disable data file preallocation - will often hurt
                              performance
  --noscripting               disable scripting engine
  --notablescan               do not allow table scans
  --nssize arg (=16)          .ns file size (in MB) for new databases
  --profile arg               0=off 1=slow, 2=all
  --quota                     limits each database to a certain number of files
                              (8 default)
  --quotaFiles arg            number of files allowed per db, requires --quota
  --repair                    run repair on all dbs
  --repairpath arg            root directory for repair files - defaults to 
                              dbpath
  --rest                      turn on simple rest api
  --shutdown                  kill a running server (for init scripts)
  --slowms arg (=100)         value of slow for profile and console log
  --smallfiles                use a smaller default file size
  --syncdelay arg (=60)       seconds between disk syncs (0=never, but not 
                              recommended)
  --sysinfo                   print some diagnostic system information
  --upgrade                   upgrade db if needed

Replication options:
  --oplogSize arg       size to use (in MB) for replication op log. default is 
                        5% of disk space (i.e. large is good)

Master/slave options (old; use replica sets instead):
  --master              master mode
  --slave               slave mode
  --source arg          when slave: specify master as <server:port>
  --only arg            when slave: specify a single database to replicate
  --slavedelay arg      specify delay (in seconds) to be used when applying 
                        master ops to slave
  --autoresync          automatically resync if slave data is stale

Replica set options:
  --replSet arg           arg is <setname>[/<optionalseedhostlist>]
  --replIndexPrefetch arg specify index prefetching behavior (if secondary) 
                          [none|_id_only|all]

Sharding options:
  --configsvr           declare this is a config db of a cluster; default port 
                        27019; default dir /data/configdb
  --shardsvr            declare this is a shard db of a cluster; default port 
                        27018

mongodb停止方式

  • 向mongodb服務器發送一個SIGINT或者SIGTERM 信號
  • 使用shutdown命令
[1]
使用kill -2|-15  pid
等到當前運行的操作或者文件預分配完成,關閉所有打開的連接,將緩存的數據刷新到磁盤,最後停止
不能使用(kill -9) 導致數據庫直接關閉,使數據文件損壞

[2]
>use admin
> db.shutdownServer()
Wed Jun 11 23:50:16.048 DBClientCursor::init call() failed
server should be down...
Wed Jun 11 23:50:16.049 trying reconnect to 127.0.0.1:27017
Wed Jun 11 23:50:16.069 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017

整理自網絡

svoid
2014-07-25

發佈了54 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章