1.单节点配置如下
-
[root@ip-10-1-2-32 etc]# more shard1.conf
-
logpath=/usr/local/mongodb/logs/mongo_shard1.log
-
logappend=true # need logrotae scripts
-
fork=true
-
journal=true
-
port=27019
-
dbpath=/usr/local/mongodb/shard1
-
pidfilepath=/usr/local/mongodb/logs/mongo_shard1.pid
-
bind_ip=10.1.2.32
-
wiredTigerCacheSizeGB=4
单节点升级为复制集
-
1.关闭mongo
-
2.配置文件加上复制通道replSet=shard1
-
3.重启
-
4.其它节点也以replSet=shard1的方式启动
-
5.搭建复制集群
-
参考:http://www.mongoing.com/docs/tutorial/convert-standalone-to-replica-set.html
复制集升级为分片加复制集
-
1 重启second节点以--shardsvr模式启动,在配置文件加上shardsvr=true就好了
-
2.主节点降级
-
rs.stepDown()
-
3.重启主节点以shardsvr模式
-
4.搭建config的复制集模式
-
5.所有节点启动mongos
-
-
-
6.主节点mongos添加shard1的分片集群
-
db.runCommand({ addshard: 'shard1/10.1.2.68:27019,10.1.2.32:27019,10.1.2.175:27019'})
-
-
-
7.新的分片搭建复制集
-
-
8.主节点mongos加上新的分片集群
-
db.runCommand({ addshard: 'newshard/10.1.2.32:27018,10.1.2.68:27018,10.1.2.175:27018'})
-
-
-
9.开启目标db可分片
-
mongos> sh.enableSharding( "test" )
-
-
-
10.将目标文档进行分片
-
mongos> db.test_collection.createIndex( { number : 1 } )
-
mongos> sh.shardCollection( "test.test_collection", { "number" : 1 } )
添加分片报错的解决办法
-
#重新添加刚才删除的分片,将遇到以下错误,提示test数据库已经存在
-
db.runCommand({addShard:"172.16.127.130:27017"})
-
{
-
"code" : 96,
-
"ok" : 0,
-
"errmsg" : "can't add shard '172.16.127.130:27017'
-
"because a local database 'test' exists in another shard0001"
-
}
-
#这时,就需要通过MongoDB shell连接到shard01实例,删除test数据库,然后重新添加
-
use test
-
db.dropDatabase()
-
#回到mongos节点
-
use admin
-
mongos> sh.addShard('172.16.127.130:27017')
-
{
-
"code" : 11000,
-
"ok" : 0,
-
"errmsg" : "E11000 duplicate key error collection:
-
"admin.system.version index: _id_ dup key: { : \"shardIdentity\" }"
-
}
-
#这时候再连接到shard01节点,删除admin.system.version集合中记录
-
db.system.version.remove({})
-
db.system.version.remove({"_id":"shardIdentity"})
-
WriteResult({
-
"nRemoved" : 0,
-
"writeError" : {
-
"code" : 40070,
-
"errmsg" : "cannot delete shardIdentity document while in --shardsvr mode"
-
}
-
})
-
#删除时报错,意思是说不能在分片模式下删除这张表中的这条记录,然后我们关闭shard01,然后以非shardsvr的方式启动,删除这条记录后,再以shardsvr方式启动
-
db.shutdownServer()
-
mongodb-3.4.4/bin/mongod -f mongodb-3.4.4/conf/mongod.conf
-
use admin
-
db.system.version.remove({})
-
db.shutdownServer()
-
mongodb-3.4.4/bin/mongod -f mongodb-3.4.4/conf/mongod.conf --shardsvr
-
#最后添加之前被删除的分片shard01,大功告成
-
db.runCommand({addShard:"172.16.127.130:27017"})
-
{ "shardAdded" : "shard0002", "ok" : 1 }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-2154616/
,如需转载,请注明出处,否则将追究法律责任。