NoSQL MongoDB, 설치와 간단 사용법

2016.05.10 10:32

졸리운_곰 조회 수:1537

 

 

MongoDB, 설치와 간단 사용법

 
MongoDB(from “humongous”)는 전통적인 RDBMS의 대안인 NOSQL Family의 일부로 오픈 소스 “Document-Oriented Database”을 지향하고 있습니다.
MongoDB에서 Data는 JSON과 같은 Dynamic Schema 형태의 Document(문자열로 구성된 단위라고 생각하면 됩니다.)구조로 저장 되는데 MongoDB에서는 이를 BSON 이라 합니다.
MongoDB가 다른 문서 데이터베이스와 구별되는 한 가지 기능은 SQL문을 MongoDB 쿼리 함수 호출로 매우 간단하게 변환하는 기능있어 기존 RDBMS를 쉽게 마이그레이션 할 수 있다고 합니다.
MogoDB가 최근에 주목을 받게 된 이유는 Mobile 환경의 확대와 SNS의 활성화로 설명 할 수 있을 것 같습니다.

Mobile환경의 확대
  1. 최근 모바일 어플리케이션은 Hybrid 형태을 띄며 Data을 기본적으로 Server Side에 저장하기 때문에 지속적으로 Server Side로 부터 Data을 받아오는게 사용자 사용성에 중요한 척도가 되었다.
  2. 모바일 어플리케이션 개발 기술 중 Server/Client 통신 기술은 Open API 형태로 WebService을 사용하며 그 중 RESTFul 방식으로 전문형태는 JSON을 사용하는 것이 거의 표준으로 잡혀 있음.
  3. MogoDB와 같은 Document 기반 DB는 JSON형태로 저장되기 때문에 Data와 어플리케이션간 통합에 최적의 구조를 가진다.

SNS의 활성화
  1. 모바일 환경에서 SNS가 활성화 되고 나서 엄청난 규모의 Data가 발생한다.
  2. 전통적인 RDBMS로는 현재 규모의 Big Data을 저장하고 관리하기 어렵다.
  3. 새로운 형태의 NoSQL 기술이 필요하게 되었고 가장 적합한 형태가 MongoDB이다.

설치하기
Download(현재버전 2.2.3) : http://www.mongodb.org/downloads
환경에 맞는 설치 파일을 다운로드 받는다.(저는 Linux 32-bit) 64bit용을 받게되면 메모리 사용에 제약을 받지 않지만 32bit을 사용하는 경우 2G이상 사용할 수 없다고 한다. 일반적인 경우 64bit을 사용하기를 권장하고 있다.
또한 다양한 language의 Driver을 지원하고 있기 때문에 필요한 Driver도 다운 받는다.

Ubuntu OS에 설치해보기
Step 1 ‘.deb’ 페키지를 설치하기 위해 GPG 키를 등록 한다.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Step 2 command ‘sudo apt-get update’
Step 3 command ‘sudo apt-get install mongodb’

MongoDB 설정
  • 설정 파일 : /etc/mongodb.conf
  • 실행 설정 파일: /etc/init.d/mongodb.
  • 라이브러리 위치: /var/lib/mongodb
  • 로그 위치: /var/log/mongodb

시작과 종료
  • Start: command ‘sudo service mongodb start’
  • Stop: command ‘sudo service mongodb stop’
  • Restart: command ‘sudo servcie mongodb restart’

초간단 사용해보기
  • local 접속: ‘mongo’
  • remote 접속: 'mongo localhost:27017/test’ 27017은 default port, test는 기본생성 DB

yunchang-lee@HSK01-INTER999:/var/log/mongodb$ mongo localhost:27017/test
MongoDB shell version: 2.0.6
connecting to: localhost:27017/test → Connection
> db → 현재 접속한 DB 확인
test
> show dbs → 모든 DB 확인
local (empty)
test (empty)
> use mydb → 사용할 DB 지정, 없는 경우 자동 생성된다.
switched to db mydb
> db → 정말 바꿔졌는지 확인
mydb
> help → 도움말 보기
db.help()                    help on db methods
db.mycoll.help()             help on collection methods
rs.help()                    help on replica set methods
help admin                   administrative help
help connect                 connecting to a db help
help keys                    key shortcuts
help misc                    misc things to know
help mr                      mapreduce

show dbs                     show database names
show collections             show collections in current database
show users                   show users in current database
show profile                 show most recent system.profile entries with time >= 1ms
show logs                    show the accessible logger names
show log [name]              prints out the last segment of log in memory, 'global' is default
use <db_name>                set current database
db.foo.find()                list objects in collection foo
db.foo.find( { a : 1 } )     list objects in foo where a == 1
it                           result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x   set default number of items to display on shell
exit                         quit the mongo shell
> db.help() → DB 메소드 도움말 보기
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.currentOp() displays the current operation in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus()
db.removeUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.shutdownServer()
db.stats()
db.version() current version of the server
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnock() unlocks server following a db.fsyncLock()
> j={name:"mongo"} → 데이터를 넣어보기 위해, JSON 형태의 DATA 생성
{ "name" : "mongo" }
> k={x:3} → 처음과 다른 형태의 JSON 형태 생성, MongoDB는 JSON 형태가 다르더라도 같은 Collection에 넣을 수 있다.
{ "x" : 3 }
> db.things.insert(j) → 생성한 ‘j’ collection 'things’에 넣는다.
> db.things.insert(k) → 생성한 ‘k’ collection 'things’에 넣는다.
> show collections → 어떤 collection들이 있는지 확인해본다.
system.indexes
things → j, k 가 포함된 things가 보인다.
> db.things.find() → things에 포함된 Document을 조회 한다, "_id”는 MongoDB가 collection에 입력되는 순간 자동으로 부여하며, mogoDB내에서 유니크 하다.
{ "_id" : ObjectId("5111e871abd47e5e659ee261"), "name" : "mongo" }
{ "_id" : ObjectId("5111e8f1abd47e5e659ee262"), "x" : 3 }
> for(var i=1; i<=20; i++) db.things.insert({x:4,j:i}) → for 문을 이용해서 다중 문서를 넣어본다.
> db.things.find()
{ "_id" : ObjectId("5111e871abd47e5e659ee261"), "name" : "mongo" }
{ "_id" : ObjectId("5111e8f1abd47e5e659ee262"), "x" : 3 }
{ "_id" : ObjectId("5111e97babd47e5e659ee263"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5111e97babd47e5e659ee264"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5111e97babd47e5e659ee265"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5111e97babd47e5e659ee266"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5111e97babd47e5e659ee267"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5111e97babd47e5e659ee268"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5111e97babd47e5e659ee269"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5111e97babd47e5e659ee26a"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5111e97babd47e5e659ee26b"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5111e97babd47e5e659ee26c"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("5111e97babd47e5e659ee26d"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("5111e97babd47e5e659ee26e"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("5111e97babd47e5e659ee26f"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("5111e97babd47e5e659ee270"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("5111e97babd47e5e659ee271"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("5111e97babd47e5e659ee272"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("5111e97babd47e5e659ee273"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("5111e97babd47e5e659ee274"), "x" : 4, "j" : 18 }
has more
> it → 더 있는 경우 ‘it'
{ "_id" : ObjectId("5111e97babd47e5e659ee275"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("5111e97babd47e5e659ee276"), "x" : 4, "j" : 20 }
>

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

[출처] http://daddycat.blogspot.kr/2013/02/mongodb-1.html

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86672
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 79030
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95791
31 오라클 컬럼명 찾기 졸리운_곰 2017.04.17 2031
30 [Oracle | 오라클] DB 덤프/임포트 하기 졸리운_곰 2017.03.17 1231
29 Oracle Data pump 실전 사용 졸리운_곰 2017.03.17 858
28 오라클 덤프 export / import file 졸리운_곰 2017.03.17 1350
27 오라클 DB 백업과 복원 졸리운_곰 2017.03.17 1781
26 SQL HINT 를 이용하여 SQL Tunning 을 하는 방법 file 졸리운_곰 2017.03.15 1305
25 <운바-DB> Oracle HINT(기초) file 졸리운_곰 2017.03.15 1532
24 조인 순서 조정을 위한 힌트(ordered, leading) 졸리운_곰 2017.03.15 1478
23 ORACLE Hint 정리 file 졸리운_곰 2017.03.15 1728
22 Oracle Hint 설명 및 사용법 졸리운_곰 2017.03.15 1562
21 ORACLE SQL 서버 및 PLSQL 코딩 표준 가이드 file 졸리운_곰 2017.01.14 1759
20 ORACLE SQL 튜닝 가이드 file 졸리운_곰 2014.06.07 3945
19 Oracle DECODE문을 IF문처럼 사용하기. 졸리운_곰 2014.01.25 2478
18 [ORACLE] NVL함수 & DECODE함수 졸리운_곰 2014.01.25 2472
17 ※ Oracle 오라클 DECODE 함수 file 졸리운_곰 2014.01.25 2775
16 오라클 백업 및 복구(Datapump) 졸리운_곰 2014.01.25 2125
15 (Oracle) delete 복구 졸리운_곰 2014.01.25 2423
14 오라클 쿼리 복구 졸리운_곰 2014.01.25 3456
13 오라클 플래쉬백 사용하기(Flashback Query) 졸리운_곰 2014.01.25 2604
12 체크 제약조건 적용사례 졸리운_곰 2014.01.19 3172
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED