本文档基于MongoDB版本3.6.2

下载地址:

建议使用最新版本

https://www.mongodb.com/download-center#community

安装文件

集群ip及端口设计方案:

服务

192.168.141.201

192.168.141.202

192.168.141.203

Router

Mongos(17017)

Mongos(17017)

 

Config

Config server1(27017)

Config server2(27017)

Config server3(27017)

 

Shard1-主(37017)

Shard2-主(47017)

Shard3-主(57017)

Shard

Shard2-从(47017)

Shard1-从(37017)

Shard1-从(37017)

 

Shard3-从(57017)

Shard3-从(57017)

Shard2-从(47017)

在每台部署MongoDB的机器上面执行如下命名:(创建mongo下三个服务路径)

mkdir -p /home/mongo/{config,router,shard}
mkdir -p /home/mongo/config/{data,logs}
mkdir -p /home/mongo/router/logs
mkdir -p /home/mongo/shard/{data,logs}
mkdir -p /home/mongo/shard/data/{shard1,shard2,shard3}

可以生成一个脚本文件,mongodirs.sh,再拷贝到所有mongo机器上执行:

#!/usr/bin/bash
mkdir -p mongo
mkdir -p mongo/{config,router,shard} 
mkdir -p mongo/config/{data,logs} 
mkdir -p mongo/router/logs 
mkdir -p mongo/shard/{data,logs} 
mkdir -p mongo/shard/data/{shard1,shard2,shard3}

然后在mongo数据库的根目录下执行

./mongodirs.sh

config服务:

注意:config服务至少启动三个节点

vi /home/mongo/config/config.config

dbpath=/home/mongo/config/data 
logpath=/ home /mongo/config/logs/config.log 
bind_ip=0.0.0.0
port=27017 
logappend=true 
fork=true 
quiet=true 
journal=true 
configsvr=true 
replSet=configRS/192.168.141.201:27017,192.168.141.202:27017,192.168.141.203:27017


启动config服务

 mongod --config  /home/mongo/config/config.config

可能的错误如下:

about to fork child process, waiting until server is ready for connections.
forked process: 10632
ERROR: child process failed, exited with error number 14
To see additional information in this output, start without the "--fork" option.

没有权限,sudo启动就可以了

error number 18:

error number 100:端口被占用

exited with error number 48

原因是端口被占用

[****@centosvm config]$ sudo mongod --config config.config 
[sudo] **** 的密码:
about to fork child process, waiting until server is ready for connections.
forked process: 10667
child process started successfully, parent exiting

正确的启动命令行如下:

用mongo shell连接到已经启动的mongoconfig服务 ,初始化config服务

mongo –port 27017

[****@centosvm config]$ mongo -port 27017
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] 
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-01-26T14:38:04.377+0800 I CONTROL [initandlisten] 
2018-01-26T14:55:45.854+0800 E - [main] Error loading history file: FileOpenFailed: Unable to fopen() file /home/****/.dbshell: No such file or directory
MongoDB Enterprise >

按照集群的设计,配置集群的ip和端口列表,并指定哪个是主节点,哪几个是从节点;以下命令在MongoDB Shell命令行下执行:

rs.initiate({_id:"configRS",configsvr:true,members:[{_id:1,host:"192.168.141.201:27017",priority:2},{_id:2,host:"192.168.141.202:27017"},{_id:3,host:"192.168.141.203:27017"}]})

可能的错误的返回结果:

MongoDB Enterprise > rs.initiate({_id:”configRS”,configsvr:true,members:[{_id:1,host:”192.168.126.132:27017”,priority:2},{_id:2,host:”192.168.126.131:27017”},{_id:3,host:”192.168.126.130:27017”}]})
2018-01-26T15:01:17.200+0800 E QUERY [thread1] SyntaxError: illegal character @(shell):1:17

可能的原因是双引号是全角字符

MongoDB Enterprise > rs.initiate({_id:"configRS",configsvr:true,members:[{_id:1,host:"192.168.126.132:27017",priority:2},{_id:2,host:"192.168.126.131:27017"},{_id:3,host:"192.168.126.130:27017"}]})
{
"ok" : 0,
"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.126.131:27017 failed with No route to host, 192.168.126.130:27017 failed with No route to host",
"code" : 74,
"codeName" : "NodeNotFound",
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
}
}

可能的原因是,节点间联通失败,这时要看一下config.config文件的bind_ip是不是配置了

dbpath=/home/****/mongo/config/data
logpath=/home/****/mongo/config/logs/config.log
bind_ip=0.0.0.0
port=27017
logappend=true
fork=true
quiet=true
journal=true
configsvr=true
replSet=configRS/192.168.126.130:27017,192.168.126.131:27017,192.168.126.132:27017

正确的返回结果样例:

MongoDB Enterprise > rs.initiate({_id:"configRS",configsvr:true,members:[{_id:1,host:"192.168.126.132:27017",priority:2},{_id:2,host:"192.168.126.131:27017"},{_id:3,host:"192.168.126.130:27017"}]})
{
"ok" : 1,
"operationTime" : Timestamp(1516954993, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1516954993, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"$clusterTime" : {
"clusterTime" : Timestamp(1516954993, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

然后到另外两台机器上查看一下,看看config节点群是不是联通了。

MongoDB Enterprise > rs.status()
{
"set" : "configRS",
"date" : ISODate("2018-01-26T08:30:51.551Z"),
"myState" : 2,
"term" : NumberLong(1),
"syncingTo" : "192.168.126.130:27017",
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 1,
"name" : "192.168.126.132:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 36,
"optime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-26T08:30:44Z"),
"optimeDurableDate" : ISODate("2018-01-26T08:30:44Z"),
"lastHeartbeat" : ISODate("2018-01-26T08:30:49.985Z"),
"lastHeartbeatRecv" : ISODate("2018-01-26T08:30:50.967Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1516955424, 1),
"electionDate" : ISODate("2018-01-26T08:30:24Z"),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.126.131:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 545,
"optime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-26T08:30:44Z"),
"syncingTo" : "192.168.126.130:27017",
"configVersion" : 1,
"self" : true
},
{
"_id" : 3,
"name" : "192.168.126.130:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 36,
"optime" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1516955444, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-26T08:30:44Z"),
"optimeDurableDate" : ISODate("2018-01-26T08:30:44Z"),
"lastHeartbeat" : ISODate("2018-01-26T08:30:49.991Z"),
"lastHeartbeatRecv" : ISODate("2018-01-26T08:30:50.986Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "192.168.126.132:27017",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1516955444, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
},
"$clusterTime" : {
"clusterTime" : Timestamp(1516955444, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

mongos服务:

编辑mongos配置文件mongo/router/router.config

configdb=configRS/192.168.126.132:27017,192.168.126.131:27017,192.168.126.130:27017
bind_ip=0.0.0.0
port=17017
fork=true
logpath=/home/****/mongo/router/logs/mongos.log

启动mongos,可能要等待5到10秒:

[****@centosvm router]$ sudo mongos --config router.config 
about to fork child process, waiting until server is ready for connections.
forked process: 14553
child process started successfully, parent exiting

mongos至少要启动两个以上,mongos的IP+端口就是客户端配置的mongo服务地址。

把router.config文件拷贝到另一台mongo机器上相同目录中,按照上面命令启动。

caused by :: ShardNotFound: Database cloudconf not found due to No shards found

shard服务:

在每台服务器下,我们已经建立好路径:mongo/shard/data/{shard1,shard2,shard3}

在shard目录下写好3个shard配置文件

vi /home/mongo/shard/shard1.config 

dbpath=/ home /mongo/shard/data/shard1 
logpath=/ home /mongo/shard/logs/shard1.log 
port=37017 
bind_ip=0.0.0.0
logappend=true 
fork=true 
quiet=true 
journal=true 
shardsvr=true 
replSet= shard1RS/192.168.141.201:37017,192.168.141.202:37017,192.168.141.203:37017

vi /home/mongo/shard/shard2.config 

dbpath=/home/mongo/ shard /data/ shard2 
logpath=/ home /mongo/ shard /logs/ shard2.log 
port=47017 
bind_ip=0.0.0.0
logappend=true 
fork=true 
quiet=true 
journal=true 
shardsvr =true 
replSet= shard2RS/192.168.141.201:47017,192.168.141.202:47017,192.168.141.203:47017

同上配置shard3.config,端口57017

注意:更改相应ip、端口、集群名

replSet= shard1RS/192.168.141.201:37017,192.168.141.202:37017,192.168.141.203:37017

启动3个shard服务,先把我们预先设计的主片启动,然后启动剩余shard

mongod -f /home/mongo/shard/shard1.config
 mongod -f /home/mongo/shard/shard2.config

初始化shard服务,进入任意一台mongodb服务器,配置每个shard副本集

mongo -port 37017
rs.initiate({_id:"shard1RS",members:[{_id:1,host:"192.168.141.201:37017",priority:2},{_id:2,host:"192.168.141.202:37017"},{_id:3,host:"192.168.141.203:37017"}]})
mongo -port 47017
rs.initiate({_id:"shard2RS",members:[{_id:1,host:"192.168.141.202:47017",priority:2},{_id:2,host:"192.168.141.201:47017"},{_id:3,host:"192.168.141.203:47017"}]})

同上完成端口57017配置

配置分片,将主片添加至集群

mongo -port 17017

>use admin

>db.runCommand({"addShard":"shard1RS/192.168.141.201:37017" ,"maxsize":1024})

>db.runCommand({"addShard":"shard2RS/192.168.141.202:47017" ,"maxsize":1024})

>db.runCommand({"addShard":"shard3RS/192.168.141.203:57017" ,"maxsize":1024})

使用,在使用的时候,需要对数据库开启分片功能,并对数据库下的表的字段指定分片算法

>use admin

--对库hdctest开启分片

>db.runCommand({"enablesharding":"hdctest"})

--对库hdctest下的表person按字段ID配置hash分库算法

>db.runCommand({"shardcollection":"hdctest.person","key":{_id:'hashed'}})

注意:登陆从库查看数据信息的时候,可能会报:

not master and slaveOk= false,code=13435

执行:

db.getMongo().setSlaveOk()

monodb分片集群部署的更多相关文章

  1. MongoDB DBA 实践8-----Linux系统Mongodb分片集群部署

    在Linux系统中,主要是使用命令行进行mongodb的分片集群部署 一.先决条件 mongodb安装成功,明确路径, MongoDB的几个路径: /var/lib/mongodb /var/log/ ...

  2. mongoDB研究笔记:分片集群部署

    前面几篇文章的分析复制集解决了数据库的备份与自动故障转移,但是围绕数据库的业务中当前还有两个方面的问题变得越来越重要.一是海量数据如何存储?二是如何高效的读写海量数据?尽管复制集也可以实现读写分析,如 ...

  3. MongoDB DBA 实践6-----MongoDB的分片集群部署

    一.分片 MongoDB使用分片技术来支持大数据集和高吞吐量操作. 1.分片目的 对于单台数据库服务器,庞大的数据量及高吞吐量的应用程序对它而言无疑是个巨大的挑战.频繁的CRUD操作能够耗尽服务器的C ...

  4. MongoDB分片集群部署方案

    前言 副本集部署是对数据的冗余和增加读请求的处理能力,却不能提高写请求的处理能力:关键问题是随着数据增加,单机硬件配置会成为性能的瓶颈.而分片集群可以很好的解决这一问题,通过水平扩展来提升性能.分片部 ...

  5. MongoDB在windows平台分片集群部署

    本文转载自:https://www.cnblogs.com/hx764208769/p/4260177.html 前言-为什么我要使用mongodb 最近我公司要开发一个日志系统,这个日志系统包括很多 ...

  6. MongoDB部署实战(一)MongoDB在windows平台分片集群部署

    前言-为什么我要使用mongodb 最近我公司要开发一个日志系统,这个日志系统包括很多类型,错误的,操作的,...用MongoDB存储日志,大量的日志产生,大量读写吞吐量很大的时候,单个Server很 ...

  7. mongo分片集群部署

    测试环境192.168.56.101-213 前期准备: openssl rand -base64 756 > /home/software/mongodb/mongodbkey chmod   ...

  8. redis分片集群安装部署

    redis分片集群安装与部署 分片集群的优势 高可用.且方便扩展. 数据分片,多节点提供服务,提高性能,数据提供冗余备份. 分片集群部署 只需更改配置文件 部署架构:6个节点,3主3从.数据集分为3片 ...

  9. Mongodb分片集群技术+用户验证

    随着数据量持续增多,后续迟早会出现一台机器硬件瓶颈问题的.而mongodb主打的就是海量数据架构,“分片”就用这个来解决这个问题. 从图中可以看到有四个组件:mongos.config server. ...

随机推荐

  1. vue.js 知识点(三)

    ---恢复内容开始--- vue和react相同,都是单项数据流,也就是只能从父组件流向子组件,但是因为根据引用的不同,子组件也是可以经过函数处理流向父组件的!这点跟react十分相似,但是也有不同: ...

  2. 多继承c3算法

    一.mro mro即 method resolution order (方法解释顺序),主要用于在多继承时判断属性的路径(来自于哪个类). print(类.mro)得到属性路径按继承顺序 在pytho ...

  3. Redis数据持久化,安全

    一.redis数据持久化 由于redis是一个内存数据库,如果系统遇到致命问题需要关机或重启,内存中的数据就会丢失,这是生产环境所不能允许的.所以redis提供了数据持久化的能力. redis提供了两 ...

  4. [ActionScript 3.0] AS3 ServerSocket示例(官方示例)

    下面的示例创建一个套接字服务器.要使用该服务器,可将套接字绑定到本地端口,然后从其他应用程序连接到该端口.该服务器仅识别 UTF-8 字符串. package { import flash.displ ...

  5. Eclipse Java SE升级Java EE

    网上教程大多是提供了“http://download.eclipse.org/releases/ganymede/”地址,但是实际更新过程中会报错. 大致查询了一下,很可能是版本不匹配的问题,正确的更 ...

  6. JavaScript DOM编程艺术 笔记(二)语句操作

    操作 var total = (1+4)*5; year = year +1; year++; var message = "i am" + "girl"; 是 ...

  7. 编写第一个Go程序

    编码格式 Go语言源码文件编码格式必须是 UTF-8 格式,否则会导致编译器出错. 结束语句 在 Go 程序中,一行代表一个语句结束.每个语句不需要像其它语言一样以分号 ";"结尾 ...

  8. 三种实现日志过滤器的方式 (过滤器 (Filter)、拦截器(Interceptors)和切面(Aspect))

    1.建立RequestWrapper类 import com.g2.order.server.utils.HttpHelper; import java.io.BufferedReader; impo ...

  9. Oracle递归查询(start with…connect by prior)

    查询基本结构: select … from table_name       start with 条件1       connect by 条件2 1.建测试用表 create table test ...

  10. org.elasticsearch.search.sort.SortBuilder使用

    org.elasticsearch.search.sort.SortBuilder是一个抽象类,有4个子类 org.elasticsearch.search.sort.FieldSortBuilder ...