Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

第 47 章 MongoDB

目录

47.1. Quickstart
47.1.1. 二进制tar包安装
47.1.2. Ubuntu MongoDB
47.1.3. CentOS MongoDB
47.1.4. MongoDB + Hadoop
47.2. Security and Authentication
47.2.1. 超级管理员
47.2.2. 数据库用户
47.2.3. 删除用户
47.3. Replication
47.3.1. Master
47.3.2. Slave
47.3.3. 测试
47.4. Drivers
47.4.1. Using MongoDB in PHP
47.5. mongo shell
47.5.1. help
47.5.2. show 查看命令
47.5.2.1. show dbs
47.5.2.2. show collections
47.5.2.3. show users
47.5.2.4. show profile
47.5.3. 切换数据库
47.5.4. 记录管理
47.5.4.1. save
47.5.4.2. insert
47.5.4.3. update
47.5.4.4. remove
47.5.4.5. find
47.5.4.6. count()
47.5.4.7. group()
47.6. 命令
47.6.1. mongo
47.6.1.1. 管道操作
47.6.2. mongodump - Backup
47.6.3. mongorestore
47.6.4. mongostat
47.6.5. mongotop
47.6.6. mongofiles - Browse and modify a GridFS filesystem.
47.6.6.1. list 浏览文件
47.6.6.2. put 上传文件
47.6.6.3. get 下载
47.6.6.4. delete 删除
47.7. Mongo Admin UI
47.7.1. RockMongo
47.7.2. MongoVUE
47.8. Unique Indexes
47.8.1. 索引管理
47.8.1.1. 唯一索引
47.9. Map-Reduce
47.9.1. 使用 Map-Reduce 统计Web 服务器 access.log 日志文件

http://www.mongodb.org/

47.1. Quickstart

47.1.1. 二进制tar包安装

Install MongoDB

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.5.5.tgz

debian:/srv# tar zxf mongodb-linux-x86_64-1.5.5.tgz
debian:/srv# ln -s mongodb-linux-x86_64-1.5.5 mongodb
			

Create a data directory

By default MongoDB will store data in /data/db, but it won't automatically create that directory. To create it, do:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db
			

You can also tell MongoDB to use a different data directory, with the --dbpath option.

Run and connect to the server

First, start the MongoDB server in one terminal:

$ ./mongodb/bin/mongod
			

In a separate terminal, start the shell, which will connect to localhost by default:

$ ./mongodb/bin/mongo
> db.foo.save( { a : 1 } )
> db.foo.find()
			

Congratulations, you've just saved and retrieved your first document with MongoDB!

例 47.1. MongoDB Test

				
debian:/srv/mongodb/bin# ./mongo
MongoDB shell version: 1.5.5
connecting to: test
[initandlisten] Thu Jul 22 16:42:07 connection accepted from 127.0.0.1:42876 #1
> db.foo.save({a:1})
Thu Jul 22 16:42:23 allocating new datafile /data/db/test.ns, filling with zeroes...
Thu Jul 22 16:42:23 done allocating datafile /data/db/test.ns, size: 16MB,  took 0.025 secs
Thu Jul 22 16:42:23 allocating new datafile /data/db/test.0, filling with zeroes...
Thu Jul 22 16:42:23 done allocating datafile /data/db/test.0, size: 64MB,  took 0.105 secs
[conn1] Thu Jul 22 16:42:23 building new index on { _id: 1 } for test.foo
[conn1] Thu Jul 22 16:42:23 Buildindex test.foo idxNo:0 { name: "_id_", ns: "test.foo", key: { _id: 1 } }
[conn1] Thu Jul 22 16:42:23 done for 0 records 0secs
[conn1] Thu Jul 22 16:42:23 insert test.foo 136ms
> Thu Jul 22 16:42:23 allocating new datafile /data/db/test.1, filling with zeroes...
Thu Jul 22 16:42:24 done allocating datafile /data/db/test.1, size: 128MB,  took 0.228 secs
> db.foo.find()
{ "_id" : ObjectId("4c48046f74050cbf6c9a0ef6"), "a" : 1 }

> use neo
switched to db neo
> db.foo.save({a:1})
Thu Jul 22 16:54:50 allocating new datafile /data/db/neo.ns, filling with zeroes...
Thu Jul 22 16:54:50 done allocating datafile /data/db/neo.ns, size: 16MB,  took 0.026 secs
Thu Jul 22 16:54:50 allocating new datafile /data/db/neo.0, filling with zeroes...
Thu Jul 22 16:54:50 done allocating datafile /data/db/neo.0, size: 64MB,  took 0.122 secs
[conn1] Thu Jul 22 16:54:50 building new index on { _id: 1 } for neo.foo
[conn1] Thu Jul 22 16:54:50 Buildindex neo.foo idxNo:0 { name: "_id_", ns: "neo.foo", key: { _id: 1 } }
Thu Jul 22 16:54:50 allocating new datafile /data/db/neo.1, filling with zeroes...
[conn1] Thu Jul 22 16:54:50 done for 0 records 0.01secs
[conn1] Thu Jul 22 16:54:50 insert neo.foo 164ms
> Thu Jul 22 16:54:50 done allocating datafile /data/db/neo.1, size: 128MB,  took 0.217 secs

> db.foo.find()
{ "_id" : ObjectId("4c48075a74050cbf6c9a0ef7"), "a" : 1 }
>

> db.neo.save({a:1})
[conn1] Thu Jul 22 16:58:32 building new index on { _id: 1 } for neo.neo
[conn1] Thu Jul 22 16:58:32 Buildindex neo.neo idxNo:0 { name: "_id_", ns: "neo.neo", key: { _id: 1 } }
[conn1] Thu Jul 22 16:58:32 done for 0 records 0.024secs
> db.neo.find()
{ "_id" : ObjectId("4c48083874050cbf6c9a0ef8"), "a" : 1 }

				
				

Starting Mongo

Running as a Daemon

 $ ./mongod --fork --logpath /var/log/mongodb.log --logappend
			

47.1.2. Ubuntu MongoDB

$ sudo apt-get install mongodb-server mongodb-clients
			
$ /etc/init.d/mongodb
Usage: /etc/init.d/mongodb {start|stop|force-stop|restart|force-reload|status}
			

47.1.3. CentOS MongoDB

# yum install mongodb-server

# chkconfig mongod on

# service mongod start
			

单独安装客户端

# yum install mongodb
			

47.1.4. MongoDB + Hadoop

Hadoop Connector

http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-hadoop/

git clone https://github.com/mongodb/mongo-hadoop.git
git checkout release-1.0
			
# vim build.sbt
hadoopRelease in ThisBuild := "cdh4"
			
./sbt package
			
			
wget --no-check-certificate https://github.com/downloads/mongodb/mongo-java-driver/mongo-2.7.3.jar
cp mongo-2.7.3.jar /usr/lib/hadoop/lib/
cp core/target/mongo-hadoop-core_cdh3u3-1.0.0.jar /usr/lib/hadoop/lib/
			
			

待续......

comments powered by Disqus