일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Melbourne 여행
- 두바이
- bind9
- mauritius casela
- r
- duabi
- 모리셔스 여행
- 경주 여행
- 느낌 사진
- 모리셔스 카셀라
- 서울야경
- mauritius
- 트루오비쉬
- 모리셔스 카젤라
- Sony A850
- cloud foundry
- postgresql 비밀번호 초기화
- openstack
- 모리셔스여행
- 프랑크푸르트 여행
- postgresql 비밀번호 변경
- 모리셔스
- 모리셔스리조트
- 독일여행
- 두바이여행
- Trou aux Biches
- 경주
- 서울 야경
- postgresql 설치
- 톰캣 서비스 등록
- Today
- Total
I.K.Picture & IT Info.
[MongoDB] 사용자 인증 활성화 본문
몽고 디비는 설치와 동시에 인증 없이 바로 사용할 수 있습니다.
하지만 이렇게 사용하는건 실제 업무시 보안적 문제가 있을 수 있겠죠?
나중에 여러 사용자 사용 시에도 인증 프로세스는 반드시 필요하구요
그래서 설치 후 인증하는 방법에 대해 정리해보았습니다.
각 데이터베이스에 대한 접근 권한 아이디를 생성하는 방식으로 했습니다
이렇게 하면 나중에 새로운 데이터베이스마다 다른 사용자 인증으로 권한을 줄 수 있으니까요~
(단일 또는 여러명 가능)
일단 제가 사용한 몽고 디비에 대한 버전은 다음과 같습니다.
Ubuntu 16.04 LTS, MongoDB shell version v3.6.5
일단 당연히 몽고 디비 스크립트로 들어가야겠죠?
$mongo |
그러면 이제 ">" 표시로 바뀌면서 몽고 디비 쿼리를 할 수 있도록 됩니다.
제일 먼저 기본 데이터베이스인 admin 데이터베이스에 대한 사용자 권한을 주기 위해 admin 사용자를 만드려고 합니다.
먼저 admin 데이터베이스를 사용하는 명령어를 써주고
그 다음 생성할 아이디를 아래 명령어로 생성해줍니다.
이렇게 생성한 후 exit 명령어로 몽고디비 스크립트 작성을 나갑니다.
>use admin >db.createUser({ user: "admin", pwd: "관리자 비밀번호", roles: [{ role: "userAdminAnyDatabase", db:"admin"}] }) >exit |
이제 인증부분을 활성화 시켜줍니다.
몽고 디비 설정파일은 /etc/mongod.conf 에서 설정합니다.
$sudo vi /etc/mongod.conf |
mongod.conf 를 열면 아래와 같이 나올꺼예요
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo #security:
#operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp: |
여기서 중간에 보이는 security 부분에 대한 주석을 풀어주시고 authorization 부분을 enabled 해주면됩니다.
(빨간색 부분이 적용한 내용입니다)
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo security: authorization:
#operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp: |
$sudo service mongod restart |
자 그러면 이제 데이터베이스에 사용자 권한을 집어넣어보도록 하겠습니다.
그 전에 사용자 인증이 제대로 활성화되었는지 확인해볼 건데요
먼저 몽고 디비 쉘 스크립트로 들어간 후 간단히 show dbs를 해보도록 하면 아래와 같이 나옵니다.
(쉘 스크립트로 들어갈 떄는 별도 인증 과정을 거치지 않습니다)
$mongo MongoDB shell version v3.6.5 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.6.5 >show dbs 2018-06-12T16:19:48.549+0900 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }", "code" : 13, "codeName" : "Unauthorized" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1 shellHelper.show@src/mongo/shell/utils.js:849:19 shellHelper@src/mongo/shell/utils.js:739:15 @(shellhelp2):1:1 |
위에 보이는 것처럼 인증 실패가 나오게 되죠
그럼 인증 과정을 거치고 해보죠
$mongo MongoDB shell version v3.6.5 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.6.5 > use admin switched to db admin > db.auth("admin", "intruder12") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB test 0.027GB |
인증 과정을 거치게 되면 이렇게 제대로 보이는 것을 확인할 수 있습니다.
자 이렇게 인증 과정을 먼저 거친 후 test 라는 데이터베이스에 사용자를 추가하려고 합니다.
명령으는 위에와 동일해요
>db.createUser({ user : "아이디", pwd: "비밀번호", roles: [{ role: "dbOwner", db: "test" }] }) |
여기서 db:"test" 는 원하는 데이터베이스 이름을 넣어주면 됩니다.
참고로 위에 인증 과정을 거치지 않고 하면 사용자를 만들 수 없습니다.
이렇게 만들면 새로 만들어진 아이디는 해당 db에만 권한을 유지하게 되는 사용자 아이디가 됩니다.
이렇게 만든 후
use admin 후 admin 계정 인증 후
use admin 에서 db.getUsers()와 use test 에서의 db.getUsers()가 달라지는 모습을 보면 어떤 방식인지 쫌 이해하기 쉽지 않을까 싶어서 출력 내용을 아래 적어놓았어요 (제가 사용자 아이디로 만든건 intruder 입니다)
>use admin >db.auth("admin", "admin비밀번호") >db.getUsers() [ { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }, { "_id" : "admin.inturder", "user" : "inturder", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" } ] } ] |
>use test >db.getUsers() #### 이 때 인증은 admin 계정이 되어 있어야합니다. [ { "_id" : "test.intruder", "user" : "intruder", "db" : "test", "roles" : [ { "role" : "dbAdmin", "db" : "test" }, { "role" : "readWrite", "db" : "test" } ] } ] |
'Development > Database' 카테고리의 다른 글
PostgreSQL 설치 시 ... An error occured executing the Microsoft VC++ runtime installer 경우 (1) | 2019.08.21 |
---|---|
[PostgreSQL] Master /Slave 이중화 작업 (0) | 2018.09.22 |
PostgreSQL pgAdmin3 에서 비밀번호 변경시 버그 (0) | 2016.01.21 |
MySQL 외부 접근 허용하기 (0) | 2016.01.21 |
mysql 사용자별 권한 부여, 제거, 삭제 및 설정 (0) | 2016.01.21 |