ITPub博客

首页 > 数据库 > NoSQL > 【Mongo】单节点升级为复制集再升级为分片加复制集

【Mongo】单节点升级为复制集再升级为分片加复制集

原创 NoSQL 作者:小亮520cl 时间:2018-05-17 16:12:21 0 删除 编辑
1.单节点配置如下
  1. [root@ip-10-1-2-32 etc]# more shard1.conf
  2. logpath=/usr/local/mongodb/logs/mongo_shard1.log
  3. logappend=true # need logrotae scripts
  4. fork=true
  5. journal=true
  6. port=27019
  7. dbpath=/usr/local/mongodb/shard1
  8. pidfilepath=/usr/local/mongodb/logs/mongo_shard1.pid
  9. bind_ip=10.1.2.32
  10. wiredTigerCacheSizeGB=4


单节点升级为复制集

  1. 1.关闭mongo
  2. 2.配置文件加上复制通道replSet=shard1
  3. 3.重启
  4. 4.其它节点也以replSet=shard1的方式启动
  5. 5.搭建复制集群
  6. 参考:http://www.mongoing.com/docs/tutorial/convert-standalone-to-replica-set.html


复制集升级为分片加复制集
  1. 1 重启second节点以--shardsvr模式启动,在配置文件加上shardsvr=true就好了

  1. 2.主节点降级
  2. rs.stepDown()

  1. 3.重启主节点以shardsvr模式

  1. 4.搭建config的复制集模式

  1. 5.所有节点启动mongos


  2. 6.主节点mongos添加shard1的分片集群
  3. db.runCommand({ addshard: 'shard1/10.1.2.68:27019,10.1.2.32:27019,10.1.2.175:27019'})


  4. 7.新的分片搭建复制集


  1. 8.主节点mongos加上新的分片集群
  2. db.runCommand({ addshard: 'newshard/10.1.2.32:27018,10.1.2.68:27018,10.1.2.175:27018'})


  3. 9.开启目标db可分片
  4. mongos> sh.enableSharding( "test" )


  5. 10.将目标文档进行分片
  6. mongos> db.test_collection.createIndex( { number : 1 } )
  7. mongos> sh.shardCollection( "test.test_collection", { "number" : 1 } )

主体流程如上,具体可参考官方文档:
http://www.mongoing.com/docs/tutorial/convert-replica-set-to-replicated-shard-cluster.html


上面的78步骤可以在9.10步骤之后的,先将数据shard到一个节点,然后新加节点,然后在平衡数据就好了


添加分片报错的解决办法
  1. #重新添加刚才删除的分片,将遇到以下错误,提示test数据库已经存在
  2. db.runCommand({addShard:"172.16.127.130:27017"})
  3. {
  4.   "code" : 96,
  5.   "ok" : 0,
  6.   "errmsg" : "can't add shard '172.16.127.130:27017'
  7.   "because a local database 'test' exists in another shard0001"
  8. }
  9. #这时,就需要通过MongoDB shell连接到shard01实例,删除test数据库,然后重新添加
  10. use test
  11. db.dropDatabase()
  12. #回到mongos节点
  13. use admin
  14. mongos> sh.addShard('172.16.127.130:27017')
  15. {
  16.         "code" : 11000,
  17.         "ok" : 0,
  18.         "errmsg" : "E11000 duplicate key error collection:
  19.             "admin.system.version index: _id_ dup key: { : \"shardIdentity\" }"
  20. }
  21. #这时候再连接到shard01节点,删除admin.system.version集合中记录
  22. db.system.version.remove({})
  23. db.system.version.remove({"_id":"shardIdentity"})
  24. WriteResult({
  25.         "nRemoved" : 0,
  26.         "writeError" : {
  27.                 "code" : 40070,
  28.                 "errmsg" : "cannot delete shardIdentity document while in --shardsvr mode"
  29.         }
  30. })
  31. #删除时报错,意思是说不能在分片模式下删除这张表中的这条记录,然后我们关闭shard01,然后以非shardsvr的方式启动,删除这条记录后,再以shardsvr方式启动
  32. db.shutdownServer()
  33. mongodb-3.4.4/bin/mongod -f mongodb-3.4.4/conf/mongod.conf
  34. use admin
  35. db.system.version.remove({})
  36. db.shutdownServer()
  37. mongodb-3.4.4/bin/mongod -f mongodb-3.4.4/conf/mongod.conf --shardsvr
  38. #最后添加之前被删除的分片shard01,大功告成
  39. db.runCommand({addShard:"172.16.127.130:27017"})
  40. { "shardAdded" : "shard0002", "ok" : 1 }



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-2154616/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1126890