1.下载mongodb-linux-x86_64-2.6.5.gz,放到linux下目标目录,例如/opt/
tar xzvf mongodb-linux-x86_64-2.6.5.gz
解压后,重命名目录为:mongodb2.6.5
2.做软连接,cd /bin
ln -s /opt/mongodb2.6.5/bin/mongod mongod
3.启动
mongod --port 27027 --fork --dbpath=/opt/mongodb2.6.5/data --logpath=/opt/mongodb2.6.5/mongod.log --journal --logappend
启动时的常用参数说明:
--bind_ip 绑定IP,绑定后只能绑定的IP访问服务
--dbpath 指定数据库目录
--port 指定数据库端口,默认是27107
--logpath 指定日志存放目录
--logappend 使用追加的方式写日志
--pidfilepath 指定进程文件,不指定则不产生进程文件
--journal 启用日志
--maxConns 最大的并发连接数,默认2000
--fork 将服务放到后台运行
--notablescan 不允许表扫描
--syncdelay 数据写入硬盘的时间(秒),0是不等待,直接写入
--auth 以认证方式启动数据库
4.让机器启动时能够自动启动mongodb。vi /etc/rc.local 加入:
mongod --port 27027 --fork --dbpath=/opt/mongodb2.6.5/data --logpath=/opt/mongodb2.6.5/mongod.log --journal --logappend
一些mongodb的操作:
[root@iZ23bv2prypZ ~]# mongo 127.0.0.1:27027
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27027/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> show dbs
admin (empty)
local 0.078GB
> use admin
switched to db admin
> show collections
> db.system.users.find()
> use test
switched to db test
> show collections
> db.createCollection("li")
{ "ok" : 1 }
> show collections
li
system.indexes
> db.li.insert(name:"iphone5s",price:1000,addr:"hangzhou")
2014-12-01T12:05:13.693+0800 SyntaxError: Unexpected token :
> db.li.insert({name:"iphone5s",price:1000,addr:"hangzhou"})
WriteResult({ "nInserted" : 1 })
> db.li.find()
{ "_id" : ObjectId("547be91c8c79fb89302dee5d"), "name" : "iphone5s", "price" : 1000, "addr" : "hangzhou" }
> use hz
switched to db hz
> db.createCollection("wa")
{ "ok" : 1 }
> db.hz.insert({name:"huawei p7",price:2000,addr:"shanghai"})
WriteResult({ "nInserted" : 1 })
> db.hz.find()
{ "_id" : ObjectId("547bea148c79fb89302dee5e"), "name" : "huawei p7", "price" : 2000, "addr" : "shanghai" }
> show dbs
admin (empty)
hz 0.078GB
local 0.078GB
test 0.078GB
> use test
switched to db test
> db.li.find()
{ "_id" : ObjectId("547be91c8c79fb89302dee5d"), "name" : "iphone5s", "price" : 1000, "addr" : "hangzhou" }
>
> show dbs
admin (empty)
hz 0.078GB
local 0.078GB
test 0.078GB
> use macao
switched to db macao
> db.createCollection("auth_policy")
{ "ok" : 1 }
> db.addUser("macao","macao") // 创建用户和密码
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "macao", "roles" : [ "dbOwner" ] }
> db.auth("macao","macao")
1
> db.system.users.find();
> use admin
switched to db admin
> db.system.users.find();
{ "_id" : "macao.macao", "user" : "macao", "db" : "macao", "credentials" : { "MONGODB-CR" : "fae8a8507a4c2fb39e15199f6620cfaa" }, "roles" : [ { "role" : "dbOwner", "db" : "macao" } ] }
> exit
bye
停止mongodb
[root@iZ23bv2prypZ ~]# mongo 127.0.0.1:27027
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27027/test
> use admin
switched to db admin
> db.shutdownServer()
2014-12-01T14:11:08.664+0800 DBClientCursor::init call() failed
server should be down...
2014-12-01T14:11:08.667+0800 trying reconnect to 127.0.0.1:27027 (127.0.0.1) failed
2014-12-01T14:11:08.667+0800 warning: Failed to connect to 127.0.0.1:27027, reason: errno:104 Connection reset by peer
2014-12-01T14:11:08.667+0800 reconnect 127.0.0.1:27027 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27027 (127.0.0.1), connection attempt failed
> exit
bye
以认证方式重新启动数据库
[root@iZ23bv2prypZ ~]# mongod --port 27027 --fork --dbpath=/opt/mongodb2.6.5/data --logpath=/opt/mongodb2.6.5/mongod.log --journal --logappend --auth
about to fork child process, waiting until server is ready for connections.
forked process: 10637
child process started successfully, parent exiting
使用用户名和密码进行连接
[root@iZ23bv2prypZ ~]# mongo 127.0.0.1:27027/macao -umacao -pmacao
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27027/macao
> show dbs
2014-12-01T14:16:50.633+0800 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
> show collections
auth_policy
system.indexes
> db.auth_policy.find()
> db.auth_policy.insert({user:"lichunxi",objects:"instance1",operations:".*"})
WriteResult({ "nInserted" : 1 })
> db.auth_policy.find()
{ "_id" : ObjectId("547c0c59713e2d34037ccc8b"), "user" : "lichunxi", "objects" : "instance1", "operations" : ".*" }