무료 몽고디비 cloud를 이용해보자

무료 클라우드에 가입하고 스프링 연동을 해보자!

기본적으로 무료 500메가를 제공한다. 몽고디비가 4.x버전위로 업그레이드 되면서 홈페이지가 많이 리뉴얼 된 모습이다.. 엄청 헤맴

아래의 사이트로 접속

1. 홈페이지 접근

Mlab_Homeage

Mlab_Homeage

대충 가입을 누르고 무료 클러스터를 생성해보자

2. Region선택

선택할 때 구글 클라우드를 선택하고 일본 region을 선택하자 한국과 가장 가까움

mlab_cluster_region

3. 총 3개의 노드가 생성되어 올라가고 있다.

중요! 생성하는데 5분정도 걸리니 그냥 기다리자

프라이머리 노드 - 세컨더리 - 세컨더리

waiting

4. db access 유저를 생성

create_user

5. network access 설정

0.0.0.0/0 전역으로 넣자! 물론 테스트이니가 가능한일..

6. 노드가 전부 올라감

all_node_activated

7. mongodb uri 획득

A. Connect 클릭
A

B. connect your application 클릭
B

C. copy 클릭
C

4번에서 설정한 username, password 기입 ( <> 해당 기호 제거)

1
mongodb+srv://<username>:<password>@cluster0.yul3n.gcp.mongodb.net/<dbname>?retryWrites=true&w=majority

8. Spring 설정

build.gradle 디펜던시 추가

1
2
3
4
5
6
7
8
9
10
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}

application.yml 추가, 위에서 따온 mongodb uri 입력

1
2
3
4
5
spring:
data:
mongodb:
uri: mongodb+srv://username:password@cluster0.yul3n.gcp.mongodb.net/test?retryWrites=true&w=majority
database: test

application run

1
2
3
4
5
6
7
8
9
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:219779}] to cluster0-shard-00-02.yul3n.gcp.mongodb.net:27017
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:3, serverValue:208437}] to cluster0-shard-00-00.yul3n.gcp.mongodb.net:27017
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-00.yul3n.gcp.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=41594384, rd-0', canonicalAddress=cluster0-shard-00-00.yul3n.gcp.mongodb.net:27017, hosts=[cluster0-shard-00-02.yul3n.gcp.mongodb.net:27017, cluster0-shard-00-00.yul3n.gcp.mongodb.net:27017, cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', egion', value='NORTHEASTERN_ASIA_PACIFIC'}]}, electionId=null, setVersion=4, lastWriteDate=Mon Aug 10 15:11:47 KST 2020, lastUpdateTimeNanos=253097206247824}
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-02.yul3n.gcp.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=41947405, rd-0', canonicalAddress=cluster0-shard-00-02.yul3n.gcp.mongodb.net:27017, hosts=[cluster0-shard-00-02.yul3n.gcp.mongodb.net:27017, cluster0-shard-00-00.yul3n.gcp.mongodb.net:27017, cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', egion', value='NORTHEASTERN_ASIA_PACIFIC'}]}, electionId=null, setVersion=4, lastWriteDate=Mon Aug 10 15:11:47 KST 2020, lastUpdateTimeNanos=253097206247010}
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:218330}] to cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=40369006, rd-0', canonicalAddress=cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017, hosts=[cluster0-shard-00-02.yul3n.gcp.mongodb.net:27017, cluster0-shard-00-00.yul3n.gcp.mongodb.net:27017, cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', egion', value='NORTHEASTERN_ASIA_PACIFIC'}]}, electionId=7fffffff000000000000001f, setVersion=4, lastWriteDate=Mon Aug 10 15:11:47 KST 2020, lastUpdateTimeNanos=253097859619057}
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.cluster : Setting max election id to 7fffffff000000000000001f from replica set primary cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.cluster : Setting max set version to 4 from replica set primary cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017
INFO 54288 --- [ngodb.net:27017] org.mongodb.driver.cluster : Discovered replica set primary cluster0-shard-00-01.yul3n.gcp.mongodb.net:27017

3개의 몽고디비 노드들이 정상적으로 붙었음을 확인