본문 바로가기
SQL/NoSQL

MongoDB 사용방법

by 지구 2018. 5. 3.

1. 설치


https://www.hugeserver.com/kb/install-mongodb-centos-6-7/


Install MongoDB

For installing the latest stable version of MongoDB you should add MongoDB repository first.

Adding MongoDB Repository

Move to the following directory:

cd /etc/yum.repos.d/

Create the following file with your favorite text editor:

nano mongodb-org-3.4.repo

For the latest stable version of MongoDB paste the following line in the file then save and exit:

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

Installing MongoDB

Update your repositories list executing the following command:

yum repolist

Now you can install the latest stable version of MongoDB using “Yum”:

yum install mongodb-org -y

=========================================================================================================

- 2017.05.16 기준 3.4 버전이 최신. Mongodb-3.4 repository 를 추가하여 yum 으로 설치해야 함.

  (그냥 yum 설치 시 2.x 버전으로 설치됨)

- 데몬 실행 시 권한 문제로 실행되지 않는 경우가 있다이 경우 log 파일을 참고하자.

(/etc/mongo.conf 의 log 파일 경로 참고. Default 경로는 /var/log/mongodb/mongod.log)

- systemctl start mongod (실행)

- systemctl status mongod (상태확인)


2. /etc/mongo.conf 설정


MongoDB 는 기본적으로 사용자가 등록되어 있지 않으며계정설정 없이 모든 데이터베이스에 접근이 가능하다따라서서비스할 데이터베이스에 사용자를 등록하고 권한을 제한하여 보안사고에 대비하도록 한다.

다음의 순서대로 진행하자.


1. mongo 접속

mongo --port 포트번호


2. admin Database 의 계정 생성


- admin database 로 이동


-user 생성

 

* role 은 다음 링크를 참고

https://docs.mongodb.com/manual/reference/built-in-roles/


3. /etc/mongo.conf 의 security 설정 후 mongod 재실행

 


4. mongo 쉘에 다시 접속 후 인증

 

- 쉘에 접속하면서도 -u, -p 옵션으로 인증할 수 있지만 특수문자가 password 에 있으면 안된다.



참고 : 

http://html5around.com/wordpress/tutorials/mongodb-%EC%9D%B8%EC%A6%9D%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0/



3. 접속 포트 설정


/etc/mongo.conf 의 port 옵션 변경 (Default : 27017)



4. 특정 IP 에서만 접속이 가능하도록 설정


/etc/mongo.conf 의 bindIp 옵션 설정

 bindIp: [127.0.0.1, x.x.x.x]

- 여러 ip 주소를 설정하는 경우 대괄호안에 명시

- 0.0.0.0 으로 설정 시 모든 외부접속 허용



5. Spring 과 연동


Spring data 레퍼런스 문서는 너무 방대하고 무슨 말인지도 모르겠다.

그래서 아래 사이트를 참고했다.

https://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/


아래는 인증설정을 적용한 mongodb 의 Spring java config 모습
 

[출처] MongoDB 시작하기|작성자 moonv11


반응형

댓글