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}