포스트

[Mongodb] python으로 연결하기

파이썬은 아시다시피 인공지능과 머신러닝 분야에서 매우 인기있는 언어입니다. 따라서 빅데이터를 다루는 pandas 라이브러리, 또 그 데이터를 시각화해주는 라이브러리도 파이썬에는 잘 되어있습니다. MongoDB는 MySQL과는 다르게 NoSQL로서 빅데이터를 다루는 데에 최적화되어 있는 데이터베이스 프레임워크입니다. 미리 필드를 정해놓을 필요 없이 없는 필드(컬럼)을 가지는 데이터를 입력해도 잘 저장이 되는 특징이 있죠. 사용 용도 면에서 많은 공통분모를 가지기 때문에 mongoDB와 Python언어는 잘 맞는다고 할 수 있겠습니다.

그래서 오늘은 그러한 mongodb를 Python으로 접속하는 방법을 소개해드리겠습니다.

아주 간단합니다.

pymongo 라이브러리를 사용할 것이기 때문에 설치를 해줍니다.

1
!pip install pymongo

설치가 되었다면, 다음과 같이 mongodb에 client로 연결할 수 있습니다. 혹시 로컬 PC가 아닌 서버에 있는 DB에 접속하고자 한다면 해당 서버(예:AWS EC2 등)의 IP주소를 localhost 대신 입력하시면 됩니다.

1
2
3
4
#python
# MongoDB에 연결
client = MongoClient("mongodb://localhost:27017") #인증이 필요 없다면 사용.
client = MongoClient("mongodb://username:password@localhost:27017") #인증이 필요하다면 사용.

더 많은 사용법과 연습을 원하신다면 아래 출처를 참고하시면 좋을 것 같습니다.

sticker

감사합니다.

https://pymongo.readthedocs.io/en/stable/

PyMongo 4.4.1 documentation PyMongo 4.4.1 Documentation Overview PyMongo is a Python distribution containing tools for working with MongoDB , and is the recommended way to work with MongoDB from Python. This documentation attempts to explain everything you need to know to use PyMongo . Installing / Upgrading Instructions on ho… PyMongo 4.4.1 Documentation Overview PyMongo is a Python distribution containing tools for working with MongoDB , and is the recommended way to work with MongoDB from Python. This documentation attempts to explain everything you need to know to use PyMongo . Installing / Upgrading Instructions on ho…

https://www.bearpooh.com/170

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.