mongoDB分片集群-Sharded Cluster

分片概念

分片( sharding )是一种跨多台机器分布数据的方法, MongoDB 使用分片来支持具有非常大的数据集和高吞吐量操作的部署。
换句话说:分片 (sharding) 是指将数据拆分,将其分散存在不同的机器上的过程。有时也用分区(partitioning)来表示这个概念。将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存更多的数据,处理更多的负载。
具有大型数据集或高吞吐量应用程序的数据库系统可以会挑战单个服务器的容量。例如,高查询率会耗尽服务器的CPU 容量。工作集大小大于系统的 RAM 会强调磁盘驱动器的 I / O 容量。
有两种解决系统增长的方法:垂直扩展和水平扩展。
垂直扩展意味着增加单个服务器的容量,例如使用更强大的 CPU ,添加更多 RAM 或增加存储空间量。可用技术的局限性可能会限制单个机器对于给定工作负载而言足够强大。此外,基于云的提供商基于可用的硬件配置具有硬性上限。结果,垂直缩放有实际的最大值。
水平扩展意味着划分系统数据集并加载多个服务器,添加其他服务器以根据需要增加容量。虽然单个机器的总体速度或容量可能不高,但每台机器处理整个工作负载的子集,可能提供比单个高速大容量服务器更高的效率。扩展部署容量只需要根据需要添加额外的服务器,这可能比单个机器的高端硬件的总体成本更低。权衡是基础架构和部署维护的复杂性增加。MongoDB支持通过分片进行水平扩展。

分片集群包含的组件

MongoDB 分片群集包含以下组件:
  • 分片(存储):每个分片包含分片数据的子集。 每个分片都可以部署为副本集。
  • mongos(路由):mongos充当查询路由器,在客户端应用程序和分片集群之间提供接口。
  • config servers调度的配置):配置服务器存储群集的元数据和配置设置。 从MongoDB 3.4始,必须将配置服务器部署为副本集(CSRS)。
下图描述了分片集群中组件的交互:
MongoDB 在集合级别对数据进行分片,将集合数据分布在集群中的分片上。
27018 if mongod is a shard member
27019 if mongod is a config server member

分片集群架构目标

两个分片节点副本集( 3+3 + 一个配置节点副本集( 3 + 两个路由节点( 2 ),共 11 个服务节点。

分片(存储)节点副本集的创建

所有的的配置文件都直接放到 sharded_cluster 的相应的子目录下面,默认配置文件名字:mongod.conf

第一套副本集

准备存放数据和日志的目录:
#-----------myshardrs01
mkdir -p /mongodb/sharded_cluster/myshardrs01_27018/log 
mkdir -p /mongodb/sharded_cluster/myshardrs01_27018/data/db 
mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/log 
mkdir -p /mongodb/sharded_cluster/myshardrs01_27118/data/db 
mkdir -p /mongodb/sharded_cluster/myshardrs01_27218/log 
mkdir -p /mongodb/sharded_cluster/myshardrs01_27218/data/db
新建或修改配置文件:
vim /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
myshardrs01_27018
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myshardrs01_27018/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.pid"
net:
    #服务实例绑定所有IP,有副作用,副本集初始化的时候,节点名字会自动设置为本地域名,而不是ip
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #bindIp
    #绑定的端口
    port: 27018
replication:
    #副本集的名称
    replSetName: myshardrs01
sharding:
    #分片角色
    clusterRole: shardsvr
sharding.clusterRole
Value
Description
configsvr
Start this instance as a config server . The instance starts on port 27019 by default.
shardsvr
Start this instance as a shard . The instance starts on port 27018 by default.
注意:
设置 sharding.clusterRole 需要 mongod 实例运行复制。 要将实例部署为副本集成员,请使用
replSetName 设置并指定副本集的名称。
新建或修改配置文件:
vim /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
myshardrs01_27118
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myshardrs01_27118/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.pid"
net:
    #服务实例绑定所有IP
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27118
replication:
    replSetName: myshardrs01
sharding:
    clusterRole: shardsvr
新建或修改配置文件:
vim /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
myshardrs01_27218
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myshardrs01_27218/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myshardrs01_27218/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27218/log/mongod.pid"
net:
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27218
replication:
    replSetName: myshardrs01
sharding:
    clusterRole: shardsvr
启动第一套副本集:一主一副本一仲裁
依次启动三个 mongod 服务:
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf

示例

[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
od.confabout to fork child process, waiting until server is ready for connections.
forked process: 2538
child process started successfully, parent exiting
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2567
child process started successfully, parent exiting
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2596
child process started successfully, parent exiting
[root@localhost ~]# 
查看服务是否启动:
[root@localhost ~]# ps -ef |grep mongod
root       2538      1  2 13:54 ?        00:00:01 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
root       2567      1  2 13:54 ?        00:00:01 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
root       2596      1  1 13:54 ?        00:00:01 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
root       2634   1517  0 13:56 pts/0    00:00:00 grep --color=auto mongod
[root@localhost ~]# 
1 )初始化副本集和创建主节点:
使用客户端命令连接任意一个节点,但这里尽量要连接主节点:
/usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27018
执行初始化副本集命令:
> rs.initiate()
{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "192.168.2.4:27018",
	"ok" : 1,
	"operationTime" : Timestamp(1703570219, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703570219, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs01:SECONDARY> 
查看副本集情况 ( 节选内容 )
myshardrs01:SECONDARY> rs.status()
{
	"set" : "myshardrs01",
	"date" : ISODate("2023-12-26T05:57:28.794Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1703570241, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1703570241, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1703570241, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1703570241, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1703570221, 2),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.2.4:27018",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 159,
			"optime" : {
				"ts" : Timestamp(1703570241, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-12-26T05:57:21Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1703570219, 2),
			"electionDate" : ISODate("2023-12-26T05:56:59Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1703570241, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703570241, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs01:PRIMARY> 
2 )主节点配置查看:
myshardrs01:PRIMARY> rs.conf()
{
	"_id" : "myshardrs01",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.2.4:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("658a6b2b9ecc809064759f36")
	}
}
myshardrs01:PRIMARY> 
3 )添加副本节点:
myshardrs01:PRIMARY>  rs.add("192.168.2.4:27118")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703570318, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703570318, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs01:PRIMARY> 
4 )添加仲裁节点:
myshardrs01:PRIMARY>  rs.addArb("192.168.2.4:27218")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703570355, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703570355, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs01:PRIMARY> 
查看副本集的配置情况:
myshardrs01:PRIMARY> rs.conf()
{
	"_id" : "myshardrs01",
	"version" : 3,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.2.4:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.2.4:27118",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.2.4:27218",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("658a6b2b9ecc809064759f36")
	}
}
myshardrs01:PRIMARY> 

第二套副本集

准备存放数据和日志的目录:
#-----------myshardrs02
mkdir -p /mongodb/sharded_cluster/myshardrs02_27318/log 
mkdir -p /mongodb/sharded_cluster/myshardrs02_27318/data/db 
mkdir -p /mongodb/sharded_cluster/myshardrs02_27418/log 
mkdir -p /mongodb/sharded_cluster/myshardrs02_27418/data/db 
mkdir -p /mongodb/sharded_cluster/myshardrs02_27518/log 
mkdir -p /mongodb/sharded_cluster/myshardrs02_27518/data/db
新建或修改配置文件:
vim /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
myshardrs02_27318
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myshardrs02_27318/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myshardrs02_27318/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myshardrs02_27318/log/mongod.pid"
net:
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27318
replication:
    replSetName: myshardrs02
sharding:
    clusterRole: shardsvr
新建或修改配置文件:
vim /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
myshardrs02_27418
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myshardrs02_27418/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myshardrs02_27418/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myshardrs02_27418/log/mongod.pid"
net:
    #服务实例绑定所有IP
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27418
replication:
    replSetName: myshardrs02
sharding:
    clusterRole: shardsvr
新建或修改配置文件:
vim /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf
myshardrs02_27518
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myshardrs02_27518/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myshardrs02_27518/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myshardrs02_27518/log/mongod.pid"
net:
    #服务实例绑定所有IP
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27518
replication:
    replSetName: myshardrs02
sharding:
    clusterRole: shardsvr
启动第二套副本集:一主一副本一仲裁
依次启动三个 mongod 服务:
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf

示例

[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
od.confabout to fork child process, waiting until server is ready for connections.
forked process: 3111
child process started successfully, parent exiting
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3140
child process started successfully, parent exiting
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3169
child process started successfully, parent exiting
[root@localhost ~]# 
查看服务是否启动:
[root@localhost ~]# ps -ef |grep mongod
root       2538      1  1 13:54 ?        00:00:23 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
root       2567      1  1 13:54 ?        00:00:23 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
root       2596      1  1 13:54 ?        00:00:17 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
root       2645   1517  0 13:56 pts/0    00:00:00 /usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27018
root       3111      1  3 14:19 ?        00:00:00 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
root       3140      1  3 14:19 ?        00:00:00 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
root       3169      1  2 14:19 ?        00:00:00 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf
root       3207   2794  0 14:20 pts/1    00:00:00 grep --color=auto mongod
[root@localhost ~]# 
1 )初始化副本集和创建主节点:
使用客户端命令连接任意一个节点,但这里尽量要连接主节点:
/usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27318
执行初始化副本集命令:
>  rs.initiate()
{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "192.168.2.4:27318",
	"ok" : 1,
	"operationTime" : Timestamp(1703571666, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703571666, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs02:SECONDARY> 
查看副本集情况 ( 节选内容 )
myshardrs02:SECONDARY> rs.status()
{
	"set" : "myshardrs02",
	"date" : ISODate("2023-12-26T06:21:31.945Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1703571688, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1703571688, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1703571688, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1703571688, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1703571668, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.2.4:27318",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 101,
			"optime" : {
				"ts" : Timestamp(1703571688, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-12-26T06:21:28Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1703571666, 2),
			"electionDate" : ISODate("2023-12-26T06:21:06Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1703571688, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703571688, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs02:PRIMARY> 
2 )主节点配置查看:
myshardrs01:PRIMARY> rs.conf()
{
	"_id" : "myshardrs01",
	"version" : 3,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.2.4:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.2.4:27118",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.2.4:27218",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("658a6b2b9ecc809064759f36")
	}
}
myshardrs01:PRIMARY> 
3 )添加副本节点:
myshardrs02:PRIMARY> rs.add("192.168.2.4:27418")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703574343, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703574343, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs02:PRIMARY> 
4 )添加仲裁节点:
myshardrs02:PRIMARY>  rs.addArb("192.168.2.4:27518")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703574372, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703574372, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myshardrs02:PRIMARY> 
查看副本集的配置情况:
myshardrs02:PRIMARY> rs.conf()
{
	"_id" : "myshardrs02",
	"version" : 3,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.2.4:27318",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.2.4:27418",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.2.4:27518",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("658a70d226e5698b5d741a0f")
	}
}
myshardrs02:PRIMARY> 

配置节点副本集的创建

第一步:准备存放数据和日志的目录:
#-----------configrs
#建立数据节点data和日志目录
mkdir -p /mongodb/sharded_cluster/myconfigrs_27019/log 
mkdir -p /mongodb/sharded_cluster/myconfigrs_27019/data/db 
mkdir -p /mongodb/sharded_cluster/myconfigrs_27119/log 
mkdir -p /mongodb/sharded_cluster/myconfigrs_27119/data/db 
mkdir -p /mongodb/sharded_cluster/myconfigrs_27219/log 
mkdir -p /mongodb/sharded_cluster/myconfigrs_27219/data/db
新建或修改配置文件:
vim /mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
myconfigrs_27019
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myconfigrs_27019/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.pid"
net:
    #服务实例绑定所有IP
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27019
replication:
    replSetName: myconfigrs
sharding:
    clusterRole: configsvr
新建或修改配置文件:
vim /mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
myconfigrs_27119
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myconfigrs_27119/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myconfigrs_27119/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myconfigrs_27119/log/mongod.pid"
net:
    #服务实例绑定所有IP
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27119
replication:
    replSetName: myconfigrs
sharding:
    clusterRole: configsvr
新建或修改配置文件:
vim /mongodb/sharded_cluster/myconfigrs_27219/mongod.conf
myconfigrs_27219
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/myconfigrs_27219/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    dbPath: "/mongodb/sharded_cluster/myconfigrs_27219/data/db"
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: "/mongodb/sharded_cluster/myconfigrs_27219/log/mongod.pid"
net:
    #服务实例绑定所有IP
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #绑定的端口
    port: 27219
replication:
    replSetName: myconfigrs
sharding:
    clusterRole: configsvr
启动配置副本集:一主两副本
依次启动三个 mongod 服务:
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27219/mongod.conf

示例

[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
confabout to fork child process, waiting until server is ready for connections.
forked process: 3549

child process started successfully, parent exiting
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3584
child process started successfully, parent exiting
[root@localhost ~]# /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27219/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3619
child process started successfully, parent exiting
[root@localhost ~]# 
查看服务是否启动:
[root@localhost ~]# ps -ef |grep mongod
root       2538      1  1 13:54 ?        00:00:44 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
root       2567      1  1 13:54 ?        00:00:47 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
root       2596      1  1 13:54 ?        00:00:34 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
root       2645   1517  0 13:56 pts/0    00:00:00 /usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27018
root       3111      1  1 14:19 ?        00:00:14 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
root       3140      1  1 14:19 ?        00:00:15 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
root       3169      1  1 14:19 ?        00:00:13 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf
root       3217   2794  0 14:20 pts/1    00:00:00 /usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27318
root       3549      1  2 14:38 ?        00:00:00 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
root       3584      1  2 14:38 ?        00:00:00 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
root       3619      1  2 14:38 ?        00:00:00 /usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/myconfigrs_27219/mongod.conf
root       3655   3454  0 14:38 pts/2    00:00:00 grep --color=auto mongod
[root@localhost ~]# 
1 )初始化副本集和创建主节点:
使用客户端命令连接任意一个节点,但这里尽量要连接主节点:
/usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27019
执行初始化副本集命令:
> rs.initiate()
{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "192.168.2.4:27019",
	"ok" : 1,
	"operationTime" : Timestamp(1703572769, 1),
	"$gleStats" : {
		"lastOpTime" : Timestamp(1703572769, 1),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"lastCommittedOpTime" : Timestamp(0, 0),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703572769, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myconfigrs:SECONDARY> 
查看副本集情况 ( 节选内容 )
myconfigrs:PRIMARY> rs.status()
{
	"set" : "myconfigrs",
	"date" : ISODate("2023-12-26T07:07:22.759Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"configsvr" : true,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1703574441, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1703574441, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1703574441, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1703574441, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1703574391, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.2.4:27019",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1761,
			"optime" : {
				"ts" : Timestamp(1703574441, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-12-26T07:07:21Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1703572769, 2),
			"electionDate" : ISODate("2023-12-26T06:39:29Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1703574441, 1),
	"$gleStats" : {
		"lastOpTime" : Timestamp(1703572769, 1),
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1703574441, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703574441, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myconfigrs:PRIMARY> 
2 )主节点配置查看:
myshardrs01:PRIMARY> rs.conf()
{
	"_id" : "myshardrs01",
	"version" : 5,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.2.4:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.2.4:27118",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.2.4:27218",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 3,
			"host" : "192.168.2.4:27418",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 4,
			"host" : "192.168.2.4:27518",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("658a6b2b9ecc809064759f36")
	}
}
myshardrs01:PRIMARY> 
3 )添加两个副本节点:
myshardrs01:PRIMARY> rs.add("192.168.2.4:27119")
myshardrs01:PRIMARY> rs.add("192.168.2.4:27219")

示例

myconfigrs:PRIMARY> rs.add("192.168.2.4:27119")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703574461, 1),
	"$gleStats" : {
		"lastOpTime" : {
			"ts" : Timestamp(1703574461, 1),
			"t" : NumberLong(1)
		},
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1703574452, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703574461, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myconfigrs:PRIMARY>  rs.add("192.168.2.4:27219")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703574466, 1),
	"$gleStats" : {
		"lastOpTime" : {
			"ts" : Timestamp(1703574466, 1),
			"t" : NumberLong(1)
		},
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1703574461, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703574466, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myconfigrs:PRIMARY> 
查看副本集的配置情况:
myconfigrs:PRIMARY> rs.conf()
{
	"_id" : "myconfigrs",
	"version" : 3,
	"configsvr" : true,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.2.4:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.2.4:27119",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "192.168.2.4:27219",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("658a75216772e0970da0bafc")
	}
}
myconfigrs:PRIMARY> rs.status()
{
	"set" : "myconfigrs",
	"date" : ISODate("2023-12-26T07:08:30.038Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"configsvr" : true,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1703574501, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1703574501, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1703574501, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1703574501, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1703574451, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.2.4:27019",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1829,
			"optime" : {
				"ts" : Timestamp(1703574501, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-12-26T07:08:21Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1703572769, 2),
			"electionDate" : ISODate("2023-12-26T06:39:29Z"),
			"configVersion" : 3,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.2.4:27119",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 48,
			"optime" : {
				"ts" : Timestamp(1703574501, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1703574501, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-12-26T07:08:21Z"),
			"optimeDurableDate" : ISODate("2023-12-26T07:08:21Z"),
			"lastHeartbeat" : ISODate("2023-12-26T07:08:28.710Z"),
			"lastHeartbeatRecv" : ISODate("2023-12-26T07:08:29.240Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.2.4:27019",
			"syncSourceHost" : "192.168.2.4:27019",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 3
		},
		{
			"_id" : 2,
			"name" : "192.168.2.4:27219",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 43,
			"optime" : {
				"ts" : Timestamp(1703574501, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1703574501, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-12-26T07:08:21Z"),
			"optimeDurableDate" : ISODate("2023-12-26T07:08:21Z"),
			"lastHeartbeat" : ISODate("2023-12-26T07:08:28.709Z"),
			"lastHeartbeatRecv" : ISODate("2023-12-26T07:08:29.283Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.2.4:27119",
			"syncSourceHost" : "192.168.2.4:27119",
			"syncSourceId" : 1,
			"infoMessage" : "",
			"configVersion" : 3
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1703574501, 1),
	"$gleStats" : {
		"lastOpTime" : {
			"ts" : Timestamp(1703574466, 1),
			"t" : NumberLong(1)
		},
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1703574501, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703574501, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
myconfigrs:PRIMARY> 

路由节点的创建和操作

第一个路由节点的创建和连接

第一步:准备存放数据和日志的目录:
#-----------mongos01
mkdir -p /mongodb/sharded_cluster/mymongos_27017/log
mymongos_27017 节点:
新建或修改配置文件:
vi /mongodb/sharded_cluster/mymongos_27017/mongos.conf
mongos.conf
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/mymongos_27017/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: /mongodb/sharded_cluster/mymongos_27017/log/mongod.pid"
net:
    #服务实例绑定所有IP,有副作用,副本集初始化的时候,节点名字会自动设置为本地域名,而不是ip
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #bindIp
    #绑定的端口
    port: 27017
sharding:
    #指定配置节点副本集
    configDB: myconfigrs/192.168.2.4:27019,192.168.2.4:27119,192.168.2.4:27219
启动 mongos
[root@localhost ~]# /usr/local/mongodb/bin/mongos -f /mongodb/sharded_cluster/mymongos_27017/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4523
child process started successfully, parent exiting
[root@localhost ~]# 
提示:启动如果失败,可以查看 log 目录下的日志,查看失败原因。
客户端登录 mongos
/usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27017
此时,写不进去数据,如果写数据会报错:
mongos> use aadb
switched to db aadb
mongos> db.aa.insert({aa:"aa"})
WriteCommandError({
	"ok" : 0,
	"errmsg" : "unable to initialize targeter for write op for collection aadb.aa :: caused by :: Database aadb not found :: caused by :: No shards found",
	"code" : 70,
	"codeName" : "ShardNotFound",
	"operationTime" : Timestamp(1703575237, 2),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703575237, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
})
mongos> 
原因:
通过路由节点操作,现在只是连接了配置节点,还没有连接分片数据节点,因此无法写入业务数据。
properties 配置文件参考:
logpath=/mongodb/sharded_cluster/mymongos_27017/log/mongos.log
logappend=true
bind_ip_all=true
port=27017
fork=true
configdb=myconfigrs/192.168.2.4:27019,192.168.2.4:27119,192.168.2.4:272
19

在路由节点上进行分片配置操作

使用命令添加分片:
1 )添加分片:
语法:
sh.addShard("IP:Port")
将第一套分片副本集添加进来:
mongos> sh.addShard("myshardrs01/192.168.2.4:27018,192.168.2.4:27118,192.168.2.4:27218")
{
	"shardAdded" : "myshardrs01",
	"ok" : 1,
	"operationTime" : Timestamp(1703575423, 8),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703575423, 8),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 
查看分片状态情况:
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("658a75236772e0970da0bb0b")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/192.168.2.4:27018,192.168.2.4:27118",  "state" : 1 }
  active mongoses:
        "4.0.10" : 1
  autosplit:
        Currently enabled: yes
  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:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

mongos> 
继续将第二套分片副本集添加进来:
mongos> sh.addShard("myshardrs02/192.168.2.4:27318,192.168.2.4:27418,192.168.2.4:27518")
{
	"shardAdded" : "myshardrs02",
	"ok" : 1,
	"operationTime" : Timestamp(1703575522, 4),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703575522, 4),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 
查看分片状态:
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("658a75236772e0970da0bb0b")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/192.168.2.4:27018,192.168.2.4:27118",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/192.168.2.4:27318,192.168.2.4:27418",  "state" : 1 }
  active mongoses:
        "4.0.10" : 1
  autosplit:
        Currently enabled: yes
  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:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

mongos> 
提示:如果添加分片失败,需要先手动移除分片,检查添加分片的信息的正确性后,再次添加分片。 移除分片参考( 了解 )
use admin
db.runCommand( { removeShard: "myshardrs02" } )
注意:如果只剩下最后一个 shard ,是无法删除的
移除时会自动转移分片数据,需要一个时间过程。
完成后,再次执行删除分片命令才能真正删除。
2 )开启分片功能: sh.enableSharding(" 库名 ") sh.shardCollection(" 库名 . 集合名 ",{"key":1})
mongos 上的 articledb 数据库配置 sharding:
mongos>  sh.enableSharding("articledb")
{
	"ok" : 1,
	"operationTime" : Timestamp(1703575605, 5),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703575605, 5),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 
查看分片状态:
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("658a75236772e0970da0bb0b")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/192.168.2.4:27018,192.168.2.4:27118",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/192.168.2.4:27318,192.168.2.4:27418",  "state" : 1 }
  active mongoses:
        "4.0.10" : 1
  autosplit:
        Currently enabled: yes
  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:
        {  "_id" : "articledb",  "primary" : "myshardrs02",  "partitioned" : true,  "version" : {  "uuid" : UUID("d0dd4fca-4c8f-45f7-b208-bfc8ada86e26"),  "lastMod" : 1 } }
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

mongos> 
3 )集合分片
对集合分片,你必须使用 sh.shardCollection() 方法指定集合和分片键。
语法:
sh.shardCollection(namespace, key, unique)
参数:
Parameter
Type
Description
namespace
string
要(分片)共享的目标集合的命名空间,格式: <database>. <collection>
key
document
用作分片键的索引规范文档。 shard 键决定 MongoDB 如何在shard之间分发文档。除非集合为空,否则索引必须在 shardcollection命令之前存在。如果集合为空,则 MongoDB 在对集合进行分片之前创建索引,前提是支持分片键的索引不存在。简单的说:由包含字段和该字段的索引遍历方向的文档组成。
unique
boolean
当值为 true 情况下,片键字段上会限制为确保是唯一索引。哈希策略片键不支持唯一索引。默认是false
对集合进行分片时 , 你需要选择一个 片键( Shard Key , shard key 是每条记录都必须包含的 , 且建立了索引的单个字段或复合字段,MongoDB 按照片键将数据划分到不同的 数据块 中 , 并将 数据块 均衡地分布到所有分片中. 为了按照片键划分数据块 ,MongoDB 使用 基于哈希的分片方式(随机平均分配)或者基于范围的分片方式(数值大小分配) 。
用什么字段当片键都可以,如: nickname 作为片键,但一定是必填字段。
分片规则一:哈希策略
对于 基于哈希的分片 ,MongoDB 计算一个字段的哈希值 , 并用这个哈希值来创建数据块 .
在使用基于哈希分片的系统中 , 拥有 相近 片键的文档 很可能不会 存储在同一个数据块中 , 因此数据的分离性更好一些.
使用 nickname 作为片键,根据其值的哈希值进行数据分片
mongos> sh.shardCollection("articledb.comment",{"nickname":"hashed"})
{
	"collectionsharded" : "articledb.comment",
	"collectionUUID" : UUID("ecbc9ca7-7875-4a11-9336-434f21d7b8f6"),
	"ok" : 1,
	"operationTime" : Timestamp(1703575752, 33),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703575752, 33),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 
查看分片状态: sh.status()
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("658a75236772e0970da0bb0b")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/192.168.2.4:27018,192.168.2.4:27118",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/192.168.2.4:27318,192.168.2.4:27418",  "state" : 1 }
  active mongoses:
        "4.0.10" : 1
  autosplit:
        Currently enabled: yes
  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:
        {  "_id" : "articledb",  "primary" : "myshardrs02",  "partitioned" : true,  "version" : {  "uuid" : UUID("d0dd4fca-4c8f-45f7-b208-bfc8ada86e26"),  "lastMod" : 1 } }
                articledb.comment
                        shard key: { "nickname" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01	2
                                myshardrs02	2
                        { "nickname" : { "$minKey" : 1 } } -->> { "nickname" : NumberLong("-4611686018427387902") } on : myshardrs01 Timestamp(1, 0) 
                        { "nickname" : NumberLong("-4611686018427387902") } -->> { "nickname" : NumberLong(0) } on : myshardrs01 Timestamp(1, 1) 
                        { "nickname" : NumberLong(0) } -->> { "nickname" : NumberLong("4611686018427387902") } on : myshardrs02 Timestamp(1, 2) 
                        { "nickname" : NumberLong("4611686018427387902") } -->> { "nickname" : { "$maxKey" : 1 } } on : myshardrs02 Timestamp(1, 3) 
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01	1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : myshardrs01 Timestamp(1, 0) 

mongos> 
分片规则二:范围策略
对于 基于范围的分片 ,MongoDB 按照片键的范围把数据分成不同部分 . 假设有一个数字的片键 : 想象一个从负无穷到正无穷的直线, 每一个片键的值都在直线上画了一个点 .MongoDB 把这条直线划分为更短的不重叠的片段, 并称之为 数据块 , 每个数据块包含了片键在一定范围内的数据 .
在使用片键做范围划分的系统中 , 拥有 相近 片键的文档很可能存储在同一个数据块中 , 因此也会存储在同一个分片中.
如使用作者年龄字段作为片键,按照点赞数的值进行分片:
mongos> sh.shardCollection("articledb.author",{"age":1})
{
	"collectionsharded" : "articledb.author",
	"collectionUUID" : UUID("37f85f7a-d47e-46a0-a477-cd4eafa5f17a"),
	"ok" : 1,
	"operationTime" : Timestamp(1703575816, 12),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1703575816, 12),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 
注意的是:
  1. 1)一个集合只能指定一个片键,否则报错。
  2. 2)一旦对一个集合分片,分片键和分片值就不可改变。 如:不能给集合选择不同的分片键、不能更新分片键的值。
  3. 3)根据age索引进行分配数据。
基于范围的分片方式与基于哈希的分片方式性能对比:
基于范围的分片方式提供了更高效的范围查询 , 给定一个片键的范围 , 分发路由可以很简单地确定哪个数据块存储了请求需要的数据, 并将请求转发到相应的分片中 .
不过 , 基于范围的分片会导致数据在不同分片上的不均衡 , 有时候 , 带来的消极作用会大于查询性能的积极作用. 比如 , 如果片键所在的字段是线性增长的 , 一定时间内的所有请求都会落到某个固定的数据块中 , 最终导致分布在同一个分片中. 在这种情况下 , 一小部分分片承载了集群大部分的数据 , 系统并不能很好地进行扩展.
与此相比 , 基于哈希的分片方式以范围查询性能的损失为代价 , 保证了集群中数据的均衡 . 哈希值的随机性使数据随机分布在每个数据块中, 因此也随机分布在不同分片中 . 但是也正由于随机性 , 一个范围查询很难确定应该请求哪些分片, 通常为了返回需要的结果 , 需要请求所有分片 .
如无特殊情况,一般推荐使用 Hash Sharding
而使用 _id 作为片键是一个不错的选择,因为它是必有的,你可以使用数据文档 _id 的哈希作为片键。这个方案能够是的读和写都能够平均分布,并且它能够保证每个文档都有不同的片键所以数据块能够很精细。
似乎还是不够完美,因为这样的话对多个文档的查询必将命中所有的分片。虽说如此,这也是一种比较好的方案了。 理想化的 shard key 可以让 documents 均匀地在集群中分布:

显示集群的详细信息:

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("658a75236772e0970da0bb0b")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/192.168.2.4:27018,192.168.2.4:27118",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/192.168.2.4:27318,192.168.2.4:27418",  "state" : 1 }
  active mongoses:
        "4.0.10" : 1
  autosplit:
        Currently enabled: yes
  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:
        {  "_id" : "articledb",  "primary" : "myshardrs02",  "partitioned" : true,  "version" : {  "uuid" : UUID("d0dd4fca-4c8f-45f7-b208-bfc8ada86e26"),  "lastMod" : 1 } }
                articledb.author
                        shard key: { "age" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs02	1
                        { "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : myshardrs02 Timestamp(1, 0) 
                articledb.comment
                        shard key: { "nickname" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01	2
                                myshardrs02	2
                        { "nickname" : { "$minKey" : 1 } } -->> { "nickname" : NumberLong("-4611686018427387902") } on : myshardrs01 Timestamp(1, 0) 
                        { "nickname" : NumberLong("-4611686018427387902") } -->> { "nickname" : NumberLong(0) } on : myshardrs01 Timestamp(1, 1) 
                        { "nickname" : NumberLong(0) } -->> { "nickname" : NumberLong("4611686018427387902") } on : myshardrs02 Timestamp(1, 2) 
                        { "nickname" : NumberLong("4611686018427387902") } -->> { "nickname" : { "$maxKey" : 1 } } on : myshardrs02 Timestamp(1, 3) 
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01	1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : myshardrs01 Timestamp(1, 0) 

mongos> 

 查看均衡器是否工作(需要重新均衡时系统才会自动启动,不用管它):

mongos> sh.isBalancerRunning()
false
mongos> 
查看当前 Balancer 状态:
mongos> sh.getBalancerState()
true
mongos> 

分片后插入数据测试

测试一(哈希规则):登录 mongs 后,向 comment 循环插入 1000 条数据做测试:
mongos> use articledb
switched to db articledb
mongos> for(var i=1;i<=1000;i++){db.comment.insert({_id:i+"",nickname:"BoBo"+i})}
WriteResult({ "nInserted" : 1 })
mongos> db.comment.count()
1000
提示: js 的语法,因为 mongo shell 是一个 JavaScript shell
注意:从路由上插入的数据,必须包含片键,否则无法插入。
分别登陆两个片的主节点,统计文档数量
第一个分片副本集:
/usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27018
myshardrs01:PRIMARY> use articledb
switched to db articledb
myshardrs01:PRIMARY> db.comment.count()
508
第二个分片副本集:
/usr/local/mongodb/bin/mongo --host 192.168.2.4 --port 27318
myshardrs02:PRIMARY> use articledb
switched to db articledb
myshardrs02:PRIMARY> db.comment.count()
493
可以看到, 1000 条数据近似均匀的分布到了 2 shard 上。是根据片键的哈希值分配的。
这种分配方式非常易于水平扩展:一旦数据存储需要更大空间,可以直接再增加分片即可,同时提升了性能。
使用 db.comment.stats() 查看单个集合的完整情况, mongos 执行该命令可以查看该集合的数据分片的情况。
使用 sh.status() 查看本库内所有集合的分片信息。
测试二(范围规则):登录 mongs 后,向 comment 循环插入 1000 条数据做测试:
mongos> use articledb
switched to db articledb
mongos> for(var i=1;i<=20000;i++)
{db.author.save({"name":"BoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBo"+i,"age":NumberInt(i%120)})}
WriteResult({ "nInserted" : 1 })
mongos> db.comment.count()
20000
插入成功后,仍然要分别查看两个分片副本集的数据情况。
分片效果:
articledb.author
shard key: { "age" : 1 }
unique: false
balancing: true
chunks:
myshardrs01 2
myshardrs02 1
{ "age" : { "$minKey" : 1 } } -->> { "age" : 0 } on :
myshardrs02 Timestamp(2, 0)
{ "age" : 0 } -->> { "age" : 112 } on : myshardrs01
Timestamp(2, 1)
{ "age" : 112 } -->> { "age" : { "$maxKey" : 1 } } on :
myshardrs01 Timestamp(1, 3)
提示:
如果查看状态发现没有分片,则可能是由于以下原因造成了:
1 )系统繁忙,正在分片中。
2 )数据块( chunk )没有填满,默认的数据块尺寸( chunksize )是 64M ,填满后才会考虑向其他片的数据块填充数据,因此,为了测试,可以将其改小,这里改为1M ,操作如下:
use config
db.settings.save( { _id:"chunksize", value: 1 } )
测试完改回来:
db.settings.save( { _id:"chunksize", value: 64 } )
注意:要先改小,再设置分片。为了测试,可以先删除集合,重新建立集合的分片策略,再插入数据测试即可。

再增加一个路由节点

文件夹:
#-----------mongos02
mkdir -p /mongodb/sharded_cluster/mymongos_27117/log
新建或修改配置文件:
vi /mongodb/sharded_cluster/mymongos_27117/mongos.conf
mongos.conf
systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/sharded_cluster/mymongos_27117/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
    pidFilePath: /mongodb/sharded_cluster/mymongos_27117/log/mongod.pid"
net:
    #服务实例绑定所有IP,有副作用,副本集初始化的时候,节点名字会自动设置为本地域名,而不是ip
    #bindIpAll: true
    #服务实例绑定的IP
    bindIp: localhost,192.168.2.4
    #bindIp
    #绑定的端口
    port: 27117
sharding:
    configDB: myconfigrs/192.168.2.4:27019,192.168.2.4:27119,192.168.2.4:27219
启动 mongos2
[root@localhost ~]# /usr/local/mongodb/bin/mongos -f /mongodb/sharded_cluster/mymongos_27117/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 5084
child process started successfully, parent exiting
[root@localhost ~]# 
使用 mongo 客户端登录 27117 ,发现,第二个路由无需配置,因为分片配置都保存到了配置服务器中了。

Compass连接分片集群

compass 连接:

提示:和连接单机mongod一样。

SpringDataMongDB连接分片集群

Java 客户端常用的是 SpringDataMongoDB ,其连接的是 mongs 路由,配置和单机 mongod 的配置是一样的。
多个路由的时候的 SpringDataMongoDB 的客户端配置参考如下:
spring:
    #数据源配置
    data:
        mongodb:
            # 主机地址
            # host: 192.168.2.4
            # 数据库
            # database: articledb
            # 默认端口是27017
            # port: 27017
            #也可以使用uri连接
            # uri: mongodb://192.168.2.4:28017/articledb
            # 连接副本集字符串
            # uri:mongodb://192.168.2.4:27017,192.168.2.4:27018,192.168.2.4:27019/article
db?connect=replicaSet&slaveOk=true&replicaSet=myrs
#连接路由字符串
            uri: mongodb://192.168.2.4:27017,192.168.2.4:27117/articledb
通过日志发现,写入数据的时候,会选择一个路由写入:
2019-09-03 11:04:09.166 INFO 11816 --- [68.40.141:27117]
org.mongodb.driver.connection : Opened connection
[connectionId{localValue:2}] to 180.76.159.126:27117
2019-09-03 11:04:09.166 INFO 11816 --- [68.40.141:27017]
org.mongodb.driver.connection : Opened connection
[connectionId{localValue:1}] to 180.76.159.126:27017
2019-09-03 11:04:09.529 INFO 11816 --- [ main]
org.mongodb.driver.connection : Opened connection
[connectionId{localValue:3}] to 180.76.159.126:27117
2019-09-03 11:04:09.826 INFO 11816 --- [ main]
c.i.article.service.CommentServiceTest : Started CommentServiceTest in 7.009
seconds (JVM running for 8.043)
2019-09-03 11:04:10.173 INFO 11816 --- [ Thread-2]
org.mongodb.driver.connection : Closed connection
[connectionId{localValue:3}] to 180.76.159.126:27117 because the

清除所有的节点数据(备用)

如果在搭建分片的时候有操作失败或配置有问题,需要重新来过的,可以进行如下操作:
第一步:查询出所有的测试服务节点的进程:
[root@bobohost sharded_cluster]# ps -ef |grep mongo
root 10184 1 0 06:04 ? 00:01:25 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
root 10219 1 0 06:04 ? 00:01:25 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
root 10253 1 0 06:04 ? 00:00:46 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
root 10312 1 0 06:04 ? 00:01:23 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
root 10346 1 0 06:05 ? 00:01:23 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
root 10380 1 0 06:05 ? 00:00:44 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myshardrs02_27518/mongod.conf
root 10414 1 1 06:05 ? 00:01:36 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
root 10453 1 1 06:05 ? 00:01:37 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
root 10492 1 1 06:05 ? 00:01:38 /usr/local/mongodb/bin/mongod
-f /mongodb/sharded_cluster/myconfigrs_27219/mongod.conf
root 11392 1 0 06:15 ? 00:00:24 /usr/local/mongodb/bin/mongos
--config /mongodb/sharded_cluster/mymongos_27017/mongos.cfg
root 14829 1 0 07:15 ? 00:00:13 /usr/local/mongodb/bin/mongos
--config /mongodb/sharded_cluster/mymongos_27117/mongos.cfg
根据上述的进程编号,依次中断进程:
kill -2 进程编号
第二步:清除所有的节点的数据:
rm -rf /mongodb/sharded_cluster/myconfigrs_27019/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myconfigrs_27119/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myconfigrs_27219/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myshardrs01_27018/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myshardrs01_27118/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myshardrs01_27218/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myshardrs02_27318/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myshardrs02_27418/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/myshardrs02_27518/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/mymongos_27017/data/db/*.* \ &
rm -rf /mongodb/sharded_cluster/mymongos_27117/data/db/*.*
第三步:查看或修改有问题的配置
第四步:依次启动所有节点,不包括路由节点:
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myshardrs01_27018/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myshardrs01_27118/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myshardrs01_27218/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myshardrs02_27318/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myshardrs02_27418/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myshardrs02_27518/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myconfigrs_27019/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myconfigrs_27119/mongod.conf
/usr/local/mongodb/bin/mongod -f
/mongodb/sharded_cluster/myconfigrs_27219/mongod.conf
第五步:对两个数据分片副本集和一个配置副本集进行初始化和相关配置
第六步:检查路由 mongos 的配置,并启动 mongos
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/mymongos_27017/mongos.cfg
/usr/local/mongodb/bin/mongod -f /mongodb/sharded_cluster/mymongos_27017/mongos.cfg
第七步: mongo 登录 mongos ,在其上进行相关操作。
  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值