Docs Menu
Docs Home
/ / /
C#/.NET

์—ฐ๊ฒฐ ๋ฌธ์ œ ํ•ด๊ฒฐ

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ์—ฐ๊ฒฐ ์˜ค๋ฅ˜
  • ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด ํ™•์ธ
  • ๋ฐฉํ™”๋ฒฝ ๊ตฌ์„ฑ
  • ์ธ์ฆ ์˜ค๋ฅ˜
  • ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด ํ™•์ธ
  • MongoClientSettings ์†์„ฑ ํ™•์ธ
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
  • X.509 ์ž๊ฒฉ ์ฆ๋ช… ์˜ค๋ฅ˜
  • ๋ฉ”์‹œ์ง€ ์ „์†ก ์˜ค๋ฅ˜
  • ์‚ฌ์šฉ์ž ๊ถŒํ•œ ํ™•์ธ
  • ๋ฐฉํ™”๋ฒฝ ๊ตฌ์„ฑ
  • ์—ฐ๊ฒฐ ํšŸ์ˆ˜ ํ™•์ธ
  • ์—ด๋ ค ์žˆ๋Š” ์—ฐ๊ฒฐ์ด ๋„ˆ๋ฌด ๋งŽ์Œ
  • ์—ฐ๊ฒฐ ํšŸ์ˆ˜ ํ™•์ธ
  • ์‹œ๊ฐ„ ์ดˆ๊ณผ ์˜ค๋ฅ˜
  • ์—ฐ๊ฒฐ ์ œํ•œ ์‹œ๊ฐ„ ์„ค์ •
  • ์—ฐ๊ฒฐ ํšŸ์ˆ˜ ํ™•์ธ

์ด ํŽ˜์ด์ง€์—์„œ๋Š” MongoDB .NET/C# ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MongoDB ๋ฐฐํฌ์— ์—ฐ๊ฒฐํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ž ์žฌ์ ์ธ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ 

์ด ํŽ˜์ด์ง€์—์„œ๋Š” ์—ฐ๊ฒฐ ๋ฌธ์ œ๋งŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. MongoDB ๋˜๋Š” ๋“œ๋ผ์ด๋ฒ„์— ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐฉ๋ฌธํ•˜์„ธ์š”.

  • .NET/ C# ๋“œ๋ผ์ด๋ฒ„ ์— ๋Œ€ํ•œ ์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ( FAQ )

  • ๋ฒ„๊ทธ ๋ณด๊ณ , ๋“œ๋ผ์ด๋ฒ„ ๊ธฐ์—ฌ, ์ถ”๊ฐ€ ๋ฆฌ์†Œ์Šค ์ฐพ๊ธฐ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ œ ๋ฐ ๋„์›€๋ง ํŽ˜์ด์ง€

  • ์งˆ๋ฌธ, ํ† ๋ก  ๋˜๋Š” ์ผ๋ฐ˜์ ์ธ ๊ธฐ์ˆ  ์ง€์›์„ ์œ„ํ•œ MongoDB Community ํฌ๋Ÿผ

๋‹ค์Œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋Š” ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ง€์ •๋œ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„ ๋˜๋Š” ํฌํŠธ์˜ ์„œ๋ฒ„์— ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์ƒํ™ฉ์—์„œ ์ด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒ˜ํ”Œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์—์„œ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์€ 127.0.0.1(์ด)๊ณ  ํฌํŠธ๋Š” 27017 ์ž…๋‹ˆ๋‹ค.

Error: couldn't connect to server 127.0.0.1:27017

๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ์ž ์žฌ์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ์น˜์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์˜ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„๊ณผ ํฌํŠธ ๋ฒˆํ˜ธ๊ฐ€ ๋ชจ๋‘ ์ •ํ™•ํ•œ์ง€ ํ™•์ธํ•˜์„ธ์š”. MongoDB ์ธ์Šคํ„ด์Šค์˜ ๊ธฐ๋ณธ ํฌํŠธ ๊ฐ’์€ 27017(์ด)์ง€๋งŒ ๋‹ค๋ฅธ ํฌํŠธ์—์„œ ํ†ต์‹ ํ•˜๋„๋ก MongoDB๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

MongoDB ๋ฐฐํฌ ์„œ๋ฒ„๊ฐ€ ์ˆ˜์‹ ํ•˜๋Š” ํฌํŠธ๊ฐ€ ๋™์ผํ•œ ๋„คํŠธ์›Œํฌ์˜ ๋ฐฉํ™”๋ฒฝ์— ์˜ํ•ด ์ฐจ๋‹จ๋˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. MongoDB๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํŠธ 27017 ์„(๋ฅผ) ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. MongoDB์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ํฌํŠธ ๋ฐ ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ธฐ๋ณธ MongoDB ํฌํŠธ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๊ฒฝ๊ณ 

MongoDB deployment์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํฌํŠธ์ธ์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ฐฉํ™”๋ฒฝ์—์„œ ํฌํŠธ๋ฅผ ์—ด์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

์ธ์ฆ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ์„ฑ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ .NET/C# ๋“œ๋ผ์ด๋ฒ„๊ฐ€ MongoDB ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ฆ์— SCRAM-SHA-256 ๋˜๋Š” SCRAM-SHA-1 ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์—ฐ๊ฒฐ์— ์‹คํŒจํ•  ๊ฒฝ์šฐ, ๋“œ๋ผ์ด๋ฒ„์—์„œ ๋‹ค์Œ ๋ฉ”์‹œ์ง€ ์ค‘ ํ•˜๋‚˜์™€ ์œ ์‚ฌํ•œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Command failed with error 18 (AuthenticationFailed): 'Authentication
failed.' on server <hostname>:<port>.
Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","principalName":
"<db_username>","<auth database>":"<db_username>","client":"127.0.0.1:2012",
"result":"UserNotFound: Could not find user}}
connection() error occurred during connection handshake: auth error:
sasl conversation error: unable to authenticate using mechanism
"SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.

๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ์ž ์žฌ์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ์น˜์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์ž˜๋ชป๋œ ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์€ ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด๊ณผ SCRAM-SHA-256 ๋˜๋Š” SCRAM-SHA-1์„ ์‚ฌ์šฉํ•˜์—ฌ MongoDB์— ์—ฐ๊ฒฐํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ธ์ฆ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์›์ธ์ž…๋‹ˆ๋‹ค.

ํŒ

์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฐ๊ฒฐ ๊ฐ€์ด๋“œ์˜ ์—ฐ๊ฒฐ URI๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์—ฐ๊ฒฐ string ๋ฌธ์ž์—ด ์— ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹์ธ์ง€ ํ™•์ธํ•˜์„ธ์š”. ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ์— ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ ํผ์„ผํŠธ๋กœ ์ธ์ฝ”๋”ฉ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

: / ? # [ ] @

๋‹ค์Œ ์˜ˆ๋Š” '#MyP@assword?'๋ฅผ ํผ์„ผํŠธ๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

Console.WriteLine(System.Web.HttpUtility.UrlEncode("#MyPassword?"));

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

%23MyPassword%3F

MongoDB ๋ฐฐํฌ์— ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•  ๋•Œ MongoClientSettings ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Credential ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฆ ์ •๋ณด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๊ฒฉ ์ฆ๋ช… ์ •๋ณด๊ฐ€ ์ •ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ MongoDB ๋ฐฐํฌ์— ์—ฐ๊ฒฐํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ธ์ฆ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

SCRAM-SHA-256 ๋˜๋Š” SCRAM-SHA-1๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ๊ฒฐ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ธ์ฆํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ธ์ฆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์ธ์ฆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” admin ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ž…๋‹ˆ๋‹ค. ์ธ์ฆ์— ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์— authSource ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์—์„œ๋Š” users๋ฅผ ์ธ์ฆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ๋“œ๋ผ์ด๋ฒ„์— ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

using MongoDB.Driver;
// Connection URI
const string connectionUri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=users";
// Create a new client and connect to the server
var client = new MongoClient(connectionUri);

MongoClientSettings ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ MongoClient ์ƒ์„ฑ์ž์— ์ „๋‹ฌํ•˜์—ฌ ๊ตฌ์„ฑ ์„ค์ •์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Credential ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์ธ์ฆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ง€์ •์„ ๋น„๋กฏํ•˜์—ฌ ๋กœ๊ทธ์ธ ์ž๊ฒฉ ์ฆ๋ช…์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. MongoClientSettings ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ๊ณผ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋ณด๋ ค๋ฉด MongoClientSettings ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ปฌ ์‹œ์Šคํ…œ์—์„œ ํ˜ธ์ŠคํŒ…๋˜๋Š” MongoDB ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•˜์—ฌ ์ด๊ฒƒ์ด ๋ฌธ์ œ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋จธ์‹ ์˜ ๋ฐฐํฌ์„œ๋ฒ„์—๋Š” ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐ ๊ถŒํ•œ ๋ถ€์—ฌ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Windows ๋ฅผ ์šด์˜ ์ฒด์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ .NET/ C# ๋“œ๋ผ์ด๋ฒ„ ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ X.509 ์ธ์ฆ ์ธ์ฆ์„œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ค๋ฅ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

No credentials are available in the security package

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. ์ด ์ฝ”๋“œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์— ํ•„์š”ํ•œ X.509 ์ธ์ฆ์„œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋””์Šคํฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

using (X509Certificate2 certWithKey = certOnly.CopyWithPrivateKey(key))
{
return new X509Certificate2(certWithKey.Export(X509ContentType.Pkcs12));
}

์š”์ฒญ์„ ํ•œ ํ›„ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ๋ช…๋ น์„ ์ „์†กํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

com.mongodb.MongoSocketWriteException: Exception sending message

๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ์ž ์žฌ์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ์น˜์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์˜ฌ๋ฐ”๋ฅธ ์‚ฌ์šฉ์ž๋กœ MongoDB deployment์— ์•ก์„ธ์Šคํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์˜ค๋ฅ˜์˜ '๋ฉ”์‹œ์ง€'๋ผ๋Š” ์šฉ์–ด๋Š” ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ๋ณด๋‚ธ ๋ช…๋ น์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์„ ๋ณด๋‚ผ ๊ถŒํ•œ์ด ์—†๋Š” ์‚ฌ์šฉ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋“œ๋ผ์ด๋ฒ„์—์„œ ์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด๋‚ด๋Š” ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•œ ์ ์ ˆํ•œ ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. MongoDB๋Š” RBAC(์—ญํ•  ๊ธฐ๋ฐ˜ ์•ก์„ธ์Šค ์ œ์–ด)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MongoDB ๋ฐฐํฌ์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๋ฅผ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. MongoDB์—์„œ RBAC๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ญํ•  ๊ธฐ๋ฐ˜ ์•ก์„ธ์Šค ์ œ์–ด๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋ฐฉํ™”๋ฒฝ์—๋Š” MongoDB ์ธ์Šคํ„ด์Šค์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋ฐฉํ˜• ํฌํŠธ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฉํ™”๋ฒฝ ๊ตฌ์„ฑ ์ •๋ณด๋Š” ์—ฐ๊ฒฐ ์˜ค๋ฅ˜ ์„น์…˜์˜ ๋ฐฉํ™”๋ฒฝ ๊ตฌ์„ฑ์—์„œ ์ž์„ธํžˆ ํ™•์ธํ•˜์„ธ์š”.

๊ฐ MongoClient ์ธ์Šคํ„ด์Šค๋Š” ์—ฐ๊ฒฐ ํ’€์—์„œ ์ตœ๋Œ€ ๋™์‹œ ๊ฐœ๋ฐฉ ์—ฐ๊ฒฐ ์ˆ˜๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ œํ•œ์„ ์ •์˜ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ MaxConnectionPoolSize์„(๋ฅผ) ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 100์ž…๋‹ˆ๋‹ค. MaxConnectionPoolSize๋งŒํผ ์—ด๋ ค ์žˆ๋Š” ์—ฐ๊ฒฐ์ด ์ด๋ฏธ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ ์„œ๋ฒ„๋Š” ์—ฐ๊ฒฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค. ์ด ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด MaxConnectionIdleTime ๊ฐ’์„ ์ดˆ๊ณผํ•˜๋ฉด ๋“œ๋ผ์ด๋ฒ„๋Š” ์˜ค๋ฅ˜๋กœ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค.

์—ฐ๊ฒฐ ํ’€๋ง์˜ ์ž‘๋™ ๋ฐฉ์‹์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ FAQ์—์„œ .NET/C# ๋“œ๋ผ์ด๋ฒ„์—์„œ ์—ฐ๊ฒฐ ํ’€๋ง์€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋‚˜์š”?๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋“œ๋ผ์ด๋ฒ„๋Š” ์—ฐ๊ฒฐ์„ ์—ด๋ ค๊ณ  ์‹œ๋„ํ•˜์ง€๋งŒ ์ตœ๋Œ€ ์—ฐ๊ฒฐ ์ˆ˜์— ๋„๋‹ฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

connection refused because too many open connections

๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์—ด๋ฆฐ ์—ฐ๊ฒฐ์„ ๋” ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ MaxConnectionPoolSize๋ฅผ ๋Š˜๋ฆฌ์„ธ์š”. ์—ฐ๊ฒฐ ์ˆ˜ ํ™•์ธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฉ”์‹œ์ง€ ์ „์†ก ์˜ค๋ฅ˜ ์„น์…˜์˜ ์—ฐ๊ฒฐ ์ˆ˜ ํ™•์ธ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

๋„คํŠธ์›Œํฌ๊ฐ€ ๋“œ๋ผ์ด๋ฒ„์˜ ์š”์ฒญ์„ ์„œ๋ฒ„์— ์ถฉ๋ถ„ํžˆ ๋น ๋ฅด๊ฒŒ ์ „๋‹ฌํ•˜์ง€ ๋ชปํ•˜๋ฉด ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋‹ค์Œ ๋ฉ”์‹œ์ง€์™€ ์œ ์‚ฌํ•œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

timed out while checking out a connection from connection pool: context canceled

์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋‹ค์Œ ์กฐ์น˜๋ฅผ ์‹œ๋„ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์„ธ์š”.

์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ๋ณต์ œ๋ณธ ์„ธํŠธ ๋…ธ๋“œ์— ๋„๋‹ฌํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ค ์—ฐ๊ฒฐ์„ ์„ค์ •ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ค‘๋‹จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. connectTimeMS ์„ค์ •์„ ์‚ฌ์šฉํ•˜์—ฌ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•˜๋Š” ๋ฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ค์ •์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด MongoDB Server ๋งค๋‰ด์–ผ์˜ ์‹œ๊ฐ„ ์ดˆ๊ณผ ์˜ต์…˜ ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

connectTimeoutMS ์„ค์ •์ด ํ•ด๋‹น ์„ธํŠธ์˜ ๋ฉค๋ฒ„์— ๋Œ€ํ•œ ์ตœ๋Œ€ ๋„คํŠธ์›Œํฌ ์ง€์—ฐ ์‹œ๊ฐ„๋ณด๋‹ค ๋‚ฎ์ง€ ์•Š์€์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ธ์ปจ๋”๋ฆฌ ๋ฉค๋ฒ„ ์ค‘ ํ•˜๋‚˜์˜ ์ง€์—ฐ ์‹œ๊ฐ„์ด 10000๋ฐ€๋ฆฌ์ดˆ์ธ ๊ฒฝ์šฐ connectTimeoutMS์„(๋ฅผ) 9000์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ํ•ด๋‹น ๊ตฌ์„ฑ์›์— ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด ์˜ต์…˜์€ ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์—์„œ๋Š” connectTimeoutMS๋ฅผ 10000๋ฐ€๋ฆฌ์ดˆ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

using MongoDB.Driver;
// Connection URI
const string connectionUri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?connectTimeoutMS=10000";
// Create a new client and connect to the server
var client = new MongoClient(connectionUri);

MongoClientSettings ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ MongoClient ์ƒ์„ฑ์ž์— ์ „๋‹ฌํ•˜์—ฌ ๊ตฌ์„ฑ ์„ค์ •์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. MongoClientSettings ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ๊ณผ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋ณด๋ ค๋ฉด MongoClientSettings ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์ฐธ์กฐ ํ•˜์„ธ์š”.

์„œ๋ฒ„์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ ์ˆ˜๊ฐ€ MaxConnectionPoolSize๊ฐœ๋ฅผ ์ดˆ๊ณผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ๊ฒฐ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ „์†ก ์ค‘ ์—ฐ๊ฒฐ ์ˆ˜ ํ™•์ธ ์„น์…˜์„ ์ฐธ์กฐํ•˜์„ธ์š”.

๋Œ์•„๊ฐ€๊ธฐ

FAQ