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。
默认情况下,所有的 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 参数。
想要停止服务,可以发送 INT 或 TERM 信号。
或使用 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