安装及配置
说明,MongoDB下载然后解压缩以后就可以直接使用了,不需要编译及安装。
在页面http://www.mongodb.org/display/DOCS/Downloads中找到适合于你所在操作系统的MongoDB版本。
[root@CentOS_Test_Server software]# wget http://downloads.mongodb.org/linux/mongodb-linux-i686-1.4.2.tgz
/home/software是存放软件的目录,把mongodb解压到其它的目录下
查看压缩包我们可以知道,此包已经包含有目录名称了,我们解压缩时就不需要再指定MongoDb的目录了。
[root@CentOS_Test_Server software]# tar tzvf mongodb-linux-i686-1.4.2.tgz | more
drwxr-xr-x buildslave/buildslave 0 2010-04-28 00:18:07 mongodb-linux-i686-1.4.2/
drwxr-xr-x buildslave/buildslave 0 2010-04-28 00:18:07 mongodb-linux-i686-1.4.2/lib/
...
直接解压缩到目录/usr/local/webserver 下。
[root@CentOS_Test_Server software]# tar zxvf mongodb-linux-i686-1.4.2.tgz -C /usr/local/webserver/
[root@CentOS_Test_Server webserver]# ls
eaccelerator_cache mongodb-linux-i686-1.4.2 mysql nginx php squid
将目录改名,方便以后使用方便。
[root@CentOS_Test_Server webserver]# mv mongodb-linux-i686-1.4.2/ mongodb
[root@CentOS_Test_Server software]# cd /usr/local/webserver/mongodb
[root@CentOS_Test_Server mongodb]# ls
bin GNU-AGPL-3.0 include lib README THIRD-PARTY-NOTICES
直接查看README文件的内容,里面的说明对使用MongoDB也会很有帮助。
[root@CentOS_Test_Server mongodb]# more README
MongoDB
=======
Welcome to MongoDB!
Package Contents
----------------
bin/mongod - MongoDB server
bin/mongo - MongoDB client
bin/mongodump - MongoDB dump tool - for backups, snapshots, etc..
bin/mongorestore - MongoDB restore a dump
bin/mongoexport - Export a single collection to test (json,csv)
bin/mongoimportjson - Import a json file into a collection
bin/mongofiles - Utility for putting and getting files from MongoDB gridfs
Useful Resources
----------------
MongoDB Website
* http://www.mongodb.org/
Documentation
* http://www.mongodb.org/display/DOCS/Documentation
MongoDB Maillists & IRC
* http://www.mongodb.org/display/DOCS/Community
如果直接输入命令./mongod,则 MongoDB的数据直接保存在/data/db目录(windows操作系统下是c:\data\db目录), 默认监听在27017端口。
如 果你启动MongoDB时没有指定任何参数,同时目录/data/db也不存在,则会报错。
[root@CentOS_Test_Server bin]# ./mongod --help | grep fork
--fork fork server process
[root@CentOS_Test_Server bin]# ./mongod --help | grep dbpath
--dbpath arg (=/data/db/) directory for datafiles
dbpath
--fastsync indicate that this instance is starting from a dbpath
[root@CentOS_Test_Server bin]# ./mongod --help | grep port
--port arg specify port number
--source arg when slave: specify master as <server:port>
运 行./mongod --help命令时mongodb提示了一下,也就是32位操作系统只支持最大2G的文件,如果你的应用中数据量很大,超过了2G,推荐安装64位的操作 系统。2Gb or Not 2Gb - File limits说明 了为什么32位操作系统只支持最大2G的文件。
[root@CentOS_Test_Server bin]# ./mongod --help | more
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more
创建目录保存MongoDB数据的目录。
[root@CentOS_Test_Server bin]# mkdir /home/mongodb_data
启动MongoDB
[root@CentOS_Test_Server bin]# ./mongod --fork --dbpath=/home/mongodb_data --logpath /home/mongodb_data/mongodb.log --logappend
all output going to: /home/mongodb_data/mongodb.log
forked process: 3799
[root@CentOS_Test_Server bin]# ps aux | grep mongo
root 3799 0.0 0.7 59032 2008 ? Ssl 16:59 0:00 ./mongod --fork --dbpath=/home/mongodb_data --logpath /home/mongodb_data/mongodb.log --logappend
root 3825 0.0 0.2 3920 628 pts/1 S+ 17:00 0:00 grep mongo
[root@CentOS_Test_Server bin]# netstat -antl | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
停止 MongoDB的进程时不能用kill -9,而最好用kill -15,否则会导致出现问题。
同时会自动启动一个端口为28017的服务,此服务用于监控MongoDB的状态
[root@CentOS_Test_Server bin]# netstat -a | grep 28017
tcp 0 0 *:28017 *:* LISTEN
用curl可以在启动MongoDB所在的服务器上面直接访问, 如果想从其它的访问访问此服务,则必须在防火墙上开放28017端口。
[root@CentOS_Test_Server bin]# curl localhost:28017
<html><head><title>mongodb CentOS_Test_Server:27017 </title></head><body><h2>mongodb CentOS_Test_Server:27017 </h2><p>
<pre>db version v1.4.2, pdfile version 4.5
git hash: 53749fc2d547a3139fcf169d84d58442778ea4b0
sys info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
.....
我在服务器上打开了28017端口,不知道为什么 在其它的电脑上面输入地址http://192.168.1.111:28017/无法访问,暂时不知道是什么原因。
[root@CentOS_Test_Server bin]# iptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 28017 -j ACCEPT
[root@CentOS_Test_Server bin]# /etc/init.d/iptables save
Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
更多MongoDB启动的问题,请移步 Starting and Stopping Mongo,http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
测试
[root@CentOS_Test_Server bin]# ls
运行MongoDB下面bin目录下的mongo命 令,就可以进入MongoDB的shell界面了,跟MySQL有点类似,输入命令help,其它的看帮助基本上就明白了。
mongo mongod mongodump mongoexport mongofiles mongoimport mongorestore mongos mongosniff mongostat
[root@CentOS_Test_Server bin]# ./mongo
MongoDB shell version: 1.4.2
url: test
connecting to: test
type "help" for help
> help
HELP
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
use <db name> set curent database to <db name>
db.help() help on DB methods
db.foo.help() help on collection methods
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
在MongoDB中,在使用Database或Collection前不需要提前创建,在使用的过程中会 自动创建。
更多的有关信息请移步MongoDB Tutorial。
a.测试一
我们来测试一下就明白了
> use recommender //在此之前我可从来没有创建过数据库recommender
> a={name: "caihuafeng"} //也就是json对象,大家看起来是不是非常熟悉
{ "name" : "caihuafeng" }
> b={website: "1616.net"}
{ "website" : "1616.net" }
> db.data.save(a) //在此之前我可没有创建过表data,在数据库recommender的表data中保存数据a,可以理解为往MySQL的表data中添加一条记录
> db.data.save(b) //在数据库recommender的表data中保存数据b,可以理解为往MySQL的表data中添加一条记录
> db.data.find() //显示数据库recommender的表data中的所有数据
{ "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "caihuafeng" }
{ "_id" : ObjectId("4bee745f0863b1c233b8b7eb"), "website" : "1616.net" }
> show collections //显示数据库recommender中的所有表(collection在这里相当于MySQL中的表)
data
system.indexes //这个表是自动创建的
显示数据库recommender中website为1616.net的记录,相当于MySQL中的 SELECT * FROM data WHERE website='1616.net';
> db.data.find({website:"1616.net"})
{ "_id" : ObjectId("4bee745f0863b1c233b8b7eb"), "website" : "1616.net" }
显示 数据库recommender中name为caihuafeng的记录,相当于MySQL中的SELECT * FROM data WHEREname ='caihuafeng';
> db.data.find({name:"caihuafeng"})
{ "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "caihuafeng" }
MongoDB 比其它的关系型数据库更加灵活,因为每一行的记录都可以有不同的结构,而且表的结构根本上不需要提前创建,灵活极了。
b.测试二 教学网 eduyo.com
发现就是在写js代码,然后跟数据库结合起来了。
> for (var i = 1; i <= 10; i++) db.data.save({"x":6, "name":"caihuafeng" + i});
> db.data.find();
{ "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "caihuafeng" }
{ "_id" : ObjectId("4bee745f0863b1c233b8b7eb"), "website" : "1616.net" }
{ "_id" : ObjectId("4bee804ba23d558eb6687117"), "x" : 6, "name" : "caihuafeng1" }
{ "_id" : ObjectId("4bee804ba23d558eb6687118"), "x" : 6, "name" : "caihuafeng2" }
{ "_id" : ObjectId("4bee804ba23d558eb6687119"), "x" : 6, "name" : "caihuafeng3" }
{ "_id" : ObjectId("4bee804ba23d558eb668711a"), "x" : 6, "name" : "caihuafeng4" }
{ "_id" : ObjectId("4bee804ba23d558eb668711b"), "x" : 6, "name" : "caihuafeng5" }
{ "_id" : ObjectId("4bee804ba23d558eb668711c"), "x" : 6, "name" : "caihuafeng6" }
{ "_id" : ObjectId("4bee804ba23d558eb668711d"), "x" : 6, "name" : "caihuafeng7" }
{ "_id" : ObjectId("4bee804ba23d558eb668711e"), "x" : 6, "name" : "caihuafeng8" }
{ "_id" : ObjectId("4bee804ba23d558eb668711f"), "x" : 6, "name" : "caihuafeng9" }
{ "_id" : ObjectId("4bee804ba23d558eb6687120"), "x" : 6, "name" : "caihuafeng10" }
> var cursor = db.data.find();
> while (cursor.hasNext()) {print(tojson(cursor.next()))};
{ "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "caihuafeng" }
{ "_id" : ObjectId("4bee745f0863b1c233b8b7eb"), "website" : "1616.net" }
{
"_id" : ObjectId("4bee804ba23d558eb6687117"),
"x" : 6,
"name" : "caihuafeng1"
}
{
"_id" : ObjectId("4bee804ba23d558eb6687118"),
"x" : 6,
"name" : "caihuafeng2"
}
{
"_id" : ObjectId("4bee804ba23d558eb6687119"),
"x" : 6,
"name" : "caihuafeng3"
}
{
"_id" : ObjectId("4bee804ba23d558eb668711a"),
"x" : 6,
"name" : "caihuafeng4"
}
{
"_id" : ObjectId("4bee804ba23d558eb668711b"),
"x" : 6,
"name" : "caihuafeng5"
}
{
"_id" : ObjectId("4bee804ba23d558eb668711c"),
"x" : 6,
"name" : "caihuafeng6"
}
{
"_id" : ObjectId("4bee804ba23d558eb668711d"),
"x" : 6,
"name" : "caihuafeng7"
}
{
"_id" : ObjectId("4bee804ba23d558eb668711e"),
"x" : 6,
"name" : "caihuafeng8"
}
{
"_id" : ObjectId("4bee804ba23d558eb668711f"),
"x" : 6,
"name" : "caihuafeng9"
}
{
"_id" : ObjectId("4bee804ba23d558eb6687120"),
"x" : 6,
"name" : "caihuafeng10"
}
> db.data.find({x:6}, {name:true}).forEach(function(x) {print(tojson(x));});
{ "_id" : ObjectId("4bee804ba23d558eb6687117"), "name" : "caihuafeng1" }
{ "_id" : ObjectId("4bee804ba23d558eb6687118"), "name" : "caihuafeng2" }
{ "_id" : ObjectId("4bee804ba23d558eb6687119"), "name" : "caihuafeng3" }
{ "_id" : ObjectId("4bee804ba23d558eb668711a"), "name" : "caihuafeng4" }
{ "_id" : ObjectId("4bee804ba23d558eb668711b"), "name" : "caihuafeng5" }
{ "_id" : ObjectId("4bee804ba23d558eb668711c"), "name" : "caihuafeng6" }
{ "_id" : ObjectId("4bee804ba23d558eb668711d"), "name" : "caihuafeng7" }
{ "_id" : ObjectId("4bee804ba23d558eb668711e"), "name" : "caihuafeng8" }
{ "_id" : ObjectId("4bee804ba23d558eb668711f"), "name" : "caihuafeng9" }
{ "_id" : ObjectId("4bee804ba23d558eb6687120"), "name" : "caihuafeng10" }
只取前3条记录,跟MySQL中的limit是类似的意思。
> db.data.find().limit(3);
{ "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "caihuafeng" }
{ "_id" : ObjectId("4bee745f0863b1c233b8b7eb"), "website" : "1616.net" }
{ "_id" : ObjectId("4bee804ba23d558eb6687117"), "x" : 6, "name" : "caihuafeng1"