您的位置:首页 > 数据库 > Mongodb

安装MongoDB配置及用户授权

2014-01-11 03:56 344 查看
注:win7要用以管理员身份运行命令窗口

1、下载

下载地址:http://www.mongodb.org/downloads

2、安装

安装非常简单,解压就行了,我解压后,放在C:/MongoDB目录下。

为了命令行的方便,可以把C:/MongoDB/bin加到系统环境变量的path中了。

3、运行并指定数据库存放目录(不要关闭此窗口)

C:\Windows\system32>mongod --dbpath=D:\Data\mongodb --directoryperdb
Sat Jan 11 02:50:26.483 [initandlisten] MongoDB starting : pid=13708 port=27017
dbpath=D:\Data\mongodb 64-bit host=admin-dgh
Sat Jan 11 02:50:26.484 [initandlisten] db version v2.4.8
Sat Jan 11 02:50:26.484 [initandlisten] git version: a350fc38922fbda2cec8d5dd842
237b904eafc14
Sat Jan 11 02:50:26.484 [initandlisten] build info: windows sys.getwindowsversio
n(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST
_LIB_VERSION=1_49
Sat Jan 11 02:50:26.484 [initandlisten] allocator: system
Sat Jan 11 02:50:26.484 [initandlisten] options: { dbpath: "D:\Data\mongodb", di
rectoryperdb: true }
Sat Jan 11 02:50:26.565 [initandlisten] journal dir=D:\Data\mongodb\journal
Sat Jan 11 02:50:26.566 [initandlisten] recover : no journal files present, no r
ecovery needed
Sat Jan 11 02:50:26.589 [FileAllocator] allocating new datafile D:\Data\mongodb\
local\local.ns, filling with zeroes...
Sat Jan 11 02:50:26.590 [FileAllocator] creating directory D:\Data\mongodb\local
\_tmp
Sat Jan 11 02:50:26.643 [FileAllocator] done allocating datafile D:\Data\mongodb
\local\local.ns, size: 16MB,  took 0.052 secs
Sat Jan 11 02:50:26.643 [FileAllocator] allocating new datafile D:\Data\mongodb\
local\local.0, filling with zeroes...
Sat Jan 11 02:50:26.780 [FileAllocator] done allocating datafile D:\Data\mongodb
\local\local.0, size: 64MB,  took 0.135 secs
Sat Jan 11 02:50:26.781 [initandlisten] command local.$cmd command: { create: "s
tartup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37
192ms
Sat Jan 11 02:50:26.783 [initandlisten] waiting for connections on port 27017
Sat Jan 11 02:50:26.884 [websvr] admin web console waiting for connections on po
rt 28017
注:--dbpath:数据文件存放路径,每个数据库会在其中创建一个子目录,用于防止同一个实例多次运行的mongod.lock也保存在此目录中。

--directoryperdb:每个db存放在单独的目录中,建议设置该参数。与MySQL的独立表空间类似。

MongoDB服务端的默认监听端口是:27017

4、增加访问权限(另外启动一个命令窗口)

在运行内输入CMD 进入命令窗口,在命令窗口中输入mongo 进入数据库

C:\Windows\system32>mongo
MongoDB shell version: 2.4.8 当前版本号
connecting to: test 连接的数据库名称
提示当前MongoDB 数据库的版本号和当前连接的数据库名称

输入sue admin命令

C:\Windows\system32>mongo
MongoDB shell version: 2.4.8
connecting to: test
> show collections
>
> use admin 回车
switched to db admin
则切换到admin数据库,这个表就等同于MsSql中的用户表,用来存放超级管理员的

创建一个管理员帐号

C:\Windows\system32>mongo
MongoDB shell version: 2.4.8
connecting to: test
> use admin
switched to db admin
> db.addUser('sa','sa'); 回车
{
"user" : "manager",
"readOnly" : false,
"pwd" : "3d3d8fe8664519ee240bc55965178bbe",
"_id" : ObjectId("52d041541e5129728a603c8a")
}
运用db.addUser('sa','sa')方法添加一个用户,其中第一个参数为用户名,第二个参数为用户密码。

这样我们就创建了一个系统超级管理员。那么我们先停止之前的窗口服务(即:运行并指定数据库存放目录)时运行的服务。

现在admin数据库中已经有用户信息了,我们关掉mongodb, 重新启动,这次带有--auth 参数,再访问数据库又是什么情况哪??

C:\Windows\system32>mongod --dbpath=D:\Data\mongodb --auth
在另外一个命令窗口下连接服务器。

C:\Windows\system32>mongo 回车
MongoDB shell version: 2.4.8
connecting to: test
> show collections 回车
Sat Jan 11 03:04:17.341 error: {
"$err" : "not authorized for query on test.system.namespaces",
"code" : 16550
} at src/mongo/shell/query.js:128
> ^C
bye


连接服务器后,我们查看一下test数据库下所有集合。那么就会报没有权限查询。

那么我们如何授权操作test数据库内容哪??

首先我们要切换到admin数据库,进行授权操作。

C:\Windows\system32>mongo 回车
MongoDB shell version: 2.4.8
connecting to: test
> show collections 回车
Sat Jan 11 03:06:21.505 error: {
"$err" : "not authorized for query on test.system.namespaces",
"code" : 16550
} at src/mongo/shell/query.js:128
> use admin 回车
switched to db admin
> db.auth('sa','sa') 回车
1
> use test 回车 再切换到test数据库
switched to db test
> show collections 回车 查看所有集合
> ^C
bye
那么这样就可以对其进行操作了。

5、将MongoDB作为Windows服务随机后台启动

先创建d:\mongodblog\mongodb.log日志文件(手动创建),用于存储MongoDB的日志文件,再安装系统服务。

C:\Windows\system32>mongod --dbpath=D:\Data\mongodb --logpath=D:\mongodblog\mong
odb.log --auth --directoryperdb --serviceName "MongoDB" --install 回车
Sat Jan 11 02:56:11.947 Trying to install Windows service 'MongoDB'
Sat Jan 11 02:56:12.471 Service 'MongoDB' (Mongo DB) installed with command line
'C:\mongo\bin\mongod.exe --dbpath=D:\Data\mongodb --logpath=D:\mongodblog\mongo
db.log --auth --service'
Sat Jan 11 02:56:12.471 Service can be started from the command line with 'net s
tart MongoDB'


启动服务:
C:\Windows\system32>net start MongoDB

Mongo DB 服务已经启动成功。
卸载服务:

mongod.exe --remove --serviceName "MongoDB"

通过以上操作MongoDB服务器就配置好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: