Windows 2008 R2/Windows 7 安装和配置MongoDB 3.0

下载安装文件:

http://www.mongodb.org/downloads

https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.0.1-signed.msi?_ga=1.246860033.448092955.1426173723

运行MongoDB:

1.创建配置文件,mongodb.conf

dbpath=C:\mongodb\data\db
logpath=C:\mongodb\log\mongodb.log
logappend=true
journal=true
quiet=true
auth=false
port=27017

2.运行MongoDB
...\MongoDB\Server\3.0\bin>mongod -f ...\MongoDB\conf\mongodb.conf

 

作为一个服务安装:

...\MongoDB\Server\3.0\bin>mongod.exe -f ...\MongoDB\conf\mongodb.conf --install

配置登录信息

1.添加管理员账户

如果你的系统还没有用户,你在启动数据库时使用mongod的--noauth参数启动,以获取系统的所有权限。

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

为单个数据库建管理员

use records
db.createUser(
  {
    user: "recordsUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdmin", db: "records" } ]
  }
)

2.添加用户到指定的数据库

数据库管理员登陆系统

mongo --port 27017 -u siteUserAdmin -p password --authenticationDatabase admin

给指定数据库添加用户和权限

use reporting
db.createUser(
    {
      user: "reportsUser",
      pwd: "12345678",
      roles: [
         { role: "read", db: "reporting" },
         { role: "read", db: "products" },
         { role: "read", db: "sales" },
         { role: "readWrite", db: "accounts" }
      ]
    }
)

3.修改配置文件mongodb.conf中auth为true

4.通过如下代码测试登陆验证是否生效

  MongoCredential credential = MongoCredential.createCredential(userName, databaseName, pwd);
  MongoClient mongoClient = new MongoClient(new ServerAddress(ip, port), Arrays.asList(credential));
 
  MongoDatabase database = mongoClient.getDatabase(databaseName);
  MongoCollection<Document> coll = database.getCollection(collName);
  FindIterable<Document> list = coll.find();
  Document doc = list.first();

 

posted on 2015-03-12 12:02  Li Kui  阅读(952)  评论(0编辑  收藏  举报