yum 경로에 아래 파일 생성

/etc/yum.repos.d/10gen-mongodb.repo


아래 운영체제 비트에 따라 설치! (주의: 잘못된 비트일 경우 실행안됨;)


Mongodb-repo for Fedora 18/17/16/15/14/13/12, CentOS 6.4/5.9 and Red Hat (RHEL) 6.4/5.9 on i686 (32-bit)

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0

Mongodb-repo for Fedora 18/17/15/14/13/12, CentOS 6.4/5.9 and Red Hat (RHEL) 6.4/5.9 on x86_64 (64-bit)

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0

Install stable version of MongoDB

yum install mongo-10gen mongo-10gen-server


-----------------------------------------------------------------------------------------------------------------------




2. Configure MongoDB Database Server

2.1 Edit /etc/mongod.conf file:

nano -w /etc/mongod.conf

2.2 Check and set basic settings, before starting MongoDB (default settings are good)

logpath=/var/log/mongo/mongod.log
port=27017
dbpath=/var/lib/mongo

2.3 Start MongoDB Server

service mongod start
## OR ##
/etc/init.d/mongod start

2.4 Start MongoDB on boot

chkconfig --levels 235 mongod on

3. Test MongoDB Server

3.1 Open MongoDB Command Line Client

mongo

3.2 Save, Update and Find Some Test Data on MongoDB

> use test
switched to db test
> db.foo.find()
> db.foo.save({a: 1})
> db.foo.find()
{ "_id" : ObjectId("4b8ed53c4f450867bb35a1a9"), "a" : 1 }
> db.foo.update( {a: 1}, {a: 5})
> db.foo.find()
{ "_id" : ObjectId("4b8ed53c4f450867bb35a1a9"), "a" : 5 }


node mongolian (npm install mongolian)

var Mongolian = require("mongolian");
var server = new Mongolian;

var db = server.db("test"); // test Database선택
var posts = db.collection("posts"); //posts Collection 선택

//posts Collection 에  Document 넣기
posts.insert({
    pageId: "hallo",
    title: "Hallo",
    created: new Date,
    body: "Welcome to my new blog!"
});



node mongoose 테스트 (npm install mongoose)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// localhost 에 testdb Database에 접속
mongoose.connect('mongodb://localhost/testdb');

//모델의 스키마를 작성한다
var CommentSchema = new Schema({
    desc : String,  //내용
    hit : Number,   //조회수
    date : Date     //작성일
});

// CommentSchema에 맞는 CommentModel 만들기
// comment는 Document , 자동으로 comments Collection 에 모델이 저장된
var CommentModel = mongoose.model('comment', CommentSchema);

//CommentModel의 인스턴스생성
var comment = new CommentModel();

//인스턴스에 값을 할당하고
comment.desc = "nodejs test";
comment.hit = 0;
comment.date = new Date();

//저장하면 바로 몽고db에 올라간다
comment.save(function (err) {
    if (!err) console.log('Success!');
});


mongodb의 설정파일(/etc/mongod.conf) 디폴트 내용 


# mongod.conf
#where to log
logpath=/var/log/mongodb/mongod.log
logappend=true
# fork and run in background
fork=true
#port=27017
dbpath=/var/lib/mongo
# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1
# Disables write-ahead journaling
# nojournal=true
# Enables periodic logging of CPU utilization and I/O wait
#cpu=true
# Turn on/off security. Off is currently the default
#noauth=true
#auth=true
# Verbose logging output.
#verbose=true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck=true
# Enable db quota management
#quota=true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog=0
# Ignore query hints
#nohints=true
# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface=true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting=true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan=true
# Disable data file preallocation.
#noprealloc=true
# Specify .ns file size for new databases.
# nssize=<size>
# Replication Options
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile




mongodb 설치된곳

# cd /var/lib/mongo


mongodb DB데이터 저장되는 곳

# mongod --dbpath /var/lib/mongo/journal




----------

MongoDB shell version: 2.6.12

connecting to: test

2018-08-24T16:55:27.282+0900 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

2018-08-24T16:55:27.282+0900 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146

exception: connect failed


위와 같은 오류가 발생하면, 공간이 부족하거나, 서버재시작으로 mongod가 서비스되고 있지 않고 있기 때문이다.


# mongod


# mongod --dbpath /var/lib/mongo/journal


명령을 통해 실행!