Post List

2015년 8월 2일 일요일

MongoDB Study #01 : MongoDB 소개 및 설치, 시작/종료

1. MongoDB 소개

  humongous 사에서 2007년에 제작하였습니다.
  현재는 10gen으로 회사명을 변경하였습니다.
  Document-Oriented Storage 방식으로, Data 표현을 JSON (Javascript Object Notation)으로 합니다.
  내부적으로 저장은 BSON (Binary Serial Object Notation)으로 합니다.

2. MongoDB Terminology

  RDBMS와의 용어 차이는 아래 그림으로 대신하겠습니다.



3. MongoDB 설치 환경 및 지원 Driver

  3.1. 설치 가능 Platform
       - Windows x86/x64
       - Linux x86/x64
       - Unix Solaris x86/x64
       - Mac OS X x86/x64

  3.2. 지원 Driver
       - C / C++ / C#
       - JAVA / JAVA Script
       - Perl / PHP
       - Python / Ruby / Erlang / Haskell / Scala

4. MongoDB 다운로드 및 설치

  Windows 버전으로 설명드리겠습니다.



  https://www.mongodb.org/downloads 에서 원하는 버전을 다운로드 받으세요.
  MSI 나 ZIP 중 편한것으로 받아서 설치하시면 됩니다.
  설치 경로는 C:/MongoDB 로 해주세요.



  그리고 환경설정에서 PATHC:/MongoDB/bin 을 추가해주세요.
  아래 파일들을 실행하기 위함입니다.

  - Mongod.exe : DBMS 를 구동파일
  - Mongo.exe : Client 접속 파일



5. MongoDB 시작 / 종료

먼저 Datafile이 저장될 공간을 생성해주세요.

MD C:/MongoDB/test

설치된 MongoDB의 버전 확인

C:\Users\icysw_000>mongod --version
db version v3.0.5
git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3

MongoDB 시작

C:\Users\icysw_000>mongod --dbpath c:/mongodb/test
2015-08-02T12:22:13.200+0900 I JOURNAL  [initandlisten] journal dir=c:/mongodb/test\journal
2015-08-02T12:22:13.202+0900 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-08-02T12:22:13.225+0900 I JOURNAL  [durability] Durability thread started
2015-08-02T12:22:13.225+0900 I CONTROL  [initandlisten] MongoDB starting : pid=3416 port=27017 dbpath=c:/mongodb/test 64-bit host=SJYUN-ATIV
2015-08-02T12:22:13.225+0900 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2015-08-02T12:22:13.225+0900 I CONTROL  [initandlisten] db version v3.0.5
2015-08-02T12:22:13.225+0900 I CONTROL  [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2015-08-02T12:22:13.226+0900 I CONTROL  [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2015-08-02T12:22:13.226+0900 I CONTROL  [initandlisten] allocator: tcmalloc
2015-08-02T12:22:13.226+0900 I CONTROL  [initandlisten] options: { storage: { dbPath: "c:/mongodb/test" } }
2015-08-02T12:22:13.234+0900 I JOURNAL  [journal writer] Journal writer thread started
2015-08-02T12:22:13.355+0900 I NETWORK  [initandlisten] waiting for connections on port 27017

x64 버전은 --journal 옵션을 부여하지 않더라도 기본 설정이 되지만,
x86 버전은 반드시 --journal 옵션을 부여해야 합니다.

mongod --dbpath c:/mongodb/test  --journal

위와 같이 화면이 나오고 명령 Prompt가 뜨지 않으면 성공적으로 실행된 것입니다.
명령 Prompt가 뜬다면, Error 메세지를 보고 다시 실행해 보시기 바랍니다.

이제 다른 Command 창을 하나 열어서 Client를 실행하시기 바랍니다.

C:\Users\icysw_000>mongo
MongoDB shell version: 3.0.5
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 <db_name>                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

help 를 입력하면 Mongo Shell 상태에서 실행 할 수 있는 명령어 List가 출력됩니다.

show dbs를 입력하면 현재 생성된 database의 목록을 볼 수 있습니다.

use [database명] 을 입력하면 해당 database를 사용하게 됩니다.
만약 존재하지 않은 database라면 해당 database에서 첫번째 collection 생성시 자동으로 생성됩니다.

> show dbs
local  0.078GB
test   0.078GB
> use test
switched to db test

db.stats() 를 입력하면 현재 사용중인 database의 정보 확인이 가능합니다.
현재 어느 database에 있는지를 확인할 때도 사용하면 됩니다.

> db.stats()
{
        "db" : "test",
        "collections" : 2,
        "objects" : 1,
        "avgObjSize" : 48,
        "dataSize" : 48,
        "storageSize" : 12288,
        "numExtents" : 2,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 67108864,
        "nsSizeMB" : 16,
        "extentFreeList" : {
                "num" : 4,
                "totalSize" : 278528
        },
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 22
        },
        "ok" : 1
}

db.logout() 를 입력하면 Client를 접속종료 하게 됩니다.
Client를 종료하는 명령어는 exit 입니다.

> db.logout()
{ "ok" : 1 }
> exit
bye

MongoDB를 종료하려면 admin database로 이동한 후 db.shutdownServer()를 입력하면 됩니다.

C:\Users\icysw_000>mongo
MongoDB shell version: 3.0.5
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
2015-08-02T12:41:30.715+0900 I NETWORK  DBClientCursor::init call() failed
server should be down...
2015-08-02T12:41:30.720+0900 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-08-02T12:41:31.737+0900 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:10061 대상 컴퓨터에서 연결을 거부했으므로 연결하지 못했습니다.
2015-08-02T12:41:31.759+0900 I NETWORK  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
> exit
bye

댓글 없음:

댓글 쓰기