1.前往官網(wǎng)下載MongoDB壓縮包
image
2.將下載好的壓縮包解壓,將解壓出的文件夾下的內(nèi)容全部復(fù)制到新的路徑下势腮。
cp -r mongodb-osx-x86_64-3.0.4 /usr/local/mongodb
3.在新建立的文件夾下建立data文件夾用來(lái)記錄數(shù)據(jù),log文件夾用來(lái)記錄日志
cd /usr/local/mongodb
mkdir data
mkdir log
4.進(jìn)入bin目錄下,創(chuàng)建mongodb.conf配置文件
cd bin
vim mongodb.conf
5.編寫(xiě)配置文件
port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
fork = true
port: 數(shù)據(jù)庫(kù)服務(wù)使用端口
dbpath: 數(shù)據(jù)存放的文件位置
logpath: 日志文件的存放位置
fork: 后臺(tái)守護(hù)進(jìn)程運(yùn)行
6.啟動(dòng)
在bin路徑下贫堰,執(zhí)行
./mongod -f mongodb.conf
-f 后面寫(xiě)要使用的配置文件
啟動(dòng)成功后會(huì)打印類(lèi)似于這樣的信息:
about to fork child process, waiting until server is ready for connections.
forked process: 779
child process started successfully, parent exiting
如果未啟動(dòng)成功,錯(cuò)誤信息如下的話(huà):
about to fork child process, waiting until server is ready for connections.
forked process: 760
ERROR: child process failed, exited with error number 1
一般情況下是權(quán)限問(wèn)題馅精,使用sodu操作來(lái)解決,
也可能是配置文件中路徑寫(xiě)的有問(wèn)題粱檀。
7.在bin目錄下進(jìn)入MongoDB控制臺(tái)
./mongo
8.關(guān)閉MongoDB服務(wù)
在 ./mongo 進(jìn)入控制臺(tái)后洲敢,輸入 use admin,然后輸入 db.shutdownServer()
9.查看 mongo 用法
在bin目錄下輸入:
./mongo --help
內(nèi)容顯示如下
MongoDB shell version: 3.0.4
usage: ./mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo? ? ? ? ? ? ? ? ? foo database on local machine
192.169.0.5/foo? ? ? foo database on 192.168.0.5 machine
192.169.0.5:9999/foo? foo database on 192.168.0.5 machine on port 9999
Options:
--shell? ? ? ? ? ? ? ? ? ? ? ? ? ? run the shell after executing files
--nodb? ? ? ? ? ? ? ? ? ? ? ? ? ? don't connect to mongod on startup - no
'db address' arg expected
--norc? ? ? ? ? ? ? ? ? ? ? ? ? ? will not run the ".mongorc.js" file on
start up
--quiet? ? ? ? ? ? ? ? ? ? ? ? ? ? be less chatty
--port arg? ? ? ? ? ? ? ? ? ? ? ? port to connect to
--host arg? ? ? ? ? ? ? ? ? ? ? ? server to connect to
--eval arg? ? ? ? ? ? ? ? ? ? ? ? evaluate javascript
-h [ --help ]? ? ? ? ? ? ? ? ? ? ? show this usage information
--version? ? ? ? ? ? ? ? ? ? ? ? ? show version information
--verbose? ? ? ? ? ? ? ? ? ? ? ? ? increase verbosity
--ipv6? ? ? ? ? ? ? ? ? ? ? ? ? ? enable IPv6 support (disabled by default)
Authentication Options:
-u [ --username ] arg? ? ? ? ? ? ? username for authentication
-p [ --password ] arg? ? ? ? ? ? ? password for authentication
--authenticationDatabase arg? ? ? user source (defaults to dbname)
--authenticationMechanism arg? ? ? authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg? ? ? ? ? ? ? Remote host name to use for purpose of
GSSAPI/Kerberos authentication
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
連接數(shù)據(jù)庫(kù)
在bin目錄下執(zhí)行:
./mongo 127.0.0.1:27017/test
./mongo + 服務(wù)器IP:端口號(hào)/數(shù)據(jù)庫(kù)名
服務(wù)器IP我這寫(xiě)的是本機(jī),端口號(hào)是剛才在配置文件中配置的那個(gè)茄蚯,數(shù)據(jù)庫(kù)名自己起
回車(chē)后如果能看到向右的箭頭压彭,說(shuō)明已經(jīng)成功了
11.查看日志
tail -f log/mongodb.log
log/mongod.log 為日志存放路徑
12.配置開(kāi)機(jī)啟動(dòng)
01.確保終端運(yùn)行 somepath/mongod --config somepath/mongodb.conf 可正常啟動(dòng)
somepath指用戶(hù)特定路徑,由用戶(hù)自己決定
02.創(chuàng)建一個(gè)plist文件
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
Label
org.mongodb.mongod
ProgramArguments
/usr/local/mongodb/bin/mongod
run
--config
/usr/local/mongodb/bin/mongodb.conf
RunAtLoad
KeepAlive
WorkingDirectory
/usr/local/mongodb
StandardErrorPath
/usr/local/mongodb/log/error.log
StandardOutPath
/usr/local/mongodb/log/output.log
放置在/Library/LaunchDaemons目錄下渗常,如果不在這個(gè)目錄壮不,就放到對(duì)應(yīng)正確路徑下
03.執(zhí)行命令,將mongo服務(wù)加入開(kāi)機(jī)啟動(dòng)進(jìn)程中
launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
篇尾小結(jié):
使用cp命令拷貝文件皱碘,加上 -r參數(shù) 表示級(jí)聯(lián)拷貝
在mongod工具后 使用--config 用于讀取配置文件中的配置項(xiàng)
在執(zhí)行啟動(dòng)mongo服務(wù)時(shí)加上sudo
4.執(zhí)行./mongo可進(jìn)入mongo服務(wù)臺(tái)
tail命令配合-f參數(shù)可以用于查看日志
轉(zhuǎn)載-----鏈接:http://www.reibang.com/p/3ef9f9678358