社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
mongodb复制集群搭建演示可参考上一篇:mongoDB复制集搭建,演示
原因简介
随着数据的增长,单机实例的瓶颈是很明显的。可以通过复制的机制应对压力,但mongodb中单个集群的 节点数量限制到了12个以内,所以需要通过分片进一步横向扩展。此外分片也可节约磁盘的存储。
分片优势简介
a、透明化
MongoDB自带mongos路由进程。通过mongos将客户端发来的请求准确无误的路由到集群中的一个或者一组服务器上,同时会把接收到的响应聚合起来发回到客户端。
b.高可用
MongoDB通过将副本集和分片集群结合使用,在确保数据实现分片的同时,也确保了各分片数据都有相应的备份,这样就可以确保当主服务器宕机时,其他的从库可以立即替换,继续工作。
c.易扩展
当系统需要更多的空间和资源的时候,MongoDB使我们可以按需方便的扩充系统容量。
组件 | 说明 |
---|---|
Mongos | 提供对外应用访问,所有操作均通过mongos执行。一般有多个mongos节点。数据迁移和数据自动平衡。 |
Config Server | 存储集群所有节点、分片数据路由信息。默认需要配置3个Config Server节点。 路由节点基于元数据信息 决定把请求发给哪个分片。 |
Mongod | 存储应用数据记录。一般有多个Mongod节点,达到数据分片目的。实际存储的节点,其每个数据块默认为64M,满了之后就会产生新的数据库。 |
实际上,生产使用的所有组件都是复制集群的,实际架构如下:
(备注:下面为了简化演示,配置节点集群使用2个节点,分片节点集群使用2个,路由节点集群使用1个,27017~57018是相应的端口号,搭建演示的服务器环境是阿里云的centos7上做伪分片集群演示)
#数据目录
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/config-37017
#端口
port=37017
#日志
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/config-37017/37017.log
#后台启动
fork=true
#复制集名称
replSet=configCluster
#配置节点
configsvr=true
37018节点的conf配置信息:
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/config-37018
port=37018
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/config-37018/37018.log
fork=true
replSet=configCluster
configsvr=true
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongod -f conf/conf-37017.conf
about to fork child process, waiting until server is ready for connections.
forked process: 32692
child process started successfully, parent exiting
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongod -f conf/conf-37018.conf
about to fork child process, waiting until server is ready for connections.
forked process: 32726
child process started successfully, parent exiting
[root@hugo mongodb-linux-x86_64-4.0.5]# ps -ef|grep mongo
root 32692 1 5 14:46 ? 00:00:00 ./bin/mongod -f conf/conf-37017.conf
root 32726 1 9 14:46 ? 00:00:00 ./bin/mongod -f conf/conf-37018.conf
root 32761 32621 0 14:46 pts/3 00:00:00 grep --color=auto mongo
进入37017节点的客户端配置配置节点集群的集群设置:
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongo --port 37017
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:37017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("434beeea-5179-4a2d-ad8d-01bd094d96a0") }
MongoDB server version: 4.0.5
Server has startup warnings:
2019-11-15T14:46:04.066+0800 I STORAGE [initandlisten]
2019-11-15T14:46:04.066+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-11-15T14:46:04.066+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7230 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2019-11-15T14:46:04.782+0800 I CONTROL [initandlisten]
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
> var cfg = {
... "_id":"configCluster",
... "protocolVersion":1,
... "members":[
... {
... "_id":0,"host":"127.0.0.1:37017"
... },{
... "_id":1,"host":"127.0.0.1:37018"
... }
... ]
... }
> rs.initiate(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1573800528, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1573800528, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1573800528, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
configCluster:SECONDARY> rs.status()
{
"set" : "configCluster",
"date" : ISODate("2019-11-15T06:49:09.813Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1573800540, 1),
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:37017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 185,
"optime" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-15T06:49:04Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1573800539, 1),
"electionDate" : ISODate("2019-11-15T06:48:59Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:37018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 21,
"optime" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1573800544, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-15T06:49:04Z"),
"optimeDurableDate" : ISODate("2019-11-15T06:49:04Z"),
"lastHeartbeat" : ISODate("2019-11-15T06:49:09.586Z"),
"lastHeartbeatRecv" : ISODate("2019-11-15T06:49:08.074Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:37017",
"syncSourceHost" : "127.0.0.1:37017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1573800544, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1573800528, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1573800544, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1573800544, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
configCluster:PRIMARY>
#端口号
port=27017
#日志
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/mongos-27017/27017.log
#后台启动
fork=true
#configCluster是我们上面配置的配置节点复制集的名称,要对应,不然启动不了
configdb=configCluster/127.0.0.1:37017,127.0.0.1:37018
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongos -f conf/mongos-27017.conf
2019-11-15T15:52:52.052+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 2191
child process started successfully, parent exiting
[root@hugo mongodb-linux-x86_64-4.0.5]# ps -ef|grep mongo
root 2191 1 0 15:52 ? 00:00:00 ./bin/mongos -f conf/mongos-27017.conf
root 2297 1471 0 15:56 pts/4 00:00:00 grep --color=auto mongo
root 32692 1 0 14:46 ? 00:00:18 ./bin/mongod -f conf/conf-37017.conf
root 32726 1 0 14:46 ? 00:00:18 ./bin/mongod -f conf/conf-37018.conf
[root@hugo mongodb-linux-x86_64-4.0.5]#
现在启动的路由节点是还不能正常写入数据的,因为我们的shard(分片)节点集群还没有启动:
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f1734e03-cc66-44a5-b7cb-e880de610002") }
MongoDB server version: 4.0.5
Server has startup warnings:
2019-11-15T15:52:52.057+0800 I CONTROL [main]
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2019-11-15T15:52:52.058+0800 I CONTROL [main]
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** WARNING: This server is bound to localhost.
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** Remote systems will be unable to connect to this server.
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** Start the server with --bind_ip <address> to specify which IP
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** addresses it should serve responses from, or with --bind_ip_all to
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** bind to all interfaces. If this behavior is desired, start the
2019-11-15T15:52:52.058+0800 I CONTROL [main] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-11-15T15:52:52.058+0800 I CONTROL [main]
mongos> show dbs;
admin 0.000GB
config 0.000GB
mongos> use hugo;
switched to db hugo
mongos> db.emp.insert({"name":"张三"})
WriteCommandError({
"ok" : 0,
"errmsg" : "unable to initialize targeter for write op for collection hugo.emp :: caused by :: Database hugo not found :: caused by :: No shards found",
"code" : 70,
"codeName" : "ShardNotFound",
"operationTime" : Timestamp(1573804857, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1573804857, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
})
47017的配置:
#数据目录
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/shard-47017
#端口
port=47017
#日志
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/shard-47017/47017.log
#后台启动
fork=true
#复制集名称/副本集名称
replSet=shardCluster
#表明是分片服务
shardsvr=true
47018的配置:
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/shard-47018
port=47018
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/shard-47018/47018.log
fork=true
replSet=shardCluster
shardsvr=true
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongod -f conf/shard-47017.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2827
child process started successfully, parent exiting
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongod -f conf/shard-47018.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2909
child process started successfully, parent exiting
[root@hugo mongodb-linux-x86_64-4.0.5]# ps -ef|grep mongo
root 2191 1 0 15:52 ? 00:00:01 ./bin/mongos -f conf/mongos-27017.conf
root 2827 1 0 16:14 ? 00:00:00 ./bin/mongod -f conf/shard-47017.conf
root 2909 1 3 16:15 ? 00:00:00 ./bin/mongod -f conf/shard-47018.conf
root 2952 1471 0 16:16 pts/4 00:00:00 grep --color=auto mongo
root 32692 1 0 14:46 ? 00:00:23 ./bin/mongod -f conf/conf-37017.conf
root 32726 1 0 14:46 ? 00:00:24 ./bin/mongod -f conf/conf-37018.conf
[root@hugo mongodb-linux-x86_64-4.0.5]#
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongo --port 47017
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:47017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7be3f20d-c3cf-4ee9-bb24-ae9f1695db6c") }
MongoDB server version: 4.0.5
Server has startup warnings:
2019-11-15T16:14:42.422+0800 I STORAGE [initandlisten]
2019-11-15T16:14:42.422+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-11-15T16:14:42.422+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten]
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten]
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-11-15T16:14:43.147+0800 I CONTROL [initandlisten]
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten]
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten]
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten]
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7230 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2019-11-15T16:14:43.148+0800 I CONTROL [initandlisten]
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
> var cfg = {
... "_id":"shardCluster",
... "protocolVersion":1,
... "members":[
... {
... "_id":0,"host":"127.0.0.1:47017"
... },{
... "_id":1,"host":"127.0.0.1:47018"
... }
... ]
... }
> rs.initiate(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1573806134, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1573806134, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shardCluster:SECONDARY> rs.status()
{
"set" : "shardCluster",
"date" : ISODate("2019-11-15T08:23:17.712Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1573806148, 1),
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:47017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 515,
"optime" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-15T08:23:08Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1573806146, 1),
"electionDate" : ISODate("2019-11-15T08:22:26Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:47018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 62,
"optime" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1573806188, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-15T08:23:08Z"),
"optimeDurableDate" : ISODate("2019-11-15T08:23:08Z"),
"lastHeartbeat" : ISODate("2019-11-15T08:23:16.294Z"),
"lastHeartbeatRecv" : ISODate("2019-11-15T08:23:17.074Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:47017",
"syncSourceHost" : "127.0.0.1:47017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1573806188, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1573806188, 1),
"signature" : {
"hash"
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhiwule1314/article/details/103085386
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
-
发表于 2020-03-08 14:54:23
- 阅读 ( 1704 )
- 分类:数据库
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!