728x90
반응형
NoSQL
NoSQL 중 하나인 MongoDB 를 이용하여 DB 를 구성해보자.
새로운 blog 앱을 생성한다.
python3 manage.py startapp blog
pip install pymongo
pip freeze | grep pymongo >> requirements.txt
from pymongo import MongoClient
client = MongoClient()
db = client.likelion
def create_blog() -> bool:
blog = {
"title" : "My first blog",
"content" : "This is my first blog",
"author" : "lion",
}
try:
db.blogs.insert_one(blog)
return True
except Exception as e:
print(e)
return False
def update_blog():
pass
def delete_blog():
pass
blog app - urls.py
from django.urls import path
from . import views
urlpatterns= [
path('', views.create_blog, name='blog-create'),
]
project - urls.py
...
from blog.urls import urlpatterns as blog_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include(blog_urls)),
]
project - settings.py
## Created Apps
INSTALLED_APPS += [
'forumapp',
'blog',
]
mongoDB 이미지 pull and run
docker pull mongo
docker run --name mongo -p 27017:27017 -d mongo
docker volume create mongo_data
docker-compose.yml
...
mongo:
container_name: mongo
image: mongo:lagest
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
volumes:
staticfiles:
postgres_data:
mongo_data:
실행
docker-compose -f ../docker-compose.yml up --build -d
URL 중 ‘blog’ 를 pymongo 를 사용하는 blog app 에서 create_blog 로 연결해 두었기 때문에 브라우저에서 blog 로 접속하게 되면 에러는 발생하지만 값이 들어가게 된다.
Mongo 컨테이너 접속 후 쉘 접속
docker exec -it mongo /bin/bash
mongosh
#
...
------
The server generated these startup warnings when booting
2023-08-08T04:38:42.151+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See <http://dochub.mongodb.org/core/prodnotes-filesystem>
2023-08-08T04:38:42.837+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-08-08T04:38:42.838+00:00: vm.max_map_count is too low
------
test>
use likelion
#
test> use likelion
switched to db likelion
likelion>
db.blogs.find()
# localhost:8888/blog 로 접속만 하면 def create 가 동작하면서 하드코딩된
# 내용이 들어간 것을 확인
likelion> db.blogs.find()
[
{
_id: ObjectId("64d1d0fdfc5250eb210852f9"),
title: 'My first blog',
content: 'This is my first blog',
author: 'lion'
}
]
728x90
반응형
'Dev. > Cloud' 카테고리의 다른 글
[Cloud]배포 AWS: Secrets Manager (0) | 2023.08.17 |
---|---|
[Cloud] 배포 - NCP : 클라우드 서버에서 DB 띄우기: NoSQL (0) | 2023.08.15 |
[Cloud] 배포 - NCP : 클라우드 서버에서 DB 띄우기 (0) | 2023.08.14 |
[Cloud] 배포 - NCP : DB 서버로 DB 연결 (0) | 2023.08.14 |
[Cloud] 배포 - NCP : 클라우드 서버에 올리기 (0) | 2023.08.13 |
댓글