Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101496
  • 博文数量: 20
  • 博客积分: 648
  • 博客等级: 上士
  • 技术积分: 222
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-02 11:43
文章分类

全部博文(20)

文章存档

2013年(3)

2012年(8)

2011年(7)

2010年(2)

我的朋友

分类: 数据库开发技术

2012-03-22 09:56:43

Mongod 是 MongoDB 核心程序,通常情况下我们只需折腾该程序即可。

1. dbpath & port

默认数据存储路径是 /data/db,默认端口 27017,默认 HTTP 端口 28017。用 --dbpath 和 --port 改吧。

$ sudo ./mongod --dbpath /var/mongodb --port 1234

Sat Jul 24 22:58:50 MongoDB starting : pid=1683 port=1234 dbpath=/var/mongodb 64-bit

** NOTE: This is a development version (1.5.4) of MongoDB.

** Not recommended for production.

Sat Jul 24 22:58:50 db version v1.5.4, pdfile version 4.5

Sat Jul 24 22:58:50 git version: 6c1361df41d9cabf9026364427a7df44b3c304fd

Sat Jul 24 22:58:50 sys info: Linux domU-12-31-39-06-79-A1 2.6.21.7-2.ec2.v1.2.fc8xen

[initandlisten] Sat Jul 24 22:58:50 waiting for connections on port 1234

[websvr] Sat Jul 24 22:58:50 web admin interface listening on port 2234

从启动信息可以看到 Web 管理端口 2234,CTRL + C 即可停止该进程。--bind_ip 用于设定监听绑定 IP。

Sat Jul 24 22:58:53 got kill or ctrl c signal 2 (Interrupt), will terminate after current cmd ends

[interruptThread] Sat Jul 24 22:58:53 now exiting

Sat Jul 24 22:58:53 dbexit:

[interruptThread] Sat Jul 24 22:58:53 shutdown: going to close listening sockets...

[interruptThread] Sat Jul 24 22:58:53 going to close listening socket: 5

[interruptThread] Sat Jul 24 22:58:53 going to close listening socket: 6

[interruptThread] Sat Jul 24 22:58:53 going to close listening socket: 7

[interruptThread] Sat Jul 24 22:58:53 going to close listening socket: 8

[interruptThread] Sat Jul 24 22:58:53 shutdown: going to flush oplog...

[interruptThread] Sat Jul 24 22:58:53 shutdown: going to close sockets...

[interruptThread] Sat Jul 24 22:58:53 shutdown: waiting for fs preallocator...

[interruptThread] Sat Jul 24 22:58:53 shutdown: closing all files...

Sat Jul 24 22:58:53 closeAllFiles() finished

[interruptThread] Sat Jul 24 22:58:53 shutdown: removing fs lock...

Sat Jul 24 22:58:53 dbexit: really exiting now

默认情况下,所有的 DB 都存储到 --dbpath 指定的目录中。

$ sudo ./mongod --dbpath /var/mongodb --fork --logpath /dev/null

forked process: 2018

all output going to: /dev/null

$ ./mongo

MongoDB shell version: 1.5.4

connecting to: test

> db.c1.insert({a:1})

> use blog

switched to db blog

> db.c2.insert({b:1})

> exit

bye

$ ls /var/mongodb/

blog blog.0 blog.1 blog.ns mongod.lock test test.0 test.1 test.ns _tmp

$ sudo kill -INT 2018

可以用——directoryperdb 参数让系统为每个 DB 创建一个独立子目录。

$ sudo ./mongod --dbpath /var/mongodb --fork --logpath /dev/null --directoryperdb

forked process: 2060

all output going to: /dev/null

$ ./mongo

MongoDB shell version: 1.5.4

connecting to: test

> db.c1.insert({a:1})

> use blog

switched to db blog

> db.c2.insert({b:1})

> exit

bye

$ ls -R /var/mongodb

/var/mongodb:

blog mongod.lock test _tmp

/var/mongodb/blog:

blog.0 blog.1 blog.ns

/var/mongodb/test:

test.0 test.1 test.ns

/var/mongodb/_tmp:

2. daemon

如果想以 Daemon 方式运行,需要同时使用 --fork、--logpath 参数。

$ sudo ./mongod --dbpath /var/mongodb --fork --logpath /dev/null

forked process: 1797

all output going to: /dev/null

想要停止服务,可以发送 INT 或 TERM 信号。

$ sudo kill -INT 1797

或者使用 mongo 连接到服务器,然后执行 shutdownServer 命令。

$ ./mongo

MongoDB shell version: 1.5.4

connecting to: test

> use admin

switched to db admin

> db.shutdownServer()

Sat Jul 24 23:11:36 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1

server should be down...

Sat Jul 24 23:11:36 trying reconnect to 127.0.0.1

Sat Jul 24 23:11:36 reconnect 127.0.0.1 failed couldn't connect to server {ip: "127.0.0.1", port: 27017}

Sat Jul 24 23:11:36 MessagingPort say send() errno:9 Bad file descriptor 127.0.0.1:27017

Sat Jul 24 23:11:36 Error: error doing query: unknown (anon):1421

> exit

bye

3. logging

与 Logging 有关的参数除了 --logpath,还有 --logappend 和 --verbose。

默认情况下,Logging 是覆盖模式(overwrite),通过 --logappend 可以添加模式记录日志。

参数 --verbose 设置记录等级,相当于 -v,更多的级别包括 -vv 直到 -vvvvv。与之相对的是 --quiet,生成最少的日志信息。

还可以用 --cpu 记录 CPU 的相关信息。

$ sudo ./mongod --dbpath /var/mongodb -vvvvv

Sun Jul 25 00:41:22 MongoDB starting : pid=2113 port=27017 dbpath=/var/mongodb 64-bit

** NOTE: This is a development version (1.5.4) of MongoDB.

** Not recommended for production.

Sun Jul 25 00:41:22 db version v1.5.4, pdfile version 4.5

Sun Jul 25 00:41:22 git version: 6c1361df41d9cabf9026364427a7df44b3c304fd

Sun Jul 25 00:41:22 sys info: Linux domU-12-31-39-06-79-A1 2.6.21.7-2.ec2.v1.2.fc8xen #1 ...

[initandlisten] Sun Jul 25 00:41:22 query: local.system.namespaces{ name: /^local.temp./ }

[initandlisten] Sun Jul 25 00:41:22 Accessing: local for the first time

[initandlisten] Sun Jul 25 00:41:22 used cursor: 0x1300d30

[initandlisten] Sun Jul 25 00:41:22 query local.system.namespaces reslen:36 nreturned:0 0ms

[initandlisten] Sun Jul 25 00:41:22 enter repairDatabases

[initandlisten] Sun Jul 25 00:41:22 done repairDatabases

[initandlisten] Sun Jul 25 00:41:22 waiting for connections on port 27017

[websvr] Sun Jul 25 00:41:22 web admin interface listening on port 28017

[initandlisten] Sun Jul 25 00:41:30 connection accepted from 127.0.0.1:46032 #1

[conn1] Sun Jul 25 00:41:30 query: admin.$cmd{ whatsmyuri: 1 }

[conn1] Sun Jul 25 00:41:30 run command admin.$cmd { whatsmyuri: 1 }

[conn1] Sun Jul 25 00:41:30 query admin.$cmd ntoreturn:1 command: { whatsmyuri: 1 } reslen:71 0ms

[DataFileSync] Sun Jul 25 00:42:22 flushing mmap took 0ms

[DataFileSync] Sun Jul 25 00:43:22 flushing mmap took 0ms

[DataFileSync] Sun Jul 25 00:44:22 flushing mmap took 0ms

^C

Sun Jul 25 00:45:22 got kill or ctrl c signal 2 (Interrupt), will terminate after current cmd ends

[interruptThread] Sun Jul 25 00:45:22 now exiting

Sun Jul 25 00:45:22 dbexit:

[interruptThread] Sun Jul 25 00:45:22 shutdown: going to close listening sockets...

[interruptThread] Sun Jul 25 00:45:22 going to close listening socket: 5

[interruptThread] Sun Jul 25 00:45:22 going to close listening socket: 6

[interruptThread] Sun Jul 25 00:45:22 going to close listening socket: 7

[interruptThread] Sun Jul 25 00:45:22 going to close listening socket: 8

[interruptThread] Sun Jul 25 00:45:22 shutdown: going to flush oplog...

[interruptThread] Sun Jul 25 00:45:22 shutdown: going to close sockets...

[interruptThread] Sun Jul 25 00:45:22 shutdown: waiting for fs preallocator...

[interruptThread] Sun Jul 25 00:45:22 shutdown: closing all files...

Sun Jul 25 00:45:22 closeAllFiles() finished

[interruptThread] Sun Jul 25 00:45:22 shutdown: removing fs lock...

Sun Jul 25 00:45:22 dbexit: really exiting now

4. configuration file

如果嫌命令行参数太长,可以考虑使用配置文件。

$ cat test.conf

dbpath = /var/mongodb

logpath = /var/test.log

logappend = true

fork = true

port = 1234

$ sudo ./mongod --config test.conf

forked process: 2262

all output going to: /var/test.log

5. db.serverStatus

在 mongo 中执行 admin.serverStatus() 命令可以获取 MongoDB 的运行统计信息。

$ ./mongo

MongoDB shell version: 1.5.4

connecting to: test

> use admin

switched to db admin

> db.serverStatus()

{

"version" : "1.5.4",

"uptime" : 23,

"uptimeEstimate" : 0,

"localTime" : "Sun Jul 25 2010 01:07:18 GMT+0800 (CST)",

"globalLock" : {

"totalTime" : 23074558,

"lockTime" : 470,

"ratio" : 0.000020368754192387997

},

"mem" : {

"bits" : 64,

"resident" : 2,

"virtual" : 75,

"supported" : true,

"mapped" : 0

},

"connections" : {

"current" : 1,

"available" : 19999

},

"extra_info" : {

"note" : "fields vary by platform",

"heap_usage_bytes" : 162672,

"page_faults" : 0

},

"indexCounters" : {

"btree" : {

"accesses" : 0,

"hits" : 0,

"misses" : 0,

"resets" : 0,

"missRatio" : 0

}

},

"backgroundFlushing" : {

"flushes" : 0,

"total_ms" : 0,

"average_ms" : 0,

"last_ms" : 0,

"last_finished" : "Thu Jan 01 1970 08:00:00 GMT+0800 (CST)"

},

"opcounters" : {

"insert" : 0,

"query" : 1,

"update" : 0,

"delete" : 0,

"getmore" : 0,

"command" : 2

},

"asserts" : {

"regular" : 0,

"warning" : 0,

"msg" : 0,

"user" : 0,

"rollovers" : 0

},

"ok" : true

}

相关字段说明:

  1. •uptime: 服务器运行时间(秒)。
  2. •localTime: 服务器本地时间。
  3. •mem: 服务器内存信息。
  4. •connections: 当前连接数。
  5. •opcounters: 操作统计。

6. db.stats

db.stats 查看数据库状态信息。

> db.stats()

{

"collections" : 5,

"objects" : 18,

"avgObjSize" : 50.888888888888886,

"dataSize" : 916,

"storageSize" : 26112,

"numExtents" : 5,

"indexes" : 5,

"indexSize" : 40960,

"fileSize" : 201326592,

"ok" : 1

}

7. http console

Mongod 默认会打开一个 HTTP 监听端口,通过浏览器我们能获取 MongoDB 服务器的相关状态信息。如果不想启动 HTTP Listening,可以使用 --nohttpinterface 参数。

$ sudo ./mongod --dbpath /var/mongodb --nohttpinterface

Sun Jul 25 01:46:53 MongoDB starting : pid=2406 port=27017 dbpath=/var/mongodb 64-bit

** NOTE: This is a development version (1.5.4) of MongoDB.

** Not recommended for production.

Sun Jul 25 01:46:53 db version v1.5.4, pdfile version 4.5

Sun Jul 25 01:46:53 git version: 6c1361df41d9cabf9026364427a7df44b3c304fd

Sun Jul 25 01:46:53 sys info: Linux domU-12-31-39-06-79-A1 2.6.21.7-2.ec2.v1.2.fc8xen #1 ...

[initandlisten] Sun Jul 25 01:46:53 waiting for connections on port 27017

^C

Sun Jul 25 01:47:19 got kill or ctrl c signal 2 (Interrupt), will terminate after current cmd ends

[interruptThread] Sun Jul 25 01:47:19 now exiting

Sun Jul 25 01:47:19 dbexit:

[interruptThread] Sun Jul 25 01:47:19 shutdown: going to close listening sockets...

[interruptThread] Sun Jul 25 01:47:19 going to close listening socket: 5

[interruptThread] Sun Jul 25 01:47:19 going to close listening socket: 6

[interruptThread] Sun Jul 25 01:47:19 shutdown: going to flush oplog...

[interruptThread] Sun Jul 25 01:47:19 shutdown: going to close sockets...

[interruptThread] Sun Jul 25 01:47:19 shutdown: waiting for fs preallocator...

[interruptThread] Sun Jul 25 01:47:19 shutdown: closing all files...

Sun Jul 25 01:47:19 closeAllFiles() finished

[interruptThread] Sun Jul 25 01:47:19 shutdown: removing fs lock...

Sun Jul 25 01:47:19 dbexit: really exiting now

除了用浏览器查看状态信息外,还可以使用 --rest 参数打开 RESTful 操作。

$ sudo ./mongod --dbpath /var/mongodb --rest --fork --logpath /dev/null

forked process: 2451

all output going to: /dev/null

$ curl

{ "version" : "1.5.4",

"uptime" : 13,

"uptimeEstimate" : 0,

"localTime" : Date( "Thu May 24 14:12:39 4253" ),

"globalLock" : { "totalTime" : 13207178,

"lockTime" : 400,

"ratio" : 3.028656083835623e-05 },

"mem" : { "bits" : 64,

"resident" : 2,

"virtual" : 67,

"supported" : true,

"mapped" : 0 },

"connections" : { "current" : 0,

"available" : 20000 },

"extra_info" : { "note" : "fields vary by platform",

"heap_usage_bytes" : 158000,

"page_faults" : 0 },

"indexCounters" : { "btree" : { "accesses" : 0,

"hits" : 0,

"misses" : 0,

"resets" : 0,

"missRatio" : 0 } },

"backgroundFlushing" : { "flushes" : 0,

"total_ms" : 0,

"average_ms" : 0,

"last_ms" : 0,

"last_finished" : Date( 0 ) },

"opcounters" : { "insert" : 0,

"query" : 1,

"update" : 0,

"delete" : 0,

"getmore" : 0,

"command" : 0 },

"asserts" : { "regular" : 0,

"warning" : 0,

"msg" : 0,

"user" : 0,

"rollovers" : 0 } }

$ curl http://localhost:28017/blog/users/

{

"offset" : 0,

"rows": [

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e53" }, "name" : "user0" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e54" }, "name" : "user1" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e55" }, "name" : "user2" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e56" }, "name" : "user3" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e57" }, "name" : "user4" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e58" }, "name" : "user5" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e59" }, "name" : "user6" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e5a" }, "name" : "user7" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e5b" }, "name" : "user8" } ,

{ "_id" : { "$oid" : "4c4b2998a09020a0e7681e5c" }, "name" : "user9" }

],

"total_rows" : 10 ,

"query" : {} ,

"millis" : 0

}

8. 复制数据库

 db.copyDatabase("blog", "blog", "192.168.1.202") // 从源服务器复制 blog 数据库


自:

阅读(1846) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

啦哆A梦2012-03-22 22:57:52

Mongod 是 MongoDB 核心程序,通常情况下我们只需折腾该程序即可