1. MongoDB

MongoDB는 Schemaless의 Document 지향 데이터베이스이다.

MongoDB를 사용하고 있는 회사는 다음과 같다고 알려져 있다. (https://www.quora.com/Who-is-using-MongoDB)

  • Castlight Health, Thumbtack, Klout, IBM, Citrix, Twitter, T-Mobile, Zendesk, Sony, Chegg, Techstars, Atlassian, Udacity, BrightRoll, RetailMeNot, Hootsuite, SurveyMonkey, Shyp, Criteo, MuleSoft, HackerRank, Foursquare, HTC, InVision, Intercom

2. 활용

  • 전자상거래 플랫폼
  • 각종 클라이언트들의 소셜 활동 저장
  • 빠른 기능 추가 지원 (Schemaless), RDBMS는 아무래도 확장성에 제한
  • 다양한 유형의 전자 기록 저장, 예: 지리 정보 저장
  • 자연어 프로세싱
  • 지도 학습
  • 맞춤 서식 저장

3. 설치 (MongoDB 4.0)

2019년 1월 업데이트.

3-1. Repo 추가

CentOS 기준으로 우선 다음과 같이 Repo를 업데이트하기 위해 /etc/yum.repos.d/mongodb-org-4.0.repo 파일을 생성한다.

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

3-2. yum 설치

# yum install -y mongodb-org
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
epel/metalink                                                                                                                                            | 6.8 kB     00:00
 * base: centos.mirror.cdnetworks.com
 * epel: mirror.premi.st
 * extras: centos.mirror.cdnetworks.com
 * rpmforge: ftp.neowiz.com
 * updates: mirror.kakao.com
base                                                                                                                                                     | 3.7 kB     00:00
epel                                                                                                                                                     | 4.7 kB     00:00
epel/primary_db                                                                                                                                          | 6.0 MB     00:01
extras                                                                                                                                                   | 3.4 kB     00:00
kubernetes/signature                                                                                                                                     |  454 B     00:00
kubernetes/signature                                                                                                                                     | 1.4 kB     00:00 ...
Not using downloaded repomd.xml because it is older than what we have:
  Current   : Sun Nov 14 11:48:24 48968466

  Downloaded: Fri Sep  1 06:56:20 46635961

mariadb                                                                                                                                                  | 2.9 kB     00:00
mongodb-org-4.0                                                                                                                                          | 2.5 kB     00:00
mongodb-org-4.0/primary_db                                                                                                                               |  31 kB     00:00
rpmforge                                                                                                                                                 | 1.9 kB     00:00
updates                                                                                                                                                  | 3.4 kB     00:00
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:4.0.5-1.el6 will be installed
--> Processing Dependency: mongodb-org-tools = 4.0.5 for package: mongodb-org-4.0.5-1.el6.x86_64
--> Processing Dependency: mongodb-org-shell = 4.0.5 for package: mongodb-org-4.0.5-1.el6.x86_64
--> Processing Dependency: mongodb-org-server = 4.0.5 for package: mongodb-org-4.0.5-1.el6.x86_64
--> Processing Dependency: mongodb-org-mongos = 4.0.5 for package: mongodb-org-4.0.5-1.el6.x86_64
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:4.0.5-1.el6 will be installed
---> Package mongodb-org-server.x86_64 0:4.0.5-1.el6 will be installed
---> Package mongodb-org-shell.x86_64 0:4.0.5-1.el6 will be installed
---> Package mongodb-org-tools.x86_64 0:4.0.5-1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================================================
 Package                                        Arch                               Version                                    Repository                                   Size
================================================================================================================================================================================
Installing:
 mongodb-org                                    x86_64                             4.0.5-1.el6                                mongodb-org-4.0                             5.8 k
Installing for dependencies:
 mongodb-org-mongos                             x86_64                             4.0.5-1.el6                                mongodb-org-4.0                              12 M
 mongodb-org-server                             x86_64                             4.0.5-1.el6                                mongodb-org-4.0                              21 M
 mongodb-org-shell                              x86_64                             4.0.5-1.el6                                mongodb-org-4.0                              13 M
 mongodb-org-tools                              x86_64                             4.0.5-1.el6                                mongodb-org-4.0                              32 M

Transaction Summary
================================================================================================================================================================================
Install       5 Package(s)

Total download size: 79 M
Installed size: 243 M
Downloading Packages:
(1/5): mongodb-org-4.0.5-1.el6.x86_64.rpm                                                                                                                | 5.8 kB     00:00
(2/5): mongodb-org-mongos-4.0.5-1.el6.x86_64.rpm                                                                                                         |  12 MB     00:02
(3/5): mongodb-org-server-4.0.5-1.el6.x86_64.rpm                                                                                                         |  21 MB     00:03
(4/5): mongodb-org-shell-4.0.5-1.el6.x86_64.rpm                                                                                                          |  13 MB     00:02
(5/5): mongodb-org-tools-4.0.5-1.el6.x86_64.rpm                                                                                                          |  32 MB     00:06
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                           4.8 MB/s |  79 MB     00:16
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID e52529d4: NOKEY
Retrieving key from https://www.mongodb.org/static/pgp/server-4.0.asc
Importing GPG key 0xE52529D4:
 Userid: "MongoDB 4.0 Release Signing Key <이 이메일 주소가 스팸봇으로부터 보호됩니다. 확인하려면 자바스크립트 활성화가 필요합니다.>"
 From  : https://www.mongodb.org/static/pgp/server-4.0.asc
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mongodb-org-shell-4.0.5-1.el6.x86_64                                                                                                                         1/5
  Installing : mongodb-org-mongos-4.0.5-1.el6.x86_64                                                                                                                        2/5
  Installing : mongodb-org-tools-4.0.5-1.el6.x86_64                                                                                                                         3/5
  Installing : mongodb-org-server-4.0.5-1.el6.x86_64                                                                                                                        4/5
  Installing : mongodb-org-4.0.5-1.el6.x86_64                                                                                                                               5/5
  Verifying  : mongodb-org-4.0.5-1.el6.x86_64                                                                                                                               1/5
  Verifying  : mongodb-org-server-4.0.5-1.el6.x86_64                                                                                                                        2/5
  Verifying  : mongodb-org-tools-4.0.5-1.el6.x86_64                                                                                                                         3/5
  Verifying  : mongodb-org-mongos-4.0.5-1.el6.x86_64                                                                                                                        4/5
  Verifying  : mongodb-org-shell-4.0.5-1.el6.x86_64                                                                                                                         5/5

Installed:
  mongodb-org.x86_64 0:4.0.5-1.el6

Dependency Installed:
  mongodb-org-mongos.x86_64 0:4.0.5-1.el6     mongodb-org-server.x86_64 0:4.0.5-1.el6     mongodb-org-shell.x86_64 0:4.0.5-1.el6     mongodb-org-tools.x86_64 0:4.0.5-1.el6

Complete!

4. 기동

4-1. 일반 기동

우선 MongoDB가 설치되어 있다는 전제 하에 시작을 합니다. 설치 후 bin 디렉토리로 가서 mongod를 실행하면 된다고 배웠(...), 그래서 mongod를 실행하면 다음과 같이 반응합니다.

$ ./mongod
./mongod --help for help and startup options
Mon Jan 26 22:37:51.609 [initandlisten] MongoDB starting : pid=7708 port=27017 dbpath=/data/db/ 64-bit host=jeeg-MBP
Mon Jan 26 22:37:51.609 [initandlisten] db version v2.4.12
Mon Jan 26 22:37:51.609 [initandlisten] git version: 09917767b116f4ff1c0eadda1e8bc5db30828500
Mon Jan 26 22:37:51.609 [initandlisten] build info: Darwin bs-osx-106-x86-64-2.10gen.cc 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Mon Jan 26 22:37:51.609 [initandlisten] allocator: system
Mon Jan 26 22:37:51.609 [initandlisten] options: {}
Mon Jan 26 22:37:51.609 [initandlisten] exception in initAndListen: 10296
*********************************************************************
 ERROR: dbpath (/data/db/) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
Mon Jan 26 22:37:51.609 dbexit:
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: going to close listening sockets...
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: going to flush diaglog...
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: going to close sockets...
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: waiting for fs preallocator...
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: lock for final commit...
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: final commit...
Mon Jan 26 22:37:51.609 [initandlisten] shutdown: closing all files...
Mon Jan 26 22:37:51.609 [initandlisten] closeAllFiles() finished
Mon Jan 26 22:37:51.609 dbexit: really exiting now

오류 내용은 dbpath인 /data/db가 없다는 것 같습니다. 그리고 dbpath는 --dbpath 를 이용하여 명시적으로 설정할 수 있다고 하니 그렇게 해보려고 합니다.

$ ./mongod --dbpath=../data/db

mongod 파일이 있는  곳 기준으로 ../data/db를 생성한 후, mongod에서 dbpath로 그곳을 지정하고 다시 기동하니 아까와 같은 오류는 발생하지 않았습니다.

Mon Jan 26 22:39:54.874 [initandlisten] MongoDB starting : pid=7724 port=27017 dbpath=../data/db 64-bit host=jeeg-MBP
Mon Jan 26 22:39:54.874 [initandlisten] db version v2.4.12
Mon Jan 26 22:39:54.874 [initandlisten] git version: 09917767b116f4ff1c0eadda1e8bc5db30828500
Mon Jan 26 22:39:54.874 [initandlisten] build info: Darwin bs-osx-106-x86-64-2.10gen.cc 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Mon Jan 26 22:39:54.874 [initandlisten] allocator: system
Mon Jan 26 22:39:54.874 [initandlisten] options: { dbpath: "../data/db" }
Mon Jan 26 22:39:54.875 [initandlisten] journal dir=../data/db/journal
Mon Jan 26 22:39:54.876 [initandlisten] recover : no journal files present, no recovery needed
Mon Jan 26 22:39:54.887 [initandlisten] allocating new ns file ../data/db/local.ns, filling with zeroes...
Mon Jan 26 22:39:54.916 [FileAllocator] allocating new datafile ../data/db/local.0, filling with zeroes...
Mon Jan 26 22:39:54.916 [FileAllocator] creating directory ../data/db/_tmp
Mon Jan 26 22:39:54.995 [FileAllocator] done allocating datafile ../data/db/local.0, size: 64MB,  took 0.079 secs
Mon Jan 26 22:39:55.009 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 122ms
Mon Jan 26 22:39:55.009 [websvr] admin web console waiting for connections on port 28017
Mon Jan 26 22:39:55.009 [initandlisten] waiting for connections on port 27017

그리고 이제 mongo를 실행하여 방금 띄운 mongod와 연결을 해보려고 합니다.

$ ./mongo
MongoDB shell version: 2.4.12
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뿐이라 db를 입력해 봅니다.

> db
test

test라고 반응합니다. 기본이 이것으로 알고 있습니다.

4-2, 백그라운드 기동

# mongod --fork --logpath /sw/mongo-logs/mongo.log -dbpath=/sw/mongo-data
about to fork child process, waiting until server is ready for connections.
forked process: 28370
child process started successfully, parent exiting

 --fork --logpath 로그파일위치 옵션을 추가한다.

종료 시에는 mongod --shutdown 명령을 발행한다.

 


5. 기타

5-1. mongo 커맨드로 접속 안될 때

당연하지만 mongod가 기동되어 있는지 확인.

# mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-01-22T13:19:38.064+0900 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed