Database/MongoDB

[MongoDB] connection String

Any Developer 2021. 11. 11. 18:01

[서론]

 MongoDB 를 활용한 응용프로그램 작성에서 client.connectionString 에 대한 문자열값 입력에 대한 내용 간략한 정리

 

[본론]

 

Connection String URI Format — MongoDB Manual

 

Connection String URI Format — MongoDB Manual

Docs Home → MongoDB ManualThis document describes the URI formats for defining connections between applications and MongoDB instances in the official MongoDB Drivers. For a list of drivers and links to driver documentation, see Drivers.You can specify th

docs.mongodb.com

 

 

위 Reference 문서에 모든 내용들이 나왔지만 아래 내용은 필요한 정보들만 기술함.

 

mongodb://user:password@localhost:27017/?authSource=admin&readPreference=primary&directConnection=true&ssl=false&uuidRepresentation=Standard

 

위 내용중 user 는 등록된 user 를 나타냄. (show user 를 이용해 등록된 user 를 확인해 볼 수 있음)

 

user 생성하는 방법은 아래 링크 참조

[MongoDB] User 생성하기 (tistory.com)

 

[MongoDB] User 생성하기

createUser — MongoDB Manual createUser — MongoDB Manual Docs Home → MongoDB ManualcreateUserCreates a new user on the database where you run the command. The createUser command returns a duplicat..

iotcyuty.tistory.com

 

초기 셋업에서 생성된 user가 없다면 user:password@ 부분은 삭제해도 됨.

 

localhost 는 접속할 서버의 ip 혹은 도메인 이름

뒤에 :27017  은  mongoDB 포트를 나타냄. (기본 포트 : 27017)

 

?     url 과 setting property 의 경계를 나타내는 구분 문자로 생각됨....

 

     authSource=admin

사용자 자격 증명 데이터베이스

지정을 안할 경우 defaultauthdb 로 지정된 DB 이름으로 지정이 된다고 함.

또한 defaultauthdb 가 지정이 되지 않을 경우 admin 으로 지정이 된다고 함.

 

     &     

settting property 값을 서로 구분짓는 구분 문자인 것으로 파악됨.

 

     readPreference=primary

읽기 기본 설정 지정

가능한 값들 리스트

primary (default)

primaryPreferred

secondary

secondaryPreferred

nearest

 

     directConnection=true

 

Connect to a Deployment — MongoDB Shell

 

Connect to a Deployment — MongoDB Shell

Docs Home → MongoDB ShellThis page shows how to use the MongoDB Shell to connect to a MongoDB deployment.To use the MongoDB Shell, you must have a MongoDB deployment to connect to.You can use the MongoDB Shell to connect to MongoDB version 4.0 or greater

docs.mongodb.com

 

 

     ssl=false

ssl 접속 여부

 

     uuidRepresentation=Standard

Stardard : 표준 바이너리 표현

csharpLegacy : C# driver 기본 표현

javaLegacy : Jave driver 기본 표현

pythonLegacy : Python driver 기본 표현

 

 

[결론]

MongoDB Compass 를 이용해 접속 테스트를 할 때 사용한 connectionString 을 복사하기 -> 수정 -> 응용프로그램 코드에 붙여넣기 로 확인/테스트해 본 내용을 정리함.

uuidRepresentation=Standard 는 Compass 접속 때는 없었던 내용이지만 응용프로그램 코딩 테스트에서는 생각보다 큰 변수로 작용하였음.  (일부 메소드가 실행되지 않았고 특히 collection 에 대한 쿼리가 전혀 실행되지 않는 문제가 발생하였음)  향후 더 깊은 내용을 알게되고 적용하고 내용을 정리할 때 포스팅을 업데이트 할 예정임.