MongoDB sharding Cluster Step by step

Source: Internet
Author: User
Tags mongodb sharding

This article tells the detailed steps of MongoDB's sharding Cluster, and does not make any mistakes theoretically.

On the inside of the parameters, variables, and settings, not a lot of use, only the key to some of the other can refer to the official MongoDB documents.

The latter himself Google,baidu, here do not want to speak more. The same nonsense does not say much, direct serving.

Oh, yes. sharding cluster inside must understand the three concepts, DataNode, Configserver, Router.

OK, the configuration is officially started:

1, configure Shard Data Node, which we just said Replica Set

1) Write your own mongo_sharding_datanode_1.conf

The contents of the file:

Bind_ip=localhost
port=27022
Dbpath=d:\mongodb\data\dbnode1
Logpath=d:\mongodb\log\node1.log
Logappend=true
Replset=testsh1
Shardsvr=true

2) The same file, get rid of the value of Bind_ip,port,dbpath,logpath, made mongo_sharding_datanode_2.conf

The two libraries that save the data have a primary, a secondary.

3) There is also a arbitrary in the replica set, so then write a third file: mongo_sharding_datanode_arbit.conf

The content is the same, but Bind_ip,port,dbpath,logpath is not the same.

The above configuration items, and parameters are unclear, or want to add more parameters, it is best to use mongod-h to see, the inside explanation is very clear.

4) Three file number followed by using Mongod.exe-f mongo_sharding_datanode_2.conf to start three Mongod

For example, mine is:

D:\mongodb\server\3.2\bin>
D:\mongodb\server\3.2\bin>mongod.exe-f. \conf\mongo_sharding_datanode_1.conf

5) after three servers (three Mongod) have been set up, configuring this three server as a Replica setting will allow them to become one of our shard data node.

Just log in to one of the three servers, like I'm logged in at 27022,

Mongo.exe localhost:27022

Use admin

CFG = {_id: ' Testsh1 ', members:[{_id:0,host: ' localhost:27022 ', priority:2},{_id:1,host: ' localhost:27023 ', Priority:1},{_id:2,host: ' localhost:27024 ', Arbiteronly:true}]}
Rs.initiate (CFG)

With the above command, the three servers are configured as a replica Set, which is a shard Data Node,

Now we have a Shard Data Node.

If there is an error in the middle, do not be afraid, you can find a remedy, such as Rs.remove ("localhist:27023") rs.reconfig (CFG) and so on, so that you can learn the plowing.

The most afraid of is to destroy again, but so do their own practice can, the company environment or production environment, to please absolutely careful!!

2, Configuration Configserver

1) Write your own conf file mongo_configserver_1.conf content:

Bind_ip=localhost
port=27019
Dbpath=d:\mongodb\data\confserver1
Logpath=d:\mongodb\log\confserver1.log
Logappend=true
Configsvr=true

Pay attention to the configsvr=true here.

Write the other two or more conf files in the same way.

2) Start Config Server

Mongod.exe-f mongo_configserver_1.conf

For example, the way I start:

D:\mongodb\server\3.2\bin>
D:\mongodb\server\3.2\bin>mongod.exe-f. \conf\mongo_configserver_2.conf

Now that Config Server has it, Shard DataNode has it, and all the preparation is there.

Our final role: MONGOs Router should be on the pitch.

3, Configuration MONGOs

Similarly, we write the MONGOs configuration file mongos.conf

Bind_ip=localhost
port=27030
#dbpath =d:\mongodb\data\db ####### Note that it does not need to dbpath, you can view Mongos-h view the MONGOs parameter list
Logpath=d:\mongodb\log\mongos.log
Logappend=true
configdb=localhost:27019,localhost:27020,localhost:27021 # # # #看一下这个三个ip:p ort separated by commas, which is the address of three configserver.

OK all the static, need to write to the file configuration is complete, start MONGOs

Mongos.exe-f mongos.conf

For example, my startup is:

D:\mongodb\server\3.2\bin>
D:\mongodb\server\3.2\bin>mongos.exe-f. \conf\mongos.conf

If you start MONGOs Yes, you should be able to get into mongos.

Use command: Mongo.exe localhost:27030 into MONGOs,

Enter MONGOs after the hint should be: mongos> such a son,

4, Configuration sharding

1) Join Shard

Use the command:mongos> sh.addshard ("testsh1/localhost:27022")

Remember the front of the Testsh1 bar, Testsh1 said replica set name, when the master node added to the Shard, will automatically find the set of the master, prepare, decision-making node.

So the above command is: TESTSH1 This replica set of the main node into the Shard, let shard own node and decision-making node.

You can use the command:mongos> db.runcommand ({listshards:1}); take a look at the added shards and the results should be similar:

Mongos>
Mongos> Db.runcommand ({listshards:1});
{
"Shards": [
{
"_id": "Testsh1",
"Host": "testsh1/localhost:27022,localhost:27024"
}
],
"OK": 1
}
Mongos>
Mongos>

2) Activate the database shard, using the command:

Mongos>
Mongos>
Mongos> Db.runcommand ({enablesharding: "test-db"});
{"OK": 1}
Mongos>

Activates the TEST-DB database shard.

By executing the above command, you can make the database cross Shard, and if you do not perform this step, the database will only be stored in one shard.

Once the database shard is activated, the different collection in the database will be stored on different shard, but a collection is still stored on the same shard, so that a single collection can be fragmented, and collection should be operated separately.

3) Activate collection Shard, say clearly, understand:

Use command:mongos> db.runcommand ({shardcollection: ' Test-db.usertest ', Key:{_id:1}});

Represents the Shard of the Usertest table that activates the TEST-DB database, using key is the column with the indexed _id.

After Setup, you can switch to test-db and see the status of the Usertest table.

Use the command:

Use test-db;

Db.usertest.stats ()

Because there are too many content, it is not listed here.

The article to which all the configurations have been turned out, this MONGO Shard Cluster has been configured to complete,

You can use the MONGOs to do it. such as add, delete, view and so on.

mongos> user test-db;

Mongos> Db.usertest.save ({name: ' xiaobing '});

Mongos> Db.usertest.find ();

And so on a series of operations.

OK, let's call it a day.

Endless, you still.

Haha, I'd like to know what to do if that MONGOs hangs up. Well, given a solution, in order to prevent a single point of failure, a mongos hung up how to do?

Look at the picture, not much to say:

Know how to do it, so there should be no single point of failure, perfect, perfect. Hey, hey.

MongoDB sharding Cluster Step by step

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.