monodb分片集群部署

Posted 刘广辉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了monodb分片集群部署相关的知识,希望对你有一定的参考价值。

本文档基于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分片集群部署的主要内容,如果未能解决你的问题,请参考以下文章

手把手超详细Docker部署MongoDB集群

『MongoDB』MongoDB高可用部署架构——分片集群篇(Sharding)

常见ClickHouse集群部署架构

使用ansible一键部署MongoDB分片集群

clickhouse两分片两副本集群部署

ES分片副本设置及集群部署