Schlagwort-Archiv: mongodb

MongoDB with auto-sharding, routers and a configuration server

This mini howto will take you through the steps of using MongoDB and it’s auto-sharding feature. Sharding on MongoDB means the distribution of chunks of data between multiple server instances. It’s great if your server infrastructure outgrows or if you want automatic failover (with the usage of replica).

Step 1 – starting the configuration server

creating a configuration directory for mongo

mkdir -p ~/mongo/config

starting the configuration server on port 20000

mongod --dbpath ~/mongo/config/ --port 20000 --configsvr

Step 2 – the routing server

The routing server is the server to which all the applications servers (rails or whatever) connect to communicate with mongodb. The routing server itself connects to the configuration server to get its configuration.

 mongos --port 30000 --configdb localhost:20000

Step 3 – the shards

Now let’s create a new shard with it’s own data directory

mkdir -p ~/mongo/shard1_germany
mongod --dbpath ~/mongo/shard1_germany --port 10000

After the creation we tell the router to add the new shard. Please notice that the option allowLocal is just for development / testing environments. MongoDB doesn’t let you create clusters on localhost, so we need this option.

mongo localhost:30000/admin
db.runCommand({addshard : "localhost:10000", allowLocal : true})

Step 4 – creating a database

Connect to the router and switch to the database polyvision

mongo localhost:30000/admin
use polyvision

Please remember that the database is not saved / created yet, ’cause it has no data yet. So, let’s create a simple database entry.

	db.users.save( { username:"bierbrauer" } )
	db.users.find()

the result:

	{ "_id" : ObjectId("4e7825e97501770a31110f9f"), "username" : "bierbrauer" }

Using show dbs, you can see the database and it’s size

the result:

config	0.1875GB
polyvision	0.203125GB

switching back to the admin database

use admin

enable sharding for the polyvision database

db.runCommand({"enablesharding" : "polyvision"})

so if you want to shard the users collection of your polyvision database, use this command

db.runCommand( { shardcollection : "polyvision.users", key : {_id: 1} });

Now the collection gets sharded amoung all the sharding servers using it’s document id for the chunk algorithm. You may select others keys than the document id, for example the username. The call would look like this:

db.runCommand( { shardcollection : "polyvision.users", key : {username: 1} });


Step 5 – getting sharding information

To get information about our shards, connect to the router and enter the following command.

db.printShardingStatus();