mongodb

yoon__0_0·2024년 5월 21일
0

이어드림 수업

목록 보기
43/103

mongodb 환경설정

  • mongodb.sh 생성
sudo apt install software-properties-common gnupg apt-transport-https ca-certificates -y
curl -fsSL https://pgp.mongodb.com/server-7.0.asc |  sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
  • 다운로드 및 update
$ source mongodb.sh
$ sudo apt update
$ sudo apt install mongodb-org -y
$ sudo systemctl start mongod
$ sudo systemctl status mongod
  • 계정 생성
$ mongosh

test > use admin
admin> db.createUser({user:'yj',pwd : 'yjpw', roles:['root']})

admin> quit
  • 외부 접속 허용
sudo vi /etc/mongod.conf 

변경해주기

ip 설정비밀번호 설정
$ sudo systemctl restart mongod
  • studio3t 다운로드
  • ec2 보안그룹 : 27017 열어주기
  • 연결해보기

python과 연동

  • jupyter notebook 환경
!pip install pymongo

import pandas as pd
import pymongo

client = pymongo.MongoClient('mongodb://{계정}:{비번}@프라이빗ip:27017')
client
# create
df = pd.DataFrame({
    'name': list('AB'),
    'age': [25, 34],
})

json_data = df.to_dict('records')

# table = collection , row = document
client.mongo.user.insert_many(json_data)
#읽어오기
collection = client.mongo.user
documents = collection.find({})
data = list(documents)
profile
신윤재입니다

0개의 댓글