05_MongoDB설정(2/2) - auth

mongodb auth 설정

1. admin 계정 생성

  • 계정종류
    • root : 모든 권한 계정
    • polaris : web server 사용 계정 *auth db : “userhabit”
use admin
db.createUser(
  {
    user: "admin",
    pwd: passwordPrompt(),
    roles: [{role: "dbAdminAnyDatabase", db: "admin"}],
  }
)

db.createUser(
  {
    user: "root",
    pwd: passwordPrompt(),
    roles: [{role: "root", db: "admin"}],
  }
)

use userhabit
db.createUser(
  {
    user: "polaris",
    pwd: passwordPrompt(),
    roles: [ { role: "readWrite", db: "userhabit" }],
  }
)

2. mongodb node 의 priority 조정

cfg = rs.conf()
cfg.members[0].priority = 2
rs.reconfig(cfg)

3. replica_set_key 생성

  • replica_set_key 파일을 생성 후 3개의 서버에 복사(3개의 node가 같은 key 사용)
$ openssl rand -base64 756 > ~/polaris/mongodb/replica_set_key
$ chmod 400 ~/polaris/mongodb/replica_set_key

4. key 를 각 node 에 설치

  • /etc/mongod.conf 의 auth.keyFile 의 path 와 같은 곳에 설치한다. (mceadm/apps/mongo/replica_set_key)
cp replicakey /mceadm/apps/mongo/replica_set_key
chown mongod:mongod /mceadm/apps/mongo/replica_set_key
chmod 400 /mceadm/apps/mongo/replica_set_key

5. /etc/mongod.conf 에서 auth부분 활성화

6. mongodb 재시작

systemctl restart mongod

7. WAS 에서 auth.keys생성

/mceadm/apps/polaris/auth.keys

echo -e "mongodb.id=test1\nmongodb.pw=pw1" > /mceadm/apps/polaris/auth.keys
chmod 400 /mceadm/apps/polaris/auth.keys

8. logrotate 설정

09_logrotate 설정 참고

cp mongodb.txt /etc/logrotate.d/mongodb
chmod 644 /etc/logrotate.d/mongodb

9. 특정계정을 ip address 로 제한하기

restriction 설정

use userhabit
db.updateUser(
  'test1',
  {
      authenticationRestriction: [
        {
          clientSource: ["10.227.171.96"]
        }
      ]
  }
)

결과

  • mongo mongodb://10.227.171.96:27017 로 접속하면 test1 계정의 인증 가능.
  • mongo mongodb://localhost:27017 로 접속하면 test1 계정의 인증 불가

※ 참고

db.updateUser(
  'test1',
  {
      authenticationRestriction: [
        {
          serverAddress: [],
          clientSource: ["10.227.171.96"]
        }
      ]
  }
)

10. 기타