关闭

Mongdb 3.2 复制集配置 官方默认配置 启用认证 Windows环境

366人阅读 评论(0) 收藏 举报

步骤:

1、更新hosts文件

路径:C:\Windows\System32\drivers\etc

在hosts文件里添加:

xxx.xxx.xxx.xxx    mongodb0.example.net
xxx.xxx.xxx.xxx    mongodb1.example.net
xxx.xxx.xxx.xxx    mongodb2.example.net


2、添加文件夹

路径:D:\MongoDB\data

添加3个文件夹db26001,db26002,db26003


3、生成keyfile文件

linux环境:
openssl rand -base64 756 > aaakeyfile1.dat
chmod 400 aaakeyfile1.dat

//文件需要转换格式,从Unix到Windows格式


4、准备config文件

路径:D:\MongoDB\Server\3.2\bin

文件名:mongodb26001.conf

内容:

port=26001
logpath=D:\MongoDB\data\db26001\mongod26001.log
logappend=true
pidfilepath=D:\MongoDB\data\db26001\26001.pid  
dbpath=D:\MongoDB\data\db26001
#bind_ip=mongodb0.example.net
maxConns=500
replSet=rs0
#auth=true
#keyFile=D:\MongoDB\Server\3.2\bin\aaakeyfile1.dat

#fork=true
#cpu=true
#noauth=true

//相应配置mongodb26002.conf,mongodb26003.conf


5、启动mongod

mongod -f D:\MongoDB\Server\3.2\bin\mongodb26001.conf


6、进入实例1

mongo mongodb0.example.net:26001

运行语句:

 rs.initiate()

返回结果:

运行语句:

rs.conf()

返回结果:



7、启动实例2

mongod -f D:\MongoDB\Server\3.2\bin\mongodb26002.conf

在实例1主窗口运行:

运行语句:

rs.add("mongodb1.example.net:26002")


启动实例3:

mongod -f D:\MongoDB\Server\3.2\bin\mongodb26003.conf

在实例1主窗口运行:

运行语句:

rs.add("mongodb2.example.net:26003")


8、验证:

rs.status()


9、验证复制集是否连通

进入实例3(从节点):

mongo mongodb2.example.net:26003

运行:

 db.testcol123.insert({a1:111})

返回:

WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })

//从节点不能写入


--添加数据,测试是否传到从节点

进入实例1:

mongo mongodb0.example.net:26001

运行:

 db.testcol123.insert({a1:111})

返回:

WriteResult({ "nInserted" : 1 })


查看实例2,实例3

运行语句:

 rs.slaveOk()


 db.testcol123.find()

返回:{ "_id" : ObjectId("586b1f612b93726791eff4ca"), "a1" : 111 }

//已经传入从节点



10、添加用户

进入实例1:

mongo mongodb0.example.net:26001


use admin
db.createUser(
  {
    user: "myRootAdmin",
    pwd: "xxx",
    roles: [ { role: "root", db: "admin" } ]
  }
)



use test
db.createUser(
  {
    user: "myTester",
    pwd: "xxx",

    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)


11、停止所有节点,修改config文件,取消以下注释,重启所有节点

#auth=true
#keyFile=D:\MongoDB\Server\3.2\bin\aaakeyfile1.dat


12、用java客户端验证


package com.tesco.mongodb.test;



import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


import org.bson.Document;
//import org.json.JSONObject;



import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoClientURI;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;

import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;


public class Test4 {
 
	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
			run1();	
		}
		
	}
	
	
	private static void run1(){
		Logger log = Logger.getLogger("org.mongodb.driver");
		log.setLevel(Level.OFF);
		MongoClient mongoClient  = null;
		MongoCursor<Document> mongoCursor = null;
		try {
			Long l1 = System.currentTimeMillis();
		 
			MongoClientURI connectionString = new MongoClientURI("mongodb://myTester:xxx@" +
 
					"mongodb0.example.net:26001," +
					"mongodb1.example.net:26002," +
					"mongodb2.example.net:26003/test?replicaSet=rs0&maxPoolSize=400");//&readPreference=secondary
 
			
			mongoClient = new MongoClient(connectionString);

			MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
 
			MongoCollection<Document> collection = mongoDatabase.getCollection("testcol123");
 
			FindIterable<Document> findIterable = collection.find();
			mongoCursor = findIterable.iterator();
 
			while (mongoCursor.hasNext()) {
 
				System.out.println(mongoCursor.next());
 
				
			}
			
		 
			
			
			Long l2 = System.currentTimeMillis();
			System.out.println(l2-l1);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(null != mongoCursor)
				mongoCursor.close();
			if(null != mongoClient){
				mongoClient.close();

			}
		}

	}

}



返回:

Document{{_id=586b1f612b93726791eff4ca, a1=111.0}}
Document{{_id=586b1f612b93726791eff4cb, a1=111.0}}
Document{{_id=586b1f622b93726791eff4cc, a1=111.0}}
2125
Document{{_id=586b1f612b93726791eff4ca, a1=111.0}}
Document{{_id=586b1f612b93726791eff4cb, a1=111.0}}
Document{{_id=586b1f622b93726791eff4cc, a1=111.0}}
782
Document{{_id=586b1f612b93726791eff4ca, a1=111.0}}
Document{{_id=586b1f612b93726791eff4cb, a1=111.0}}
Document{{_id=586b1f622b93726791eff4cc, a1=111.0}}
718








0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7576次
    • 积分:142
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:6篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论