mongodb集群配置

1 Mongo使用sharding集群

mongodb副本集群

mongos1

192.168.20.137

mongos2

192.168.20.138

mongo_config1

192.168.20.139

mongo_config2

192.168.20.129

mongo_config3

192.168.20.128

mongod_shard1_replicaset1

192.168.20.127

mongod_shard1_replicaset2

192.168.20.126

mongod_shard1_replicaset3

192.168.20.125

mongod_shard2_replicaset1

192.168.20.123

mongod_shard2_replicaset2

192.168.20.122

mongod_shard2_replicaset3

192.168.20.121

2下载mongodb 源码包

[root@ ~]#  wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.9.tgz

3解压缩,并拷贝到/data目录下

tar -zxvf mongodb-linux-x86_64-rhel70-3.2.9.tgz

mv mongodb-linux-x86_64-rhel70-3.2.9    /data/mongodb

4新建mongodb目录

mkdir -p /data/mongodb/etc/
mkdir -p /data/mongodb/var/log/

mkdir
/data/mongodb/run

mkdir
/data/mongodb/data

mkdir
/data/mongodb/logs

groupadd mongodb

useradd -g
mongodb -s /sbin/nologin -M mongodb

chown -R mongodb.mongodb
/data/mongodb

5 mongod配置文件文件:

vim  /data/mongodb/etc/mongodb.cnf

systemLog:

verbosity: 0

quiet: false

syslogFacility: user

path:
"/data/mongodb/logs/mongod.log"

logAppend: true

logRotate: rename

destination: file

timeStampFormat: iso8601-utc

storage:

dbPath: "/data/mongodb/data"

indexBuildRetry: true

journal:

enabled: true

directoryPerDB: true

syncPeriodSecs: 60

engine: wiredTiger

wiredTiger:

engineConfig:

cacheSizeGB: 1

statisticsLogDelaySecs: 0

journalCompressor: snappy

directoryForIndexes: true

collectionConfig:

blockCompressor: snappy

indexConfig:

prefixCompression: true

processManagement:

fork: true

pidFilePath:
"/data/mongodb/run/mongod.pid"

net:

bindIp: 0.0.0.0

port: 27017

http:

enabled: true

RESTInterfaceEnabled: true

operationProfiling:

slowOpThresholdMs: 100

mode: "slowOp"

security:

authorization: "disabled"

replication:

oplogSizeMB: 1024

replSetName: " mongod_shard1_im_test"   ###同一个sharding相同,不同的sharding relSetName 不通

5.
设置开机启动

[root@ ~ ]# vim /etc/rc.local

[root@ ~]# /data/mongodb/bin/mongod -f
/data/mongodb/etc/mongodb.cnf --rest

6启动mongodb

/data/mongodb/bin/mongod -f
/data/mongodb/etc/mongodb.cnf –rest

7.配置副本集群

[root@  etc]#  /data/mongodb/bin/mongo  #登陆到mongodb中

查看副本集状态

> rs.status()

{

"info"
: "run rs.initiate(...) if not yet done for the set",

"ok"
: 0,

"errmsg"
: "no replset config has been received",

"code"
: 94

}

进入到admin 数据库

>use admin

进行cfg配置
,最外层的_id表示replica set的名字,members里包含的是所有节点的地址以及优先级。优先级最高的即成为主节点

>
cfg={_id:"mongod_shard1_im_test",members:[{_id:0,host:'192.168.20.127:27017',priority:10},
{_id:1,host:'192.168.20.126:27017',priority:5}, {_id:2,host:'192.168.20.125:27017',priority:1}]}

进行初始化

>rs.initiate(cfg)

{ "ok" : 1 }

显示了每个节点的健康状况,名称,启动的时间,节点的类型等。查看当前副本集的配置表:

{

"_id"
: "mongod_shard1_im_test",

"version"
: 1,

"protocolVersion"
: NumberLong(1),

"members"
: [

{

"_id"
: 0,

"host"
: "192.168.20.127:27017",

"arbiterOnly"
: false,

"buildIndexes"
: true,

"hidden"
: false,

"priority"
: 10,

"tags"
: {

},

"slaveDelay"
: NumberLong(0),

"votes"
: 1

},

{

"_id"
: 1,

"host"
: "192.168.20.126:27017",

"arbiterOnly"
: false,

"buildIndexes"
: true,

"hidden"
: false,

"priority"
: 5,

"tags"
: {

},

"slaveDelay"
: NumberLong(0),

"votes"
: 1

},

{

"_id"
: 2,

"host"
: "192.168.20.125:27017",

"arbiterOnly"
: false,

"buildIndexes" :
true,

"hidden"
: false,

"priority"
: 1,

"tags"
: {

},

"slaveDelay"
: NumberLong(0),

"votes"
: 1

}

],

"settings"
: {

"chainingAllowed"
: true,

"heartbeatIntervalMillis"
: 2000,

"heartbeatTimeoutSecs"
: 10,

"electionTimeoutMillis"
: 10000,

"getLastErrorModes"
: {

},

"getLastErrorDefaults"
: {

"w"
: 1,

"wtimeout"
: 0

},

"replicaSetId"
: ObjectId("57dfb646ed538f91da25889a")

}

}

再次查看副本集状态

mongod_shard1_im_test:PRIMARY>
rs.status()

{

"set"
: "mongod_shard1_im_test",

"date"
: ISODate("2016-09-19T09:58:17.947Z"),

"myState"
: 1,

"term"
: NumberLong(1),

"heartbeatIntervalMillis"
: NumberLong(2000),

"members"
: [

{

"_id"
: 0,

"name"
: "192.168.20.127:27017",

"health"
: 1,

"state"
: 1,

"stateStr"
: "PRIMARY",

"uptime"
: 15779,

"optime"
: {

"ts"
: Timestamp(1474279005, 1),

"t"
: NumberLong(1)

},

"optimeDate"
: ISODate("2016-09-19T09:56:45Z"),

"infoMessage"
: "could not find member to sync from",

"electionTime"
: Timestamp(1474279004, 1),

"electionDate"
: ISODate("2016-09-19T09:56:44Z"),

"configVersion"
: 1,

"self"
: true

},

{

"_id"
: 1,

"name"
: "192.168.20.126:27017",

"health"
: 1,

"state"
: 2,

"stateStr"
: "SECONDARY",

"uptime"
: 103,

"optime"
: {

"ts"
: Timestamp(1474279005, 1),

"t"
: NumberLong(1)

},

"optimeDate"
: ISODate("2016-09-19T09:56:45Z"),

"lastHeartbeat"
: ISODate("2016-09-19T09:58:16.547Z"),

"lastHeartbeatRecv"
: ISODate("2016-09-19T09:58:16.332Z"),

"pingMs"
: NumberLong(0),

"syncingTo"
: "192.168.20.127:27017",

"configVersion"
: 1

},

{

"_id"
: 2,

"name"
: "192.168.20.125:27017",

"health"
: 1,

"state"
: 2,

"stateStr"
: "SECONDARY",

"uptime"
: 103,

"optime"
: {

"ts"
: Timestamp(1474279005, 1),

"t"
: NumberLong(1)

},

"optimeDate"
: ISODate("2016-09-19T09:56:45Z"),

"lastHeartbeat"
: ISODate("2016-09-19T09:58:16.547Z"),

"lastHeartbeatRecv"
: ISODate("2016-09-19T09:58:16.332Z"),

"pingMs"
: NumberLong(0),

"syncingTo"
: "192.168.20.127:27017",

"configVersion"
: 1

}

],

"ok"
: 1

}

8 mongo  config server配置

mongo_config1

192.168.20.139

mongo_config2

192.168.20.129

mongo_config3

192.168.20.128

默认端口 27019

具体配置同上,配置文件中添加
configsvr = true

systemLog:

quiet: false

path: /data/mongodb/logs/mongod.log

logAppend: false

destination: file

processManagement:

fork: true

pidFilePath: /data/mongodb/run/mongod.pid

net:

bindIp: 0.0.0.0

port: 27019

maxIncomingConnections: 65536

wireObjectCheck: true

ipv6: false

storage:

dbPath: /data/mongodb/data

indexBuildRetry: true

journal:

enabled: true

directoryPerDB: false

engine: mmapv1

syncPeriodSecs: 60

mmapv1:

quota:

enforced: false

maxFilesPerDB: 8

smallFiles: true

journal:

commitIntervalMs: 100

operationProfiling:

slowOpThresholdMs: 100

mode: off

sharding:

clusterRole:
configsvr

启动加入到rc.local中

echo “/data/mongodb/bin/mongod -f
/data/mongodb/etc/mongodb.cnf  --rest”
>>/etc/rc.local

9.mongos配置

mongos1

192.168.20.137

mongos2

192.168.20.138

mongodb.cnf

systemLog:

quiet: false

path: /data/mongodb/logs/mongod.log

logAppend: false

destination: file

processManagement:

fork: true

pidFilePath: /data/mongodb/run/mongod.pid

net:

bindIp: 0.0.0.0

port: 27017

maxIncomingConnections: 65536

wireObjectCheck: true

ipv6: false

sharding:

configDB:
192.168.20.139:27019,192.168.20.129:27019,192.168.20.128:27019

启动

/data/mongodb/bin/mongos  -f  /data/mongodb/etc/mongodb.cnf

10.配置sharding
把replica sets2个副本集群,加入到sharding中

连接mongos 服务器进行配置(任意)

[root@shard2
bin]# ./mongo --host 192.168.20.137:27017

mongos>sh.help()

mongos>
sh.help()

sh.addShard( host )                       server:port OR
setname/server:port

sh.enableSharding(dbname)                 enables sharding on the
database dbname

sh.shardCollection(fullName,key,unique)   shards the collection

sh.splitFind(fullName,find)               splits the chunk that find is in
at the median

sh.splitAt(fullName,middle)               splits the chunk that middle is
in at middle

sh.moveChunk(fullName,find,to)            move the chunk where 'find' is to
'to' (name of shard)

sh.setBalancerState( <bool on or
not> )   turns the balancer on or off
true=on, false=off

sh.getBalancerState()                     return true if enabled

sh.isBalancerRunning()                    return true if the balancer
has work in progress on any mongos

sh.disableBalancing(coll)                 disable balancing on one
collection

sh.enableBalancing(coll)                  re-enable balancing on one
collection

sh.addShardTag(shard,tag)                 adds the tag to the shard

sh.removeShardTag(shard,tag)              removes the tag from the shard

sh.addTagRange(fullName,min,max,tag)      tags the specified range of the given
collection

sh.removeTagRange(fullName,min,max,tag)   removes the tagged range of the given
collection

sh.status()                               prints a general overview of
the cluster

注意:
把mongod_shard1_im_test和
mongod_shard2_im_test加入到集群中,副本集中的其他机器自动加入了。添加replica
set作为分片:

mongos>
sh.addShard("mongod_shard1_im_test/192.168.20.127:27017")

{ "shardAdded" :
"mongod_shard1_im_test", "ok" : 1 }

mongos>
sh.addShard("mongod_shard2_im_test/192.168.20.120:27017")

{ "shardAdded" :
"mongod_shard2_im_test", "ok" : 1 }

通过sh.status()查看

mongos> sh.status()

--- Sharding Status ---

sharding version: {

"_id"
: 1,

"minCompatibleVersion"
: 5,

"currentVersion"
: 6,

"clusterId"
: ObjectId("57e0d5c4eb504a0629599e18")

}

shards:

{  "_id" :
"mongod_shard1_im_test", 
"host" :
"mongod_shard1_im_test/192.168.20.125:27017,192.168.20.126:27017,192.168.20.127:27017"
}

{  "_id" : "mongod_shard2_im_test",  "host" :
"mongod_shard2_im_test/192.168.20.120:27017,192.168.20.121:27017,192.168.20.122:27017"
}

active mongoses:

"3.2.9"
: 2

balancer:

Currently
enabled:  yes

Currently
running:  no

Failed
balancer rounds in last 5 attempts:  0

Migration
Results for the last 24 hours:

No
recent migrations

databases:

11 .数据库 启动分区功能,默认,不启动,都回保存到主sharding上

testdb 启动Sharding功能
mongos> sh.enableSharding("testdb")
{ "ok" : 1 }

12.对collections进行顺序分区

一个数据库中有多个collections,未必所有的collection都分区,
要想让collections使用分区功能,必须使用sh.sharCollection 功能简单,明确说明对那个表进行分区,并
指明分区的key

例: 对testdb.sutable(全名),name 和 Age 进行分区

mongos>
sh.shardCollection("testdb.sutable",{age:1,name:1})

建立testdb数据库,并且对sutable表插入数据

use testdb
for(i=1;i<1000;i++) db.sutable.insert({name: "User"+i,age:i,sex:
"boy",book: ["book1","book2"]})

查看分片状态
mongos> use admin
mongos>
db.runCommand({listshards:1})

 

删除一个sharding:

db.runCommand({"removeshard"
:"mongod_shard1_im_test/192.168.20.125:27017,192.168.20.126:27017,192.168.20.127:27017"});

 

补充:对有数据的数据进行分片

use test
for(i=50000;i<60000;i++) db.messages.insert({"packType": i,"fromName":i,"toName": "boy",roomId:i,"creationDate":i,"stanza":i,"deleteFlag":i,"day":i })

需要手动创建索引
db.messages.ensureIndex({"roomId":1,"creationDate":1,"deleteFlag":1})

指出对那个表的key进行分区
sh.shardCollection("test.messages",{roomId:1,creationDate:1,deleteFlag:1})

删除索引
#db.messages.dropIndex({"roomId" : 1})

写入数据
for(i=50000;i<60000;i++) db.messages.insert({"packType": i,"fromName":i,"toName": "boy",roomId:i,"creationDate":i,"stanza":i,"deleteFlag":i,"day":i })

查看
sh.status()

对没有数据的数据库进行分片准备:

sh.enableSharding("im_message_db")
sh.shardCollection("im_message_db.messages",{roomId:1,creationDate:1,deleteFlag:1})

use im_message_db

for(i=1;i<6000;i++) db.messages.insert({"packType": i,"fromName":i,"toName": "boy",roomId:i,"creationDate":i,"stanza":i,"deleteFlag":i,"day":i })
sh.status()

此方法,不用对collection 建立索引。

mongodb sharding配置的更多相关文章

  1. MongoDB Sharding分片配置

    Ps:mongod是mongodb实例,mongos被默认为为mongodb sharding的路由实例. 本文使用的mongodb版本为3.2.9,因此参考网址为:https://docs.mong ...

  2. MongoDB分片配置系列一:

    接这篇博客: http://www.cnblogs.com/xiaoit/p/4479066.html 这里不再说明安装过程. 1:分片简介 分片是一种将海量的数据水平扩展的数据库集群系统,数据分表存 ...

  3. MongoDB Sharding 机制分析

    MongoDB Sharding 机制分析 MongoDB 是一种流行的非关系型数据库.作为一种文档型数据库,除了有无 schema 的灵活的数据结构,支持复杂.丰富的查询功能外,MongoDB 还自 ...

  4. MongoDB Sharding(二) -- 搭建分片集群

    在上一篇文章中,我们基本了解了分片的概念,本文将着手实践,进行分片集群的搭建 首先我们再来了解一下分片集群的架构,分片集群由三部分构成: mongos:查询路由,在客户端程序和分片之间提供接口.本次实 ...

  5. MongoDB Sharding(一) -- 分片的概念

    (一)分片的由来随着系统的业务量越来越大,业务系统往往会出现这样一些特点: 高吞吐量 高并发 超大规模的数据量 高并发的业务可能会耗尽服务器的CPU,高吞吐量.超大规模的数据量也会带来内存.磁盘的压力 ...

  6. MongoDB的配置、启动、关闭

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  7. mongoDB 安装配置

    1. 配置文件: 建立配置文件 :mongodb.conf dbpath=D:\mongoDb\data\db #配置数据库目录路径,预先手动创建db目录 logpath=D:\mongoDb\dat ...

  8. MongoDB启动配置等

    目录: 一.mongoDB 启动配置 二.导出,导入,运行时备份 三.Fsync锁,数据修复 四.用户管理,安全认证 一.启动项 mongod --help C:\Windows\system32&g ...

  9. MongoDB C Driver and APIinstances linux MongoDB安装配置

    <一,linux平台MongoDB安装配置>在这我们使用的Centos6 yum部署的,你想搞编译,自个干!

随机推荐

  1. 洛谷 P1198 [JSOI2008]最大数 Label:线段树

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...

  2. [Codeforces] Round #320 (Div.2)

    1.前言 虽然这次我依旧没有参加正式比赛,但是事后还是看了看题目的...一般不怎么刷Codeforces. A.Raising Bacteria You are a lover of bacteria ...

  3. BZOJ4554: [Tjoi2016&Heoi2016]游戏

    Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看 是否能炸到对手,或者躲开对手的炸弹.在玩游戏的过程中,小H想到了这样一 ...

  4. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  5. C#后台如何获取客户端访问系统型号

    ASP.NET获取客户端.服务器端基础信息 . 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名:P ...

  6. 李洪强漫谈iOS开发[C语言-047]-数列求和

    // //  main.c //  53 - 数列求和 - 李洪强 // //  Created by vic fan on 16/10/15. //  Copyright © 2016年 李洪强. ...

  7. 1055. The World's Richest (25)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  8. android-数据存储之手机内部file存储

    一.基础概要 1.说明: 1>应用程序运行需要一些较大的数据或者图片可保存在手机内部 2>文件类型:任意 3>路径:/data/data/packageName/files/ 4&g ...

  9. ETL

     1.ETL的定义 ETL分别是“Extract”.“ Transform” .“Load”三个单词的首字母缩写也就是“抽取”.“转换”.“装载”,但我们日常往往简称其为数据抽取. ETL是BI/DW ...

  10. 自己编写php框架(一)

    这两天一直在琢磨如何做一个php模板,将前台html页面和php代码分割开来.虽然现在市面上有各种各样的php模板,但还是想自己编写一个体验一下. 之前一直写到了将替换完php代码的含有php代码的h ...