몽고DB 파일을 아래 경로에서 내려 받는다.
https://www.mongodb.org/download
또는
MongoDB for Windows 64-bit [Download link]
zip파일을 다운로드 받은 후 압축을 푼다. 아래와 같이 C드라이브로 복사한 후 디렉토리 이름을 'mongob'로 변경한다.
c:\mongodb
그리고 다음 디렉토리도 생성한다.
c:\data\db
c:\mongodb\log
아래와 같이 mongod.cfg 확장자를 갖는 환경설정 파일을 만들며 c:\mongodb 폴더 안에 저장한다.
##Which IP address(es) mongod should bind to.
bind_ip = 127.0.0.1
##Which port mongod should bind to.
port = 27017
##I set this to true, so that only critical events and errors are logged.
quiet = true
##store data here
dbpath=C:\data\db
##The path to the log file to which mongod should write its log messages.
logpath=C:\mongodb\log\mongo.log
##I set this to true so that the log is not overwritten upon restart of mongod.
logappend = true
##log read and write operations
diaglog=3
##It ensures write durability and data consistency much as any journaling scheme would be expected to do.
##Only set this to false if you don’t really care about your data (or more so, the loss of it).
journal = true
몽고DB 서버를 시작하기 위해서 명령 프롬프트에서 아래 명령을 사용한다.
mongod.exe --config d:\mongodb\mongo.config
c:\mongodb\bin>mongod --config c:\mongodb\mongod.cfg
2014-05-25T16:51:18.433+0530 warning: --diaglog is deprecated and will be removed in a future release
2014-05-25T16:51:18.434+0530 diagLogging level=3
2014-05-25T16:51:18.435+0530 diagLogging using file D:\mongodb\data/diaglog.5381d22e
그리고 몽고DB에 연결하기 위해서 아래와 같이 한다.
c:\mongodb\bin>mongo
c:\mongodb\bin>mongo
MongoDB shell version: 3.0.0
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see http://docs.mongodb.org/
Questions? Try the support group http://groups.google.com/group/mongodb-user
몽고DB를 중지시키려면, 인증된 사용자이어야 한다. admin 데이터베이스로 변경한 후 몽고DB를 중지키시키 위해 몽고DB 셸 클라이언트에 다음 명령을 사용한다.
db.shutdownServer()
현재 동작을 깔끔하게 정지하게 하며, mongod를 강제로 종료시킬 수 있다.
> use admin
switched to db admin
> db.shutdownServer()
2014-05-25T19:55:25.221+0530 DBClientCursor::init call() failed
server should be down...
2014-05-25T19:55:25.224+0530 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2014-05-25T19:55:26.225+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made b
ecause the target machine actively refused it.
2014-05-25T19:55:26.225+0530 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
> quit()
c:\mongodb\bin>
몽고DB를 설치하고 설정한 후 시동했다면, 몽고DB 셸을 통해 접근할 수 있다. 사용자 계정을 설정하는 것부터 데이터베이스 생성, 데이터베이스 내용을 질의하는 것까지 모든 작업을 위해 셸을 사용한다.
몽고DB 셸을 시작하기 위해 콘솔 프롬프트에서 mongo 명령을 실행하면 셸이 시작된다.
c:\mongodb\bin>mongo
몽고DB 셸이 자바스크립트 기반으로 하고 있기 때문에 대부분의 자바스크립트 문법을 사용할 수 있다.
option 인수로 도움말이 필요한 특정 영역을 지정할 수 있다.option 인수에 따라 목록을 보여준다. option의 값으로 다음 값들이 있다.global 이다.다음은 셸 메소드의 몇가지 예이다.
네이티브 메소드 전체 목록은 다음 URL에서 확인한다.
http://docs.mongodb.org/manual/reference/method/#native
몽고DB 셸의 명령, 메소드 그리고 데이터 구조는 대화형 자바스크립트를 기반으로 한다. 몽고DB를 관리하는 좋은 방법은 여러 번 수행될 수 있거나, 업그레이드와 같은 특정 시간에 실행될 준비를 할 수 있는 스크립트를 생성하는 것이다.
몽고DB 셸 스크립트를 실행하는 방법에는 두가지가 있다. 첫번째 방법은 명령 행에서 --eval을 사용해 실행하는 것이다. --eval 파라미터는 자바스크립트 문자열 또는 자바스크립트 파일을 받아 몽고DB 셸을 시작시킨 다음 곧바로 자바스크립트를 실행시킨다.
다음 명령은 몽고DB 셸을 시작시키고 db.getCollections()를 테스트 데이터베이스에 대해 실행시킨다. 그런 다음에 JSON 문자열 결과를 출력한다.
mongo test --eval "printjson(db.getCollectionNames())"
또한 load(script) 메소드를 이용해서 몽고DB 셸 스크립트를 실행할 수 있다. 이 메소드는 자바스크립트 파일을 로드해 곧바로 이 파일을 실행한다. 다음 셸 명령은 db_update.js 스크립트 파일을 로그하고 실행한다.
load("/tmp/db_update.js")