实验环境:CentOS 5.8 i386 fianl
具体步骤如下:
1)配置官方YUM源
点击(此处)折叠或打开
- [root@localhost ~]# vim /etc/yum.repos.d/10gen.repo
- [root@localhost ~]# cat /etc/yum.repos.d/10gen.repo
- [10gen]
- name=10gen Repository
- baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
- gpgcheck=0
- enabled=1
点击(此处)折叠或打开
- [root@localhost ~]# vim /etc/yum.repos.d/10gen.repo
- [root@localhost ~]# cat /etc/yum.repos.d/10gen.repo
- [10gen]
- name=10gen Repository
- baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
- gpgcheck=0
- enabled=1
#############################################################################################
#其中,官方源中包括以下软件包
#mongo-10gen 这个包包含了最新最稳定的mongodb工具
#mongo-server-10gen 这个包包含了最新最稳定版本的mongod和mongos守护进程及相关的配置和初始化脚本
#
#mongo18-10gen 这个包包含了之前发行版中的mongodb工具
#mongo18-server-10gen 这个包包含了前一个稳定版的mongod和mongos守护进程及相关的配置和初始化脚本
##############################################################################################
2)系统更新
点击(此处)折叠或打开
- [root@localhost ~]# yum update
3)安装mongodb
点击(此处)折叠或打开
- [root@localhost ~]# yum install mongo-10gen mongo-10gen-server
点击(此处)折叠或打开
- Installed:
- mongo-10gen.i686 0:2.0.5-mongodb_1 mongo-10gen-server.i686 0:2.0.5-mongodb_1
4)配置mongodb
配置文件为:/etc/mongod.conf
初始化脚本为:/etc/rc.d/init.d/mongod
数据存储路径:/var/lib/mongo/
日志文件存储路径:/var/log/mongo/
守护进程运行用户为:mongod
以上均为默认值
5)Mongodb控制
加入开机启动
点击(此处)折叠或打开
- [root@localhost ~]# chkconfig mongod on
- [root@localhost ~]# chkconfig --list | grep mongod
- mongod 0:off 1:off 2:on 3:on 4:on 5:on 6:off
点击(此处)折叠或打开
- [root@localhost ~]# service mongod start
- Starting mongod: all output going to: /var/log/mongo/mongod.log
- forked process: 26351
- [ OK ]
点击(此处)折叠或打开
- [root@localhost ~]# service mongod stop
- Stopping mongod: [ OK ]
重启mongodb
点击(此处)折叠或打开
- [root@localhost ~]# service mongod restart
- Stopping mongod: [ OK ]
- Starting mongod: all output going to: /var/log/mongo/mongod.log
- forked process: 26420
- [ OK ]
6)简单使用
查找
点击(此处)折叠或打开
- [root@localhost ~]# mongo
- MongoDB shell version: 2.0.5
- connecting to: test
- > db.test.save( { a: 1})
- > db.test.find()
- { "_id" : ObjectId("4fb83ca7698712e93dad12a7"), "a" : 1 }
点击(此处)折叠或打开
- > doc = { author:'joe', created : new Date('03/28/2009')}
- { "author" : "joe", "created" : ISODate("2009-03-27T16:00:00Z") }
- > db.test.insert(doc)
- > db.test.find()
- { "_id" : ObjectId("4fb83ca7698712e93dad12a7"), "a" : 1 }
- { "_id" : ObjectId("4fb83e2c698712e93dad12a8"), "author" : "joe", "created" : ISODate("2009-03-27T16:00:00Z") }
点击(此处)折叠或打开
- > db.test.update({author : 'joe'}, {$inc: { check: 1000 }});
- > db.test.find()
- { "_id" : ObjectId("4fb83ca7698712e93dad12a7"), "a" : 1 }
- { "_id" : ObjectId("4fb83e2c698712e93dad12a8"), "author" : "joe", "check" : 1000, "created" : ISODate("2009-03-27T16:00:00Z") }
删除
点击(此处)折叠或打开
- > db.test.remove({a:1});
- > db.test.find()
- { "_id" : ObjectId("4fb83e2c698712e93dad12a8"), "author" : "joe", "check" : 1000, "created" : ISODate("2009-03-27T16:00:00Z") }