Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the shard key

I Know that impossible to change shard key. But, when I set incorrect shard key, How to change that?

like image 429
Bill Avatar asked Jul 08 '11 09:07

Bill


2 Answers

dump the collection you sharded.. import it again.. set the new shard key.

like image 159
Mike Avatar answered Sep 21 '22 20:09

Mike


dump collection

mongodump --host <hostname> --port <port> --collection <collection_name> --db <db_name>

open mongos and drop database or collection(If you had more than 1 collection)

mongo --host <hostname> --port <port>
show dbs
use <db_name>
db.dropDatabase() //it's only if you hade ONE database in db
exit

import database

mongorestore --host <hostname> --port <port> --collection <collection_name> --db <db_name> <path to <collection_name>.bson>

open mongos and shard

mongo --host <hostname> --port <port>
sh.status() (only to understand what is happen)
sh.enableSharding("<db_name>")
sh.shardCollection("<db_name>.<collection_name>",<shard key>,<option>)

something like that p.s. You must had index in collection for shard key. Search: "ensureIndex()"

like image 43
Yuriy Kosovskiy Avatar answered Sep 22 '22 20:09

Yuriy Kosovskiy