annyoung

MongoDB 유저생성 및 데이터베이스 생성 본문

데이터베이스

MongoDB 유저생성 및 데이터베이스 생성

nopsled 2019. 1. 8. 09:52

회사에서 맨날 MongoDB를 사용하고 있는데 설정한다고 구글 검색하기가 귀찮아서 따로 만들어봤다.

 

public으로 설정이 잘못되어 있어서 하마터면 털릴뻔했다 ㅡㅡ..

 

MongoDB port(27017)을 인바운드와 noauth로 열어주는 경우엔 잘못하면 털린다.

 

그렇다면 이 설정을 어떻게 피하냐면.. 다음과 같이 하면 된다.

 

몽고디비를 새로 설치한 후 초기 셋팅이라고 생각하면 된다.

 

 

1. 초기에는 authorization이 disabled 되어 있으므로 그냥 mongo 쉘 커맨드로 접속하면 된다.

mongo
 

2. admin DB를 생성하여 모든 DB에 접속할 권한을 준다.

use admin
db.createUser({ user: "<username>",
  pwd: "<password>",
  roles: [
    "userAdminAnyDatabase",
    "dbAdminAnyDatabase",
    "readWriteAnyDatabase"
  ]
})

 

3. 다음은 사용자가 원하는 DB를 만들때 셋팅이다. (이 경우 해당 디비에만 권한을 받는다.)

use myDB
db.createUser({ user: "<username>",
  pwd: "<password>",
  roles: [
    "dbAdmin",
    "readWrite"
  ]
})

 

+ 사용자를 drop 시킬때 쓰는 명령이다. (거의 쓸일 없다)

use admin
db.dropUser("<username>")

 

 

4. 몽고로 쉘 커맨드로 접속하여 나오는 버전명을 보고 케이스를 나눠서 진행한다. (솔직히 2버전대 아니면 별 의미없는것 같다.)

Case1 : MongoDB shell version v4.0.4, MongoDB server version: 4.0.4

sudo vi /etc/mongod.conf
# mongod.conf

storage:
  dbPath: /data/db
  #dbPath: /var/lib/mongodb
  # if you want to custom MongoDB Path, modify dbPath line.
  journal:
    enabled: true


net:
   bindIp: 127.0.0.1
   port: 27017


security:
   authorization: "enabled"
   #authorization: "disabled" 
   # if you want to disable authorization, uncomment disabled line.


processManagement:
  timeZoneInfo: /usr/share/zoneinfo
   # If you use this timeZoneInfo, will be following your localtime


# where to write logging data.
systemLog:
   destination: file
   logAppend: true
   path: /var/log/mongodb/mongod.log
# If path permission has root:root, executed user(general user) can't access that file.
# Check path's permission. it followed executed permission.

 

Case2 : MongoDB shell version v3.6.7, MongoDB server version: 3.6.5

sudo vi /etc/mongod.conf
# mongod.conf

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true


# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# If path permission has root:root, executed user(general user) can't access that file.
# Check path's permission. it followed executed permission.


# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
   # If you use this timeZoneInfo, will be following your localtime


security:
   authorization: "enabled"
   #authorization: "disabled" 
   # if you want to disable authorization, uncomment disabled line.

이렇게 하고 robo로 접속 테스팅해본다.

참고로..bindIp를 걸어놓으면 로컬로 밖에 접속을 못하니 robo에서는 SSH 터널링을 통해 접속해야한다.

 

DB가 털리는 상황은 bindIp를 주석처리하고 authorization을 disabled하는 경우다. 올바른 설정을 위해선 bindIp를 걸어주거나 authorization을 enabled해준다. 둘중 하나만 설정되어 있다면 털릴 걱정 안해도 된다.

 

 

+ 참고 및 출처 : http://blog.freezner.com/archives/1040

Comments