环境:
mongodb:4.4.22
1.范围分片:
刚开始没有使用分片,后面写入数据了使用分片
在路由服务器上执行:
mongos> sh.shardCollection("db_pushmsg.app_message_nofenpian",{"user_id": 1}) { "ok" : 0, "errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection", "code" : 72, "codeName" : "InvalidOptions", "operationTime" : Timestamp(1689918228, 5), "$clusterTime" : { "clusterTime" : Timestamp(1689918228, 5), "signature" : { "hash" : BinData(0,"MLDP+DoqO+0iD9jhEzrBb2dB+Lw="), "keyId" : NumberLong("7257793727152783361") } } }
解决办法:
创建索引
db.app_message_nofenpian.createIndex({"user_id":1})
db.app_message_nofenpian.getIndexes()
再次执行分片
mongos> sh.shardCollection("db_pushmsg.app_message_nofenpian",{"user_id": 1}) { "collectionsharded" : "db_pushmsg.app_message_nofenpian", "collectionUUID" : UUID("5382ae8e-2e07-4185-bdd0-db8ee85bad5e"), "ok" : 1, "operationTime" : Timestamp(1689918641, 5), "$clusterTime" : { "clusterTime" : Timestamp(1689918641, 5), "signature" : { "hash" : BinData(0,"WSjbiHdll3P7O1ITDltvVdMijwk="), "keyId" : NumberLong("7257793727152783361") } } }
查看是否平衡
mongos> sh.isBalancerRunning()
true
平衡完成后会变成false
mongos> sh.isBalancerRunning()
false
2.hash分片
sh.shardCollection("db_pushmsg.app_message_nofenpian01",{"user_id": "hashed"}) mongos> sh.shardCollection("db_pushmsg.app_message_nofenpian01",{"user_id": "hashed"}) { "ok" : 0, "errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection", "code" : 72, "codeName" : "InvalidOptions", "operationTime" : Timestamp(1689921384, 2), "$clusterTime" : { "clusterTime" : Timestamp(1689921384, 2), "signature" : { "hash" : BinData(0,"aBgMib51mh1bRNbNLTP5uJ7Rl2E="), "keyId" : NumberLong("7257793727152783361") } } }
创建索引(注意这里必须指定hashed)
db.app_message_nofenpian01.createIndex({"user_id":"hashed"})
标签:mongodb,app,db,Timestamp,表由未,分片,message,clusterTime From: https://www.cnblogs.com/hxlasky/p/17571492.html