Mongodb学习笔记(一)
? ? ? ? 64位操作系统 curl http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.2.tgz > mongo.tgz? ? ? ? 32位操作系统?curl http://downloads.mongodb.org/linux/mongodb-linux-i686-2.2.2.tgz > mongo.tgz?? ? ? 解压文件到指定的安装目录? ? ? ? tar -zxvf mongo.tgz?? ? ??? ? ? ? 使用monogDB? ? ? ? 创建一个存放数据库目录 mkdir /data/db? ? ? ? 。使用命令行启动? ? ? ? ? ? <mongodb_home>/bin/mongod --dbpath /data/db? ? ? ? 。使用配置启动? ? ? ? ? ? <mongodb_home>/bin/mongod -f /etc/mongodb.conf
? ? ? ? ?启动参数说明? ? ? ? ? ? ??mongod的主要参数有:?
? dbpath:
数据文件存放路径,每个数据库会在其中创建一个子目录,用于防止同一个实例多次运
行的mongod.lock也保存在此目录中。
? logpath
错误日志文件
? logappend
错误日志采用追加模式(默认是覆写模式)
? bind_ip
对外服务的绑定ip,一般设置为空,及绑定在本机所有可用ip 上,如有需要可以单独
指定
? port
对外服务端口。Web管理端口在这个port的基础上+1000
? fork
以后—aemon形式运行服务
? journal
开启日志功能,通过保存操作日志来降低单机故障的恢复时间,在1.8版本后正式加入,
取代在1.7.5版本中的dur参数。
syncdelay
系统同步刷新磁盘的时间,单位为秒,默认是60秒。
directoryperdb
每个db存放在单独的目录中,建议设置该参数。与MySQL的独立表空间类似
maxConns
最大连接数
repairpath
执行repair时的临时目录。在如果没有开启journal,异常down机后重启,必须执行repair操作。?
配置文件说明:
?[root@devdb bin]# cat /etc/mongodb.conf?
dbpath=/data/db
logpath=/data/log/r3.log
fork=true
diaglog=3
directoryperdb=true
rest=true
说明:使用命令行启动时,相关参数需要加上--,使用配置文件时,象fork这类参数需要使用fork=true形式进行设置? ? ? ??启动、停止root@devdb bin]# ./mongod --dbpath /data/master
Wed Dec 19 13:36:10 [initandlisten] MongoDB starting : pid=2161 port=27017 dbpath=/data/master 64-bit host=devdb.zoneland.net
Wed Dec 19 13:36:10 [initandlisten] db version v2.2.2, pdfile version 4.5
Wed Dec 19 13:36:10 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Wed Dec 19 13:36:10 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Wed Dec 19 13:36:10 [initandlisten] options: { dbpath: "/data/master" }
Wed Dec 19 13:36:11 [initandlisten] journal dir=/data/master/journal
Wed Dec 19 13:36:11 [initandlisten] recover : no journal files present, no recovery needed
Wed Dec 19 13:36:11 [initandlisten] preallocateIsFaster=true 3.72
Wed Dec 19 13:36:11 [initandlisten] preallocateIsFaster=true 3.54
Wed Dec 19 13:36:12 [initandlisten] preallocateIsFaster=true 3.34
Wed Dec 19 13:36:12 [initandlisten] preallocating a journal file /data/master/journal/prealloc.0
Wed Dec 19 13:36:16 [initandlisten]???????????? 608174080/1073741824??? 56%
Wed Dec 19 13:36:19 [initandlisten]???????????? 692060160/1073741824??? 64%
Wed Dec 19 13:36:22 [initandlisten]???????????? 754974720/1073741824??? 70%
Wed Dec 19 13:36:25 [initandlisten]???????????? 817889280/1073741824??? 76%
Wed Dec 19 13:36:28 [initandlisten]???????????? 870318080/1073741824??? 81%
Wed Dec 19 13:36:31 [initandlisten]???????????? 933232640/1073741824??? 86%
Wed Dec 19 13:36:34 [initandlisten]???????????? 985661440/1073741824??? 91%
Wed Dec 19 13:36:37 [initandlisten]???????????? 1048576000/1073741824?? 97%
Wed Dec 19 13:37:09 [initandlisten] preallocating a journal file /data/master/journal/prealloc.1
Wed Dec 19 13:37:12 [initandlisten]???????????? 639631360/1073741824??? 59%
Wed Dec 19 13:37:15 [initandlisten]???????????? 692060160/1073741824??? 64%
Wed Dec 19 13:37:18 [initandlisten]???????????? 744488960/1073741824??? 69%
Wed Dec 19 13:37:21 [initandlisten]???????????? 796917760/1073741824??? 74%
Wed Dec 19 13:37:24 [initandlisten]???????????? 849346560/1073741824??? 79%
Wed Dec 19 13:37:27 [initandlisten]???????????? 912261120/1073741824??? 84%
Wed Dec 19 13:37:30 [initandlisten]???????????? 964689920/1073741824??? 89%
Wed Dec 19 13:37:33 [initandlisten]???????????? 1027604480/1073741824?? 95%
Wed Dec 19 13:38:06 [initandlisten] preallocating a journal file /data/master/journal/prealloc.2
Wed Dec 19 13:38:09 [initandlisten]???????????? 671088640/1073741824??? 62%
Wed Dec 19 13:38:12 [initandlisten]???????????? 754974720/1073741824??? 70%
Wed Dec 19 13:38:15 [initandlisten]???????????? 807403520/1073741824??? 75%
Wed Dec 19 13:38:18 [initandlisten]???????????? 859832320/1073741824??? 80%
Wed Dec 19 13:38:21 [initandlisten]???????????? 922746880/1073741824??? 85%
Wed Dec 19 13:38:24 [initandlisten]???????????? 975175680/1073741824??? 90%
Wed Dec 19 13:38:27 [initandlisten]???????????? 1038090240/1073741824?? 96%
Wed Dec 19 13:39:04 [initandlisten] waiting for connections on port 27017
Wed Dec 19 13:39:04 [websvr] admin web console waiting for connections on port 28017
2.2.2版本的启动过程中会自动创建三个事务日志,完成后默认访问端口是27017,web console是28017停止? ? ? ?直接ctrl+c,使用交互启动方式使用,使用kill -2 pid(pid使用ps aux|grep mongod查看),使用javaScript Shell? ? ? ?mngo? ? ? ?use admin? ? ? ?db.shutdownServer()不允许使用kill -9进行停止? ? ? ?? ? ? ?
快速使用指南
- 连接到数据库
- 连接到一个mongo实例
- 选择数据库
- 显示帮助
- 创建集合并插入文档
- 插入单条文档
??????????????????????????????k = { x : 3 }? ? ? ? ? ? ? ? ? ? ? ? ? ? ??db.things.insert( j ) //在插入第一条文档的时会自动创建数据库和集合
??????????????????????????????db.things.insert( k )? ? ? ? ? ? ? ? ? ? ? ? ? ? ??show collections? ? ? ? ? ? ? ? ? ? ? ? ? ? ? db.things.find() //集合所有文档
- 使用循环插入多个文档
- 使用游标
- serverStatus
- db.runCommand({"serverStatus":1})
? ? ? ? ?[root@devdb bin]# ./mongo
MongoDB shell version: 2.2.2
connecting to: test
> use admin
switched to db admin
> db.adduser("root","zone2009")
Wed Dec 19 15:01:40 TypeError: db.adduser is not a function (shell):1
> db.addUser("root","zone2009")
{
??????? "user" : "root",
??????? "readOnly" : false,
??????? "pwd" : "51cdc4cdf21d6dc696961cf6f47c6086",
??????? "_id" : ObjectId("50d1665f380b83390b46ba40")
}
> use test
switched to db test
> db.addUser("test_user","zone2009")
{
??????? "user" : "test_user",
??????? "readOnly" : false,
??????? "pwd" : "5c76cd3b02e09a71976ec268bc7ed866",
??????? "_id" : ObjectId("50d16671380b83390b46ba41")
}
> db.addUser("read_only","zone2009",true)
{
??????? "user" : "read_only",
??????? "readOnly" : true,
??????? "pwd" : "3cd54d9306870d554a41d94264f630f9",
??????? "_id" : ObjectId("50d16680380b83390b46ba42")
}
> use test
switched to db test
Wed Dec 19 15:03:29 DBClientCursor::init call() failed
Wed Dec 19 15:03:29 query failed : admin.$cmd { replSetGetStatus: 1.0, forShell: 1.0 } to: 127.0.0.1:27017
> exit
bye
加入用户名,重新带--auth参数启动mongodb,进行如下操作,可以看到普通用户只能按设置的权限进行操作,只有管理员用户可以进行show dbs等操作
[root@devdb bin]# ./mongo
MongoDB shell version: 2.2.2
connecting to: test
> db.test.fin();
Wed Dec 19 15:03:47 TypeError: db.test.fin is not a function (shell):1
> db.test.find();
error: {
??????? "$err" : "unauthorized db:test ns:test.test lock type:1 client:127.0.0.1",
??????? "code" : 10057
}
> db.auth("read_only","zone2009");
1
> db.test.find();
> db.test.insert({"x":2});
unauthorized
> db.auth("test_user","zone2009");
1
> db.test.insert({"x":2});
> db.test.find();
{ "_id" : ObjectId("50d16719289d4b292e3f80eb"), "x" : 2 }
> show dbs
Wed Dec 19 15:05:14 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
> use admin
switched to db admin
> db.auth("root","zone2009");
1
> show dbs
admin?? 0.203125GB
local?? (empty)
test??? 0.203125GB
> db.test.insert({"x":2});
? ? ?????????????????????