搭建mongodb分片集群

 

 注意:mongos、config、shard三个角色的实例的keyfile内容保证完全一致:

如果搭建副本集时,出错,那么删掉

[work@hostname etc]$ cat mongodb.conf
systemLog:
  destination: file
  path: /home/work/mongodb_dba/mongo_30000/log/mongodb_dba.log
  logAppend: true

#net Options
net:
  maxIncomingConnections: 10240
  port: 30000
  bindIp: 10.10.10.30,localhost
  serviceExecutor : adaptive

#security Options
security:
  keyFile: /home/work/mongodb_dba/mongo_30000/etc/test.mongos
  clusterAuthMode: "keyFile"

sharding:
  configDB: "test_config/10.10.10.40:40000,10.10.10.41:40000,10.10.10.42:40000"

processManagement:
  fork: true
  pidFilePath: /home/work/mongodb_dba/mongo_30000/tmp/mongo_30000.pid

mongos启动:(注意:不是用mongod来启动)

注意一:mongos启动


[
work@hostname mongodb_dba]$ /home/work/mongodb_dba/4.0/bin/mongos --config /home/work/mongodb_dba/mongo_30000/etc/mongodb.conf 2020-10-27T14:49:34.383+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: 30389 child process started successfully, parent exiting

注意二:配置的config需要有相同的keyfile认证,即config开启认证


[work@tj1-ai-stag-db-i1-01 mongodb_dba]$ /home/work/mongodb_dba/4.0/bin/mongos --config /home/work/mongodb_dba/mongo_30000/etc/mongodb.conf 2020-10-27T16:40:09.882+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: 300974

 登录mongos:

/home/work/mongodb_dba/4.0/bin/mongo  --host 10.38.167.100 --port 30000  -u mongo_dba  -p 123456 --authenticationDatabase admin


mongos> use admin
switched to db admin
mongos> sh.addShard("test_shard1/10.10.10.10:28001");
{
"shardAdded" : "test_shard1",
"ok" : 1,
"operationTime" : Timestamp(1603788736, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1603788736, 3),
"signature" : {
"hash" : BinData(0,"32np1vC8xMXEKRFRgE5MGjZvKqM="),
"keyId" : NumberLong("6888216464256401439")
}
}
}
mongos>

mongos> sh.status()
--- Sharding Status --- 
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f97dc61cd2244b6a298aaf4")
}
shards:
{ "_id" : "test_shard1", "host" : "test_shard1/10.10.10.10:28001", "state" : 1 }
active mongoses:
"4.0.17-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 }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
test_shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : test_shard1 Timestamp(1, 0)

mongos>



#######################################################
sh.addShard("test_shard1/10.10.10.10:28001,10.10.10.11:28001,10.10.10.12");
sh.addShard("分片集群的副本及名称/该副本集的ip列表")

############################

原文地址:https://www.cnblogs.com/igoodful/p/13877788.html