欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

windows mongodb 集群

程序员文章站 2022-06-13 17:43:27
...

在一台Windows机器下搭建了一个 Replica Sets + Sharding 测试集群环境,以此作为我后续对于Mongodb更进一步学习的实验平台。

只有一台windows机器,配置方案:
1、3个分片sharding
2、每一个分片由3个节点构成1主2备的Replica Sets
3、3个配置节点Configsever
4、1个路由节点Mongos

分片复制集A(三个分片节点构成一个复制集):

127.0.0.1:10000   127.0.0.1:10001  127.0.0.1:10002

分片复制集B(三个分片节点构成一个复制集):

127.0.0.1:20000   127.0.0.1:20001  127.0.0.1:20002 

分片复制集C(三个分片节点构成一个复制集):

127.0.0.1:30000   127.0.0.1:30001  127.0.0.1:30002

Configsvc(三个配置服务器节点):

127.0.0.1:40000   127.0.0.1:40001  127.0.0.1:4002

mongos(一个路由节点):

127.0.0.1:50000

 

详细操作步骤如下:

(1)下载mongodb-win32-i386-1.8.0

下载mongodb-win32-i386-1.8.0版本的Mongodb,放置在d:/mongodb-win32-i386-1.8.0目录下。

(2)创建数据和日志文件目录

进入d:/mongodb-win32-i386-1.8.0目录下创建如下目录

创建数据文件目录:

data/a/r0
data/a/r1
data/a/r2
data/b/r0
data/b/r1
data/b/r2
data/c/r0
data/c/r1
data/c/r2
data/configsvr/r0
data/configsvr/r1
data/configsvr/r2

创建日志文件目录:

logs/a
logs/b
logs/c
logs/configsvr

(3)创建分片和复制集

配置第一组:

从命令行进入d:/mongodb-win32-i386-1.8.0/bin目录,分别执行如下命令!

mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/a/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/a/r0 --port 10000 --shardsvr --replSet setA --rest  --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/a/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/a/r1 --port 10001 --shardsvr --replSet setA --rest  --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/a/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/a/r2 --port 10002 --shardsvr --replSet setA --rest  --oplogSize 64

启动上述分片节点之后,再使用mongo的命令行来初始化复制集

D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 127.0.0.1:10000/admin
MongoDB shell version: 1.8.0
connecting to: 127.0.0.1:10000/admin
> config={_id: 'setA', members:[{_id: 0,host:'127.0.0.1:10000'},{_id:1,host:'127.0.0.1:10001'},{_id:2,host:'
127.0.0.1:10002'}]}
{
        "_id" : "setA",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "127.0.0.1:10000"
                },
                {
                        "_id" : 1,
                        "host" : "127.0.0.1:10001"
                },
                {
                        "_id" : 2,
                        "host" : "127.0.0.1:10002"
                }
        ]
}
setA> rs.initiate(config);

配置第二组:

从命令行进入d:/mongodb-win32-i386-1.8.0/bin目录,分别执行如下命令!

mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/b/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/b/r0 --port 20000 --shardsvr --replSet setB --rest  --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/b/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/b/r1 --port 20001 --shardsvr --replSet setB --rest  --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/b/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/b/r2 --port 20002 --shardsvr --replSet setB --rest  --oplogSize 64

启动上述分片节点之后,再使用mongo的命令行来初始化复制集

D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 127.0.0.1:20000/admin
MongoDB shell version: 1.8.0
connecting to: 127.0.0.1:20000/admin
> config={_id: 'setB', members:[{_id: 0,host:'127.0.0.1:20000'},{_id:1,host:'127.0.0.1:20001'},{_id:2,host:'
127.0.0.1:20002'}]}
{
        "_id" : "setB",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "127.0.0.1:20000"
                },
                {
                        "_id" : 1,
                        "host" : "127.0.0.1:20001"
                },
                {
                        "_id" : 2,
                        "host" : "127.0.0.1:20002"
                }
        ]
}
> rs.initiate(config);

配置第三组:

从命令行进入d:/mongodb-win32-i386-1.8.0/bin目录,分别执行如下命令!
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/c/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/c/r0 --port 30000 --shardsvr --replSet setC --rest  --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/c/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/c/r1 --port 30001 --shardsvr --replSet setC --rest  --oplogSize 64
mongod.exe --logpath d:/mongodb-win32-i386-1.8.0/logs/c/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/c/r2 --port 30002 --shardsvr --replSet setC --rest  --oplogSize 64
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 127.0.0.1:30000/admin
MongoDB shell version: 1.8.0
connecting to: 127.0.0.1:30000/admin
> config={_id: 'setC', members:[{_id: 0,host:'127.0.0.1:30000'},{_id:1,host:'127.0.0.1:30001'},{_id:2,host:'
127.0.0.1:30002'}]}
{
        "_id" : "setC",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "127.0.0.1:30000"
                },
                {
                        "_id" : 1,
                        "host" : "127.0.0.1:30001"
                },
                {
                        "_id" : 2,
                        "host" : "127.0.0.1:30002"
                }
        ]
}
> rs.initiate(config);

(4)启动三个配置服务节点Configsvr

从命令行分别执行如下命令,配置三个Configsvr

cd d:/mongodb-win32-i386-1.8.0/bin
call mongod.exe --configsvr --logpath d:/mongodb-win32-i386-1.8.0/logs/configsvr/r0.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/configsvr/r0 --port 40000 --shardsvr

cd d:/mongodb-win32-i386-1.8.0/bin
call mongod.exe --configsvr --logpath d:/mongodb-win32-i386-1.8.0/logs/configsvr/r1.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/configsvr/r1 --port 40001 --shardsvr

cd d:/mongodb-win32-i386-1.8.0/bin
call mongod.exe --configsvr --logpath d:/mongodb-win32-i386-1.8.0/logs/configsvr/r2.log --logappend --dbpath d:/mongodb-win32-i386-1.8.0/data/configsvr/r2 --port 40002 --shardsvr

(5)启动一个路由节点mongos

cd d:/mongodb-win32-i386-1.8.0/bin
call mongos.exe --configdb 127.0.0.1:40000,127.0.0.1:40001,127.0.0.1:40002 --logpath d:/mongodb-win32-i386-1.8.0/logs/mongos.log --logappend --port 50000

(6)配置分片

D:/mongodb-win32-i386-1.8.0/cmd>cd d:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 127.0.0.1:50000
MongoDB shell version: 1.8.0
connecting to: 127.0.0.1:50000/test
> use admin
switched to db admin
> db.runCommand({addshard:"setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1:10002",name:"ShardSetA"})
{ "shardAdded" : "ShardSetA", "ok" : 1 }
> db.runCommand({addshard:"setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002",name:"ShardSetB"})
{ "shardAdded" : "ShardSetB", "ok" : 1 }
> db.runCommand({addshard:"setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002",name:"ShardSetC"})
{ "shardAdded" : "ShardSetC", "ok" : 1 }
> printShardingStatus()
--- Sharding Status ---
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      {
        "_id" : "ShardSetA",
        "host" : "setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1:10002"
}
      {
        "_id" : "ShardSetB",
        "host" : "setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002"
}
      {
        "_id" : "ShardSetC",
        "host" : "setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002"
}
  databases:
        { "_id" : "admin", "partitioned" : false, "primary" : "config" }

配置到这里,集群搭建完毕了!

在完成了集群的搭建工作之后,需要做的就是建立一个数据库,建立表,设置分片主键来初始化数据了!

 

Mongodb集群JavaAPI插入数据 .


在完成了集群的搭建工作之后,需要做的就是建立一个数据库,建立表,设置分片主键来初始化数据了!

 

> call mongo.exe 127.0.0.1:50000


> use wlb

>db.createCollection('wlb_orders')

> use admin


>db.runCommand({enablesharding:'wlb'})

>db.runCommand({shardcollection:'wlb.wlb_orders',key:{order_id:1}})  //设置表的分区主键为

> db.printShardingStatus() //查询数据库分片信息

 

(2)用Java代码完成数据初始化

package com.zhangzk.mongodb;

import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongodbTest {
 
 /**
  * 30W 288391
  *
  * @param args
  */
 public static void main(String[] args) {
  initData();
  // query();
 }
 
 private static void initData() {
  long start = new Date().getTime();
  try {
   Mongo db = new Mongo("127.0.0.1", 50000);
   DB mydb = db.getDB("wlb");
   DBCollection coll = mydb.getCollection("wlb_orders");
   for (long i = 1; i <= 300000; i++) {
    BasicDBObject doc = new BasicDBObject();
    doc.put("order_id", i);
    doc.put("company_id", 505 + i);
    doc.put("user_id", 180225429 + i);
    doc.put("fetcher_id", 59803 + i);
    doc.put("fetch_schedule_begin", new Date());
    doc.put("fetch_schedule_end", new Date());
    doc.put("sender_id", 59803 + i);
    doc.put("mail_no", "000000");
    doc.put("mail_type", "301");
    doc.put("order_code", "LP10012700003959" + i);
    doc.put("order_status", 30);
    doc.put("prev_order_id", 0);
    doc.put("trade_id", 2010012706189794L + i);
    doc.put("goods_remark", "");
    doc.put("receiver_name", " 凯撒");
    doc.put("receiver_wangwang_id", "sanglin01");
    doc.put("receiver_mobile_phone", "13021525841");
    doc.put("receiver_zip_code", "650045");
    doc.put("receiver_telephone", "13868117135");
    doc.put("receiver_county_id", 350102);
    doc.put("receiver_address", "福建省^^^福州市^^^鼓楼区^^^的萨芬萨芬萨芬的12号");
    doc.put("gmt_create", new Date());
    doc.put("gmt_modified", new Date());
    doc.put("status_reason", "");
    doc.put("logis_type", 0);
    doc.put("seller_wangwang_id", "tbtest943" + i);
    doc.put("seller_send_confirm", 0);
    doc.put("shipping", 2);
    doc.put("company_code", "");
    doc.put("taobao_trade_id", "2232358300" + i);
    doc.put("options", 2);
    doc.put("shipping2", 0);
    doc.put("order_source", 0);
    doc.put("status_date", new Date());
    doc.put("timeout_status", 2);
    doc.put("feature", "ip=127.0.0.1;SFID=");
    doc.put("service_fee", 0);
    doc.put("seller_store_id", "23100");
    doc.put("items_value", 23100);
    doc.put("pre_status", 0);
    doc.put("ticket_id", "");
    doc.put("tfs_url", "T1DoBbXctCXXXXXXXX");
    coll.insert(doc);
   }
   db.close();
   long endTime = new Date().getTime();
   System.out.println(endTime - start);
   System.out.println((endTime - start) / 10000000);
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (MongoException e) {
   e.printStackTrace();
  }
 }