[MongoDB] 1. 설치

Docker 환경에서 설치하기

version: "3"
services:
  mongo:
    restart: unless-stopped
    container_name: c-mongo
    image: mongo:4.4
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: secret
      MONGO_INITDB_DATABASE: sample
      MONGO_USERNAME: sample
      MONGO_PASSWORD: 1234
    volumes:
      - ./dbdata:/data/db
      - ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
mongo -- "$MONGO_INITDB_DATABASE" <<EOF
db.createUser({
    user: "$MONGO_USERNAME",
    pwd: "$MONGO_PASSWORD",
    roles: [
        { role: 'readWrite', db:"$MONGO_INITDB_DATABASE" }
    ]
})
EOF
$ docker-compose up -d
$ docker exec -it c-mongo bash
$ root@a786f144de6d: mongo

MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f0b046c5-b5da-4c06-93b2-6f1cb95ccebf") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.

> use sample
switched to db sample
db.createUser({
    user:"sample",
    pwd:"1234",
    roles: [ { role: "readWrite", db: "sample" }]
})

# 빠져나오고
> exit
$ mongo -u sample -p '1234' -authenticationDatabase sample

Admin tools for Windows

choco install -y robo3t