5. MongoDB 설치, SSH Tunneling
참조: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
5.1 설치
## import the MongoDB public GPG Key from [https://www.mongodb.org/static/pgp/server-6.0.asc](https://www.mongodb.org/static/pgp/server-6.0.asc) wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - ## ****Create a list file for MongoDB.**** echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list ## reload local package database & install package(libssl1.1) sudo apt-get update sudo -i wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb sudo apt-get install libssl1.1 ## install mongodb packages sudo apt-get install -y mongodb-org
5.2 Start MongoDB, Stop, Restart, Check Status
sudo systemctl start mongod ## stop sudo systemctl stop mongod ## restart sudo systemctl restart mongod ## Check Status sudo systemctl status mongod
[Option] Remove Mongo DB
## Stop MongoDB sudo service mongod stop ## Remove Packages. sudo apt-get purge mongodb-org* ## Remove Data Directories. sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb
5.3 외부 접근 ( Config 수정 )
5.3.1 Config
sudo vi /etc/mongod.conf ## network interfaces net: port: 27017 bindIp: 0.0.0.0 #127.0.0.1 -> 0.0.0.0
5.3.2 AWS Security Group
- 이미 Port 해놓았지만 한번 더 확인
- Private Subnet
- 27017 ← public security group
5.4 DB 테스트 및 데이터 입력 테스트
5.4.1 접속
mongosh ## 계정과 PW가 있다면 mongosh --username <userid> --password <password>
5.4.2 DB 생성
use <databasename> # 있는거라면 사용, 없으면 생성 따로 Create 가 없다. use histdb switched to db histdb histdb>
5.4.3 Collection 생성
- mongo db 데이터를 관리하는 묶음 → Collection
- RDBMS에서는 Table과 동일한 컨셉
- 데이터 하나하나를 Document라고 한다.
- 어떤 내용이 와도된다.
- 스키마가 정의 되어있지 않다.
- 스키마가 같은 도큐먼트의 집합 → Collection
db.myFirstCollection.insertOne({x:1}) myFirstDatabase> db.myFirstCollection.insertOne({x:1}) { acknowledged: true, insertedId: ObjectId("6385b61abe4338a8f21eef17") }
5.4.4 데이터 입력
db.movies.insertOne( { title: "The Favourite", genres: [ "Drama", "History" ], runtime: 121, rated: "R", year: 2018, directors: [ "Yorgos Lanthimos" ], cast: [ "Olivia Colman", "Emma Stone", "Rachel Weisz" ], type: "movie" } ) db.movies.insertMany([ { title: "Jurassic World: Fallen Kingdom", genres: [ "Action", "Sci-Fi" ], runtime: 130, rated: "PG-13", year: 2018, directors: [ "J. A. Bayona" ], cast: [ "Chris Pratt", "Bryce Dallas Howard", "Rafe Spall" ], type: "movie" }, { title: "Tag", genres: [ "Comedy", "Action" ], runtime: 105, rated: "R", year: 2018, directors: [ "Jeff Tomsic" ], cast: [ "Annabelle Wallis", "Jeremy Renner", "Jon Hamm" ], type: "movie" } ])
5.4.5 데이터 확인
db.movies.find()
5.5.1 SSH Tunneling
- New Connection
Manually configure my connection settings
- Check use SSH tunnel to connect
5.5.2 DB Connection
5.5.3 Run Query