docker로 mongoDB 설치#
pull#
# 최신버전 받아오기
$ docker pull mongo
run#
$ docker run -d \ # background 실행
--name mongo_db \ # container name
-v /Users/docker/mongodb/data/db:/data/db \ # volume 설정
-p 27017:27017 \ # port 설정
mongo -auth
컨테이너 접속#
$ docker exec -it mongo_db /bin/bash
mongo db#
# mongo db 접속. 권한이 없는 상태라 바로 접속 됨.
$ mongo
# admin db 선택
> use admin
# admin 계정 생성
> db.createUser({
user: "admin", # admin name
pwd: "password", # admin password
roles: [
{role: "userAdminAnyDatabase", db: "admin"},
{role: "dbAdminAnyDatabase", db: "admin"},
{role: "readWriteAnyDatabase", db: "admin"}
] # admin role
})
# admin 계정 접속
$ mongo -u "admin" -p --authenticationDatabase "admin"
(password 입력)
user 추가#
# 유저 생성
> db.createUser({
user: 'username',
pwd: 'password',
roles: [{role: 'readWrite', db: 'test'},
{role: 'read', db: 'reporting'}]
})
# 로그인
db.auth('username', 'password');