1.下载安装包到本地。mongoDB官方下载地址:https://www.mongodb.org/

    2.解压:tar -zxvf  mongodb-linux-x86_64-ubuntu1404-3.0.5.tgz  (我搭的是ubuntu12.04单节点的,其他系统类似)

    3.进入mongoDB文件目录并加入地址:

mv mongodb-linux-x86_64-ubuntu1404-3.0.5 mongodb
cd mongodb
mkdir -p data/db
./bin/mongod -dbpath=data/db

   注意,要加入dbpath,不然容易报错:


    

xubo@xubo:~/cloud/mongodb/bin$ ./mongo
MongoDB shell version: 3.0.5
connecting to: test
2015-08-12T12:07:01.721+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-08-12T12:07:01.722+0800 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed

运行“./bin/mongod -dbpath=data/db”会需要一段时间,显示如下。不要关,打开另外一个终端继续下一步

xubo@xubo:~/cloud/mongodb$ ./bin/mongod -dbpath=data/db  
2015-08-12T12:09:51.909+0800 I JOURNAL [initandlisten] journal dir=data/db/journal
2015-08-12T12:09:51.909+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-08-12T12:09:54.301+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 29.96
2015-08-12T12:09:56.860+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 30.28
2015-08-12T12:10:00.343+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 29.52
2015-08-12T12:10:00.343+0800 I JOURNAL [initandlisten] preallocateIsFaster check took 8.434 secs
2015-08-12T12:10:00.343+0800 I JOURNAL [initandlisten] preallocating a journal file data/db/journal/prealloc.0
2015-08-12T12:10:03.049+0800 I - [initandlisten] File Preallocator Progress: 461373440/1073741824 42%
2015-08-12T12:10:06.117+0800 I - [initandlisten] File Preallocator Progress: 765460480/1073741824 71%
2015-08-12T12:10:09.061+0800 I - [initandlisten] File Preallocator Progress: 1006632960/1073741824 93%
2015-08-12T12:10:12.670+0800 I JOURNAL [initandlisten] preallocating a journal file data/db/journal/prealloc.1
2015-08-12T12:10:15.176+0800 I - [initandlisten] File Preallocator Progress: 408944640/1073741824 38%
2015-08-12T12:10:18.615+0800 I - [initandlisten] File Preallocator Progress: 629145600/1073741824 58%
2015-08-12T12:10:21.005+0800 I - [initandlisten] File Preallocator Progress: 912261120/1073741824 84%
2015-08-12T12:10:25.238+0800 I JOURNAL [initandlisten] preallocating a journal file data/db/journal/prealloc.2
2015-08-12T12:10:28.073+0800 I - [initandlisten] File Preallocator Progress: 429916160/1073741824 40%
2015-08-12T12:10:31.100+0800 I - [initandlisten] File Preallocator Progress: 639631360/1073741824 59%
2015-08-12T12:10:34.105+0800 I - [initandlisten] File Preallocator Progress: 954204160/1073741824 88%
2015-08-12T12:10:37.881+0800 I JOURNAL [durability] Durability thread started
2015-08-12T12:10:37.881+0800 I JOURNAL [journal writer] Journal writer thread started
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] MongoDB starting : pid=24145 port=27017 dbpath=data/db 64-bit host=xubo
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten]
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten]
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten]
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] db version v3.0.5
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] build info: Linux ip-10-183-35-50 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] allocator: tcmalloc
2015-08-12T12:10:37.917+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "data/db" } }
2015-08-12T12:10:37.944+0800 I INDEX [initandlisten] allocating new ns file data/db/local.ns, filling with zeroes...
2015-08-12T12:10:38.256+0800 I STORAGE [FileAllocator] allocating new datafile data/db/local.0, filling with zeroes...
2015-08-12T12:10:38.256+0800 I STORAGE [FileAllocator] creating directory data/db/_tmp
2015-08-12T12:10:38.293+0800 I STORAGE [FileAllocator] done allocating datafile data/db/local.0, size: 64MB, took 0.017 secs
2015-08-12T12:10:38.311+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2015-08-12T12:11:40.318+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:42183 #1 (1 connection now open)
help
2015-08-12T14:15:19.999+0800 I INDEX [conn1] allocating new ns file data/db/test.ns, filling with zeroes...
2015-08-12T14:15:20.318+0800 I STORAGE [FileAllocator] allocating new datafile data/db/test.0, filling with zeroes...
2015-08-12T14:15:20.337+0800 I STORAGE [FileAllocator] done allocating datafile data/db/test.0, size: 64MB, took 0.019 secs
2015-08-12T14:15:20.354+0800 I WRITE [conn1] insert test.testdb query: { _id: ObjectId('55cae4777aa9649da278f8b1'), test1: "testval1" } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } } } 355ms
2015-08-12T14:15:20.354+0800 I COMMAND [conn1] command test.$cmd command: insert { insert: "testdb", documents: [ { _id: ObjectId('55cae4777aa9649da278f8b1'), test1: "testval1" } ], ordered: true } keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } } } 382ms
2015-08-13T11:09:20.230+0800 I INDEX [conn1] allocating new ns file data/db/xubo.ns, filling with zeroes...
2015-08-13T11:09:20.478+0800 I STORAGE [FileAllocator] allocating new datafile data/db/xubo.0, filling with zeroes...
2015-08-13T11:09:20.502+0800 I STORAGE [FileAllocator] done allocating datafile data/db/xubo.0, size: 64MB, took 0.024 secs
2015-08-13T11:09:20.522+0800 I WRITE [conn1] insert xubo.blog query: { _id: ObjectId('55cc0a607aa9649da278f8c4'), title: "My Blog Post", content: "Here is xubo blog", data: new Date(1439435335789) } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } } } 292ms
2015-08-13T11:09:20.523+0800 I COMMAND [conn1] command xubo.$cmd command: insert { insert: "blog", documents: [ { _id: ObjectId('55cc0a607aa9649da278f8c4'), title: "My Blog Post", content: "Here is xubo blog", data: new Date(1439435335789) } ], ordered: true } keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } } } 292ms



    4.启动mongoDB(新终端):./bin/mongo   

xubo@xubo:~/cloud/mongodb$ ./bin/mongo
MongoDB shell version: 3.0.5
connecting to: test
Welcome to the MongoDB shell.

   启动成功

   5.简单测试:

(1)由于mongoDB的shell是一个功能完备的JavaScript,所以可以运行javascript程序:

> x=200
200
> x
200
> x/5
40
> y=x+100
300
> z=y/x
1.5


> Math.sin(1)
0.8414709848078965
> Math.sin(0.5)
0.479425538604203
> Math.sin(Math.PI)
1.2246063538223773e-16
> Math.sin(Math.PI/2)
1
> new Date("2010/1/1")
ISODate("2009-12-31T16:00:00Z")
> new Date("2015/1/1")
ISODate("2014-12-31T16:00:00Z")


> function aa(n){
... if (n<=1) return 1;
... return n*aa(n-1);
... }
> aa(5)
120
> aa(10)
3628800
> aa(3)
6


 (2).mongodb对database的基本操作


   查看db当前指向哪一个数据库:db

  

> db
test


选择数据库:use dbname

> use xubo
switched to db xubo

增删改查

a).创建数据:新建名为post的局部便利,使用insert插入,然后再用find显示:

> post = {"title":"My Blog Post", "content":"Here is xubo blog", "data":new Date}
{
"title" : "My Blog Post",
"content" : "Here is xubo blog",
"data" : ISODate("2015-08-13T03:08:55.789Z")
}
> db.blog.insert(post)
WriteResult({ "nInserted" : 1 })

b).查找数据:

> db.blog.find()
{ "_id" : ObjectId("55cc0a607aa9649da278f8c4"), "title" : "My Blog Post", "content" : "Here is xubo blog", "data" : ISODate("2015-08-13T03:08:55.789Z") }
>


c).更新update: 在post里面新加comments健,并赋值为update,在insert插入,用find查看时显示一个更新成功

> post.comments="update"
update
> post
{
"title" : "My Blog Post",
"content" : "Here is xubo blog",
"data" : ISODate("2015-08-13T03:08:55.789Z"),
"comments" : "update"
}
> db.blog.update({title:"My Blog Post"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find()
{ "_id" : ObjectId("55cc0a607aa9649da278f8c4"), "title" : "My Blog Post", "content" : "Here is xubo blog", "data" : ISODate("2015-08-13T03:08:55.789Z"), "comments" : "update" }


d).删除remove:删除title为"My Blog Post"的数据,在find查看时显示数据被删除了。

> db.blog.remove({title:"My Blog Post"})
WriteResult({ "nRemoved" : 1 })
> db.blog.find()
>