๐Ÿฆ Python Online Store ๋งŒ๋“ค๊ธฐ 3ํŽธ - Mongo DB Atlas ์—ฐ๋™

yohan-record[web]ยท2022๋…„ 7์›” 27์ผ
0

python onlinestore ๊ฐœ๋ฐœ์ผ์ง€

๋ชฉ๋ก ๋ณด๊ธฐ
3/25
post-thumbnail

๐ŸŽˆ Front End Developer ์˜ Back End ๋„์ „๊ธฐ ! ๐ŸŽˆ

์ด ๊ธ€์€ PROJECT LION : ํ˜ธ์ฝ”์น˜ ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ์ฐธ๊ณ ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ž‘์—… ์ค‘, ๊ฐœ๋ฐœ๊ณผ์ • ์ •๋ฆฌ ๋ฐ issue๊ฐ€ ๋˜์—ˆ๋˜ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ  ๋ฐ ๊ธฐ๋กํ•˜๊ณ ์ž ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

1. Mongo DB Atlas๋ž€?

โœ ๋ชจ๋“  ๊ฒƒ์„ ๊ด€๋ฆฌํ•˜๋Š” cloud database -> data๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„

๐ŸŽ‰ MongoDB๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

  1. ๊ฐ€์ž…
  2. Cluster ์ƒ์„ฑ (ํ”„๋กœ์ ํŠธ / DB ์ƒ์„ฑ)
  3. Flask ์—ฐ๋™

text๋กœ ๋ดค์„๋•Œ๋Š” ๊ฐ„๋‹จํ•ด ๋ณด์ธ๋‹ค ๐Ÿ˜‚
๊ตฌ๊ตฌ์ ˆ์ ˆ ์„ค๋ช…ํ•˜๊ธฐ ๋ณด๋‹ค๋Š”, ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•ด๋ณด์ž !

๐ŸŽ‰ ์˜ค๋ฅ˜ VS ๋‚˜

๐Ÿ”จ Mongo DB Atlas๋ฅผ flask์™€ ์—ฐ๋™์‹œํ‚ค๋ฉด์„œ, ํฌ๊ณ  ์ž‘์€ issue๋“ค์ด ๋ฐœ์ƒํ•˜์˜€๋‹ค.

์ด๊ฒƒ๋„ ๋ชปํ•ด๋‚ด๋ฉด ๋‚จ์ž๊ฐ€ ์•„๋‹ˆ๋‹ค(^^) ๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ์˜ค๋ฅ˜๋ฅผ ์žก๊ณ  ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ๋‹ค๊ณ  ๊ธฐํ•„์ฝ” ๋‹ค์งํ–ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ 2์‹œ๊ฐ„์ด๋‚˜ ์†Œ์š”๊ฐ€ ๋˜์—ˆ๋‹ค ๐Ÿ˜‘

ํ•˜์ง€๋งŒ, ์˜ค๋ฅ˜๋ฅผ ์Šค์Šค๋กœ ํ•ด๊ฒฐํ•œ ์‚ฌ๋žŒ์€ ์•Œ๊ฒƒ์ด๋‹ค. ๊ทธ ์พŒ๊ฐ์„ !!

issue ๋ฐœ๊ฒฌ๋ถ€ํ„ฐ ํ•ด๊ฒฐํ•œ ๊ณผ์ •๊นŒ์ง€ ์•„์ฃผ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•ด๋ณด๊ฒ ๋‹ค.
๋‚˜๊ฐ™์€ issue ๊ฐ€ ๋ฐœ์ƒํ•œ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๊ผญ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๊ธธ โœŒ๏ธ

๐Ÿ“Œ ์ž‘์—… ์ˆœ์„œ (โœจ = issue)

  1. Mongo db atlas site ์ ‘์† ๋ฐ ํšŒ์›๊ฐ€์ž…

Create Cluster ํ›„, database access ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ์ž ๋“ฑ๋ก์ด ํ•„์š”ํ•˜๋‹ค.
password๋Š” ์ž๋™์ƒ์„ฑ ํ›„ ์ €์žฅํ•ด ๋‘์–ด๋„ ๋˜๊ณ , ๋งŒ์•ฝ ๊นŒ๋จน์—ˆ๋‹ค๋ฉด ์ƒˆ๋กญ๊ฒŒ update ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๊ฑฑ์ • ์•ˆํ•ด๋„ ๋œ๋‹ค.

๐ŸŽ† ์ฃผ์˜ = password์— @ ํฌํ•จ์‹œํ‚ค์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค.. ๊ผญ...
์ด์œ ๋Š” ํ•˜๋‹จ ํ•ด๊ฒฐ part ์—์„œ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

  1. Network Access

    = ip address๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
    (์–ด๋–ค ip์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์„ค์ •์ธ๋ฐ ํ˜„์žฌ ์ž‘์—…๋ฌผ์€ ๋ณด์•ˆ์— ์žˆ์–ด์„œ ํฌ๊ฒŒ ์‹ ๊ฒฝ์“ธ ํ•„์š”๋Š” ์—†์„ ๊ฒƒ ๊ฐ™๋‹ค.

    ๊ทธ๋Ÿฌ๋ฏ€๋กœ, Allow access from anywhere. ์ฆ‰ ์–ด๋””์„œ๋‚˜ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•ด ๋‘๊ฒ ๋‹ค.)

    ์ถ”ํ›„ ๋ณด์•ˆ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š”, ํŠน์ • ip์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก setting ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. โœจ ์—ฐ๋™

    DataBase Menu๋ฅผ ํด๋ฆญ ์‹œ cluster ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.
    ๊ทธ ํ›„, flask์™€ ์ƒ์„ฑ๋œ MongoDB Cluster๋ฅผ ์—ฐ๋™ ์‹œํ‚ค๋ฉด ๋œ๋‹ค.

    3-1. editor ์ ‘์† ๋ฐ models folder ์ƒ์„ฑ
    3-2. mongodb.py file ์ƒ์„ฑ

    ๐ŸŽ‰ ์ฃผ์š” code ํ•ด์„

    ๐ŸŽฒ import pymongo
    = requirements ์—์„œ ์„ค์น˜ํ•œ pymongo ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

    ๐ŸŽฒ MONGO_URI
    = MongoDB์˜ cluster ์ฃผ์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค.
    MongoDB site -> Database -> connect -> Connect your application -> Driver Python ์„ค์ • ๋ฐ 3.6 ์ด์ƒ ์„ค์ • -> ํ•˜๋‹จ url ๋ณต์‚ฌ

    ๐Ÿ”– : ๋’ค์—์˜ค๋Š” password ๋Š”,
    1๋ฒˆ) ์‚ฌ์šฉ์ž๋“ฑ๋ก ์‹œ ๊ธฐ์ž…ํ•œ password๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

    ๐ŸŽฒ MONGO_CONN = pymongo.MongoClient(MONGO_URI)
    = MongoDB์™€ ์—ฐ๊ฒฐ์„ ์œ„ํ•ด ์ž‘์„ฑ

    ๐ŸŽฒ def conn_mongodb()
    = ํ•จ์ˆ˜์ƒ์„ฑ. pymongo package ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MONGO_URI ์™€ ์—ฐ๋™์„ ํ•˜๊ฒ ๋‹ค.

    ๐ŸŽฒ db = MONGO_CONN.online_store
    = ์—ฐ๋™์ด ๋˜์—ˆ์„ ์‹œ, cluster ์•ˆ์— online_store ๋ผ๋Š” database ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

    ๐ŸŽฒ test_connection = db.test_connection.insert_one({'test':'test'})
    = db ์•ˆ์— test_connection ์ด๋ผ๋Š” collection์— test๋ผ๋Š” ์ •๋ณด๋ฅผ ๋‹ด์•„ ์ƒ์„ฑํ•˜๊ฒ ๋‹ค.

    ์ฆ‰, test_connection ์•ˆ์— test ๋ผ๋Š” document ๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. (์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•œ ๊ทธ๋ฆผ์ธ๋ฐ ์•Œ์•„๋ณด๋ฉด ๋‹คํ–‰์ผ ๊ฒƒ ๊ฐ™๋‹ค.. ๐Ÿ˜†)

์„ธํŒ…์€ ๋งˆ๋ฌด๋ฆฌ๋˜์—ˆ๋‹ค!

ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„, terminal ์—์„œ mongodb file์„ ์‹คํ–‰ํ•ด๋ณด๊ฒ ๋‹ค.
= ๊ฐ€์ƒํ™˜๊ฒฝ(venv) mode ์ ‘์† ํ›„, python3 models/mongodb.py

์—ฐ๋™์ด ์ œ๋Œ€๋กœ ๋˜์—ˆ๋‹ค๋ฉด,
๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  enter ์‹œ ์•„๋ฌด ๋ฐ˜์‘์ด ์—†์œผ๋ฉด ์ •์ƒ์ด๋‹ค.

ํ•˜์ง€๋งŒ, ๋ช‡๊ฐ€์ง€ โœจ issue ์‚ฌํ•ญ ๋“ค์ด ๋ฐœ๋ชฉ์„ ์žก์•˜๋‹ค.

(.venv) C:\OnlineStore>python3 models/mongodb.py
Python
(.venv) C:\OnlineStore>

  1. python text๋งŒ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค.

    = ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ์ด ๋˜์ง€ ์•Š์•„์•ผ ํ• ํ…๋ฐ ๋ง์ด๋‹ค..
    ํ•˜์ง€๋งŒ, ์ด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ์ฒซ๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€์— 2๋ฒˆ์งธ ์ด์Šˆ์‚ฌํ•ญ์—์„œ ๋ฐœ์ƒํ•˜์˜€๋˜๊ฒŒ ๋ฐ”๋กœ ๊ธฐ์–ต์ด ๋‚ฌ๋‹ค!

๊ทธ๋Ÿฌํ•˜์—ฌ, ๊ตฌ๋ฌธ์„ ๋ฐ”๊ฟ” ๋‹ค์‹œ ์‹œ๋„ํ•˜์˜€๋‹ค.

(.venv) C:\OnlineStore>py -3 models/mongodb.py
Traceback (most recent call last):
File "C:\OnlineStore\models\mongodb.py", line 1, in
import pymongo
ModuleNotFoundError: No module named 'pymongo'

  1. python3 models -> py -3 models ๋ณ€๊ฒฝ

    1๋ฒˆ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๋งŽ์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค.
    ์ฐจ๊ทผ์ฐจ๊ทผ ์‚ดํŽด๋ณด๋‹ˆ, pymongo๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

    venv ํด๋” ์•ˆ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ๊ฐ€ ๋ณด๊ธฐ๋„ ํ•˜๊ณ , ์žฌ์„ค์น˜๋ฅผ ํ•ด๋ณด๋ ค๊ณ  pip ๋กœ pymongo๋ฅผ ์„ค์น˜ํ•ด๋ณด๋‹ˆ ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๊ณ  ํ•˜์˜€๋‹ค.

    ์ฆ‰, ์„ค์น˜๊ฐ€ ๋˜์–ด์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค(?)

    2-1. ์šฐ์„ , python ๊ณผ pymongo ์˜ version ํ˜ธํ™˜์„ฑ์„ ์ฒดํฌํ•ด ๋ณด์•˜๋‹ค.

  • python์˜ version์€ 3.10.4 ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์—ˆ๊ณ ,

  • pymongo์˜ version์€ 4.2.0์œผ๋กœ ํ™•์ธ์ด ๋˜์—ˆ๋‹ค.

    issue ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด๋ณด์—ฌ searchํ•ด ๋ณด์•˜๋Š”๋ฐ ๋ณ„๋‹ค๋ฅธ ํŠน์ด์‚ฌํ•ญ์€ ์—†์—ˆ๋‹ค.

๐ŸŽ‰ ํ•ด๊ฒฐ!

ํ•ด๊ฒฐ์ด ๋ ์ค„๋งŒ ์•Œ์•˜๋˜ version๋„ issue ๊ฐ€๋Šฅ์„ฑ์ด ์—†์–ด๋ณด์—ฌ, python์ด ์„ค์น˜๋œ script folder๋ฅผ ์ฐพ์•„๊ฐ”๋‹ค.

  1. python์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š” scripts folder ์ ‘์† ํ›„ pip.exe ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ํ™•์ธ

  2. terminal ํ™˜๊ฒฝ์—์„œ scripts folder์— ์ ‘์†ํ•˜์—ฌ pymongo ๋ฐ pymongo[srv]๋ฅผ ์žฌ์„ค์น˜ ํ›„ ํ™•์ธ.

    ๊ฒฐ๊ณผ๋Š” ๋Œ€์„ฑ๊ณต์ด๋‹ค ๐Ÿคฉ

    ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ๋งŒ requirements.txt ๋กœ ์ž‘์„ฑํ•œ library๋“ค์ด ์„ค์น˜๋˜์–ด ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๊ณ , python์ด ์„ค์น˜๋œ ํ™˜๊ฒฝ์—์„œ๋Š” ์•„๋งˆ ์—†์—ˆ๊ธฐ์— not found ๊ฐ€ ๋‚˜ํƒ€๋‚ฌ์„ ๊ฒƒ์ด๋‹ค.

ํ•˜์ง€๋งŒ, ๊ธฐ๋ปํ•˜๊ธด ๋„ˆ๋ฌด ์ผ๋ €๋‚˜๋ณด๋‹ค. ์˜ค๋ฅ˜๊ฐ€ ๋˜ ๋ฐœ์ƒํ–ˆ๋‹ค. ๐Ÿ˜

  1. RFC 3986 error
    = ์ด ์˜ค๋ฅ˜๋Š” ๋น„๊ต์  ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐํ–ˆ๋‹ค.

    ์ฒ˜์Œ user๋ฅผ ๋“ฑ๋กํ•  ๋•Œ password์— @ ๋ฅผ ํฌํ•จ์‹œ์ผฐ๋Š”๋ฐ, ์ด๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค.

    ํ˜น์‹œ ๋˜‘๊ฐ™์€ ์ƒํ™ฉ์„ ๊ฒช๋Š” ๋ถ„๋“ค์ด ๊ณ„์‹ค์ค„๋„ ๋ชจ๋ฅด๋‹ˆ, ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค !!

์›์ธ์„ ์•Œ๊ธฐ๊นŒ์ง€ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฉด์œผ๋กœ ํ˜๋Ÿฌ๊ฐ€๋Š” logic ๋„ ๊ทธ๋ ค๋ณด๋ฉฐ ๊ณ„์† ์ƒ๊ฐํ•ด๋ณด์•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ฌ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

๋๊นŒ์ง€ ๋ชฐ์ž…ํ•ด์„œ ํŒŒ๊ณ ๋“ค๋ฉด ๊ฒฐ๊ตญ ํ•ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค!!! ๐Ÿ™Œ

๋‹ค์Œ ์ˆœ์„œ๋Š” MongoDB Compass๋ฅผ ์„ค์น˜ํ•ด ๋ณด๊ฒ ๋‹ค.

profile
๐Ÿฑโ€๐Ÿ Front End Developer

0๊ฐœ์˜ ๋Œ“๊ธ€