MongoDB入门学习(一):MongoDB的安装和管理(二)

2015-07-24 08:55:59 · 作者: · 浏览: 3
r Progress: 807403520/1073741824 75% 2014-06-05T17:11:43.063+0800 [initandlisten] File Preallocator Progress: 1017118720/1073741824 94% 2014-06-05T17:11:48.020+0800 [initandlisten] preallocating a journal file data/db/journal/prealloc.2 2014-06-05T17:11:51.040+0800 [initandlisten] File Preallocator Progress: 576716800/1073741824 53% 2014-06-05T17:11:54.039+0800 [initandlisten] File Preallocator Progress: 807403520/1073741824 75% 2014-06-05T17:11:57.026+0800 [initandlisten] File Preallocator Progress: 1069547520/1073741824 99% 2014-06-05T17:12:01.985+0800 [FileAllocator] allocating new datafile data/db/local.ns, filling with zeroes... 2014-06-05T17:12:01.985+0800 [FileAllocator] creating directory data/db/_tmp 2014-06-05T17:12:02.285+0800 [FileAllocator] done allocating datafile data/db/local.ns, size: 16MB, took 0.167 secs 2014-06-05T17:12:02.315+0800 [FileAllocator] allocating new datafile data/db/local.0, filling with zeroes... 2014-06-05T17:12:02.413+0800 [FileAllocator] done allocating datafile data/db/local.0, size: 64MB, took 0.097 secs 2014-06-05T17:12:02.434+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" } 2014-06-05T17:12:02.434+0800 [initandlisten] added index to empty collection 2014-06-05T17:12:02.456+0800 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0 reslen:37 494ms 2014-06-05T17:12:02.457+0800 [initandlisten] waiting for connections on port 9352

启动MongoDB服务器成功,监听端口9352,等待客户端来连接。如果指定日志文件,这些日志就会被输出到指定的日志文件。如果设置为守护进程,就会创建一个子进程,当MongoDB服务器启动完成后父进程就会退出。

让MongoDB稳妥的停下来很重要,因为可能有的数据还在缓存没写入磁盘,稳妥停止是先让数据写进磁盘然后再结束MongoDB进程。可以直接用ctrl+c来停止MongoDB服务器,也可以通过客户端来,mongo是一个java script shell,启动它的时候会自动连接MongoDB服务器,所以mongo也是一个MongoDB客户端,它可以运行任何java script程序,也可以操作数据库。下面来看一下mongo停止MongoDB服务器:

[tp0352@server0 bin]$ ./mongo --port 9352
MongoDB shell version: 2.6.1
connecting to: 127.0.0.1:9352/test
> show dbs
admin  (empty)
local  0.078GB
test   (empty)
> use admin
switched to db admin
> db.shutdownServer()
2014-06-05T17:21:59.263+0800 DBClientCursor::init call() failed
server should be down...
2014-06-05T17:21:59.265+0800 trying reconnect to 127.0.0.1:9352 (127.0.0.1) failed
2014-06-05T17:21:59.266+0800 warning: Failed to connect to 127.0.0.1:9352, reason: errno:111 Connection refused
2014-06-05T17:21:59.266+0800 reconnect 127.0.0.1:9352 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:9352 (127.0.0.1), connection attempt failed
> exit
bye

从上面可以看出,我们连接到了本地9352端口test数据库,admin就是一个root数据库,这个数据库是用来管理整个MongoDB的,不是任何人都可以停止MongoDB服务器的,所以要使用admin来执行shutdownServer()来停止。

2).安全和认证

上面通过mongo这个客户端来连接MongoDB服务器,没有任何的安全认证,也就是说任何人都可以连上去,当然可以在一个可信环境中运行它,保证只有可信的机器才能访问它,也可以对单个连接的认证。每个MongoDB的数据库都可以有许多用户,如果开启了安全性检查,则只有数据库认证用户才能执行相关的操作。admin的用户可以对任何数据库进行读写操作,其他数据库的用户只能执行相关权限的操作。

开启安全检查前,先创建几个用户:

> use admin
switched to db admin
> db.addUser("tp", "12345")
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "tp", "roles" : [ "root" ] }
> use test
switched to db test
> db.addUser("test1", "12345", true)