MongoDB安裝

jx_yu發表於2015-01-12

Mongodb安裝

環境說明

OS:Red Hat Enterprise Linux Server release 6.1 (Santiago)

MongoDB: mongodb-linux-x86_64-2.6.6.tgz

DownLoad mogodb

安裝MongoDB

#解壓

]# tar zxf mongodb-linux-x86_64-2.6.6.tgz

]# ls mongodb-linux-x86_64-2.6.6* -ld

drwxr-xr-x 3 root root      4096 1  12 15:38 mongodb-linux-x86_64-2.6.6

-rw-r--r-- 1 root root 116039527 1  12 10:18 mongodb-linux-x86_64-2.6.6.tgz

# mongodb不需要像別的資料庫那樣繁瑣的配置,解壓後放到慣例的/usr/local目錄,當然可以放到任意位置

]# mv mongodb-linux-x86_64-2.6.6 /usr/local/mongodb

]# cd /usr/local/mongodb

]# ls

bin  GNU-AGPL-3.0  README  THIRD-PARTY-NOTICES

建立datadirlogdir

mongodb建立資料庫存放的位置和日誌檔案,預設是在/data/db下面

#這裡我選擇/data/mongodb/data|logs作為datadirlogdir

~]# mkdir -p /data/mongodb/data

~]# mkdir -p /data/mongodb/logs

MongoDB啟動引數

#檢視mongodb啟動引數

~]# /usr/local/mongodb/bin/mongod --help

Options:

 

General options:

  -h [ --help ]               show this usage information

  --version                   show version information

  -f [ --config ] arg         configuration file specifying additional options

  -v [ --verbose ] [=arg(=v)] 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 - 1000000

                              by default

  --logpath arg               log file to send write to instead of stdout - has

                              to be a file, not directory

  --syslog                    log to system's syslog facility instead of file

                              or stdout

  --syslogFacility arg        syslog facility used for monogdb syslog message

  --logappend                 append to logpath instead of over-writing

  --timeStampFormat arg       Desired format for timestamps in log messages.

                              One of ctime, iso8601-utc or iso8601-local

  --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

  --httpinterface             enable http interface

  --clusterAuthMode arg       Authentication mode used for cluster

                              authentication. Alternatives are

                              (keyFile|sendKeyFile|sendX509|x509)

  --nounixsocket              disable listening on unix sockets

  --unixSocketPrefix arg      alternative directory for UNIX domain sockets

                              (defaults to /tmp)

  --fork                      fork server process

  --auth                      run with security

  --noauth                    run without security

  --ipv6                      enable IPv6 support (disabled by default)

  --jsonp                     allow JSONP access via http (has security

                              implications)

  --rest                      turn on simple rest api

  --slowms arg (=100)         value of slow for profile and console log

  --profile arg               0=off 1=slow, 2=all

  --cpu                       periodically show cpu and iowait utilization

  --sysinfo                   print some diagnostic system information

  --dbpath arg                directory for datafiles - defaults to /data/db

  --directoryperdb            each database will be stored in a separate

                              directory

  --noIndexBuildRetry         don't retry any index builds that were

                              interrupted by shutdown

  --noprealloc                disable data file preallocation - will often hurt

                              performance

  --nssize arg (=16)          .ns file size (in MB) for new databases

  --quota                     limits each database to a certain number of files

                              (8 default)

  --quotaFiles arg            number of files allowed per db, implies --quota

  --smallfiles                use a smaller default file size

  --syncdelay arg (=60)       seconds between disk syncs (0=never, but not

                              recommended)

  --upgrade                   upgrade db if needed

  --repair                    run repair on all dbs

  --repairpath arg            root directory for repair files - defaults to

                              dbpath

  --noscripting               disable scripting engine

  --notablescan               do not allow table scans

  --journal                   enable journaling

  --nojournal                 disable journaling (journaling is on by default

                              for 64 bit)

  --journalOptions arg        journal diagnostic options

  --journalCommitInterval arg how often to group/batch commit (ms)

  --shutdown                  kill a running server (for init scripts)

 

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

  --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 [/]

  --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

~]# /usr/local/mongodb/bin/mongod  --dbpath /data/mongodb/data --oplogSize 100 --logpath /data/mongodb/logs/mongodb.log --logappend --fork

about to fork child process, waiting until server is ready for connections.

forked process: 17937

child process started successfully, parent exiting

引數說明:

--dbpath                       指定儲存mongodb的資料目錄,預設/data/db

--oplogSize                  主節點的oplog大小(MB

--logpath                       日誌檔案位置

--logappend                使用追加方式記錄日誌

--fork                               使用守護程式方式啟動,放在後臺

#檢視mongodb日誌

~]#tail -f /data/mongodb/logs/mongodb.log

客戶端連線

~]# /usr/local/mongodb/bin/mongo

MongoDB shell version: 2.6.6

connecting to: test

> help

        db.help()                    help on db methods

        db.mycoll.help()             help on collection methods

        sh.help()                    sharding helpers

        rs.help()                    replica set helpers

        help admin                   administrative help

        help connect                 connecting to a db help

        help keys                    key shortcuts

        help misc                    misc things to know

        help mr                      mapreduce

 

        show dbs                     show database names

        show collections             show collections in current database

        show users                   show users in current database

        show profile                 show most recent system.profile entries with time >= 1ms

        show logs                    show the accessible logger names

        show log [name]              prints out the last segment of log in memory, 'global' is default

        use                 set current database

        db.foo.find()                list objects in collection foo

        db.foo.find( { a : 1 } )     list objects in foo where a == 1

        it                           result of the last line evaluated; use to further iterate

        DBQuery.shellBatchSize = x   set default number of items to display on shell

        exit                         quit the mongo shell

> show dbs

admin  (empty)

local  0.078GB

停止MongoDB

#方法一:kill pid

~]# ps -ef|grep mongod|grep -v grep

root     17937     1  0 15:54 ?        00:00:03 /usr/local/mongodb/bin/mongod --dbpath /data/mongodb/data --oplogSize 100 --logpath /data/mongodb/logs/mongodb.log --logappend --fork

~]# kill 17937

注:不能使用kill -9 pid來停止mongodb,這樣會導致直接關閉,可能使資料檔案摧毀

方法二:shutdown命令

~]# /usr/local/mongodb/bin/mongo

MongoDB shell version: 2.6.6

connecting to: test

> use admin

switched to db admin

> db.shutdownServer()

2015-01-12T16:31:59.746+0800 DBClientCursor::init call() failed

server should be down...

2015-01-12T16:31:59.749+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed

2015-01-12T16:31:59.749+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

2015-01-12T16:31:59.749+0800 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed

httpinterface

預設情況下MongoDB監聽27017

還會啟動一個基本的HTTP服務,HTTP的監聽埠比MongoDB埠高1000,也就是27017

2.6.6版本發現,預設是關閉的,使用--httpinterface引數開啟

之前的版本2.2.2預設是開啟的,使用--nohttpinterface 關閉

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27000195/viewspace-1399265/,如需轉載,請註明出處,否則將追究法律責任。

相關文章