-
MongoDB + Docker 설치 및 기본 동작 방법 bash: mongo: command not found스터디 노트 2023. 11. 9. 10:26
📌 MongoDB를 Docker에서
MongoDB를 운용하기 위해 Docker에서 이미지를 받고 설치하는 방법에 대해 설명합니다.
MongoDB Docker Image 다운로드
> docker pull mongo
docker가 설치되어있다는 가정 하에 위 명령어를 입력하여 mongo image를 다운로드 받습니다.
MongoDB Container 생성 및 실행
> docker run --name 컨테이너명 -p 27017:27107 -e MONGO_INITDB_ROOT_USERNAME=접속계정 -e MONGO_INITDB_ROOT_PASSWORD=접속비밀번호 -d 실행할이미지이름 컨테이너명 : 여기서는 mongo를 사용했습니다. -p 27017:27017 : 포트번호를 27107로 포워딩 해줍니다. -e : env 환경변수를 설정합니다. MONGO_INITDB_ROOT_USERNAME/MONGO_INITDB_ROOT_PASSWORD : 접속 계정과 비밀번호 설정 실행할이미지이름 : 다운로드한 MongoDB 이미지를 지정합니다.
정상적으로 Container가 생성 및 구동된다면 docker ps -a로 확인 시 다음처럼 컨테이너 상태를 확인하실 수 있습니다.
> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fb1d47eb1714 mongo "docker-entrypoint.s…" 17 hours ago Up 17 hours 0.0.0.0:27017->27017/tcp mongo
MongoDB 컨테이너 접속
> docker exec -it 컨테이너ID /bin/bash -it : name과 같습니다. 컨테이너ID : 실행중인 컨테이너의 좌측에 위치한 ID를 가져옵니다. /bin/bash : bash 환경으로 실행합니다.
MongoDB 접속
> mongo -u 접속계정 -p 접속비밀번호 접속계정 : 앞서 MongoDB Container 생성 시 지정해준 계정 접속비밀번호 : 앞서 MongoDB Container 생성 시 지정해준 비밀번호
위 접속 정보로 접속 요청 시 오류가 발생할 수 있습니다.
> mongo -u root -p root bash: mongo: command not found
이건 몽고 DB의 버전에 따른 접속 방식의 차이로 6.0 이상 버전을 사용한다면 다음 mongosh 명령어를 통해 접근을 요청하시면 문제없이 접속 가능합니다.
> mongosh -u root -p root Current Mongosh Log ID: 654b3f676a30e5ca49f0a55c Connecting to: mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.1 Using MongoDB: 7.0.2 Using Mongosh: 2.0.1
'스터디 노트' 카테고리의 다른 글
[SQL] JOIN이란? JOIN의 종류(INNER, LEFT, RIGHT, FULL OUTER JOIN) (1) 2023.11.17 깔짝 맛보는 프로메테우스(Prometheus) + 그라파나(Grafana) 를 이용한 시스템 모니터링 대시보드 만들기 (0) 2023.11.16 도커 + 카프카 명령어 (0) 2023.11.08 스프링 환경에서 웹 애플리케이션 설정 메커니즘 정리 (0) 2023.11.01 제네릭의 변성, 공변, 반공변 등등의 개념들 (0) 2023.10.30