MongoDB auto sharding

Source: Internet
Author: User
Tags install mongodb database sharding

MongoDB has been widely used as a NoSQL Database in recent years. More and more enterprises are trying to use MongoDB instead of the original Database. MongoDB also performs well in clusters, sharding, and replication. I will introduce various MongoDB deployment experiments.

Article 3 MongoDB auto sharding: six parts
1. initialize the file directory
2. Start the shard Node
3. Configure the shard Node
4. Insert a Data Partition Experiment
5. Delete the primary part.
6. Reset the primary part.
 
System Environment Introduction:
 
Ubuntu 12.04. LTS 64bit Server
 
1. initialize the file directory
 
Create directory
Config1, config2, and config3 are configuration nodes.
Shard1, shard2, and shard3 are shard nodes.
 

~ Pwd
/Home/conan/dbs
~ Mkdir config1 config2 config3 shard1 shard2 shard3
Conan @ u1 :~ /Dbs $ ls-l
Drwxrwxr-x 3 conan 4096 May 31 11: 27 config1
Drwxrwxr-x 3 conan 4096 May 31 11: 27 config2
Drwxrwxr-x 3 conan 4096 May 31 11: 27 config3
Drwxrwxr-x 3 conan 4096 May 31 :28 shard1
Drwxrwxr-x 3 conan 4096 May 31 :29 shard2
Drwxrwxr-x 3 conan 4096 May 31 :29 shard3
 

2. Start the shard Node
 


Start the config Node
 
~ Mongod -- dbpath/home/conan/dbs/config1 -- port 20001 -- nojournal -- fork -- logpath/home/conan/dbs/config1.log
~ Mongod -- dbpath/home/conan/dbs/config2 -- port 20002 -- nojournal -- fork -- logpath/home/conan/dbs/config2.log
~ Mongod -- dbpath/home/conan/dbs/config3 -- port 20003 -- nojournal -- fork -- logpath/home/conan/dbs/config3.log
 
Start mongos nodes
 
~ Mongos -- configdb localhost: 20001, localhost: 20002, localhost: 20003 -- port 30001 -- fork -- logpath/home/conan/dbs/mongos1.log
~ Mongos -- configdb localhost: 20001, localhost: 20002, localhost: 20003 -- port 30002 -- fork -- logpath/home/conan/dbs/ipvs2.log
 
Start the shard Node
 
~ Mongod -- dbpath/home/conan/dbs/shard1 -- port 10001 -- nojournal -- fork -- logpath/home/conan/dbs/shard1.log
~ Mongod -- dbpath/home/conan/dbs/shard2 -- port 10002 -- nojournal -- fork -- logpath/home/conan/dbs/shard2.log
~ Mongod -- dbpath/home/conan/dbs/shard3 -- port 10003 -- nojournal -- fork -- logpath/home/conan/dbs/shard3.log
 
View port
 
~ Netstat-nlt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
Tcp 0 0 0.0.0.0: 21003 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 10001 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 30001 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 10002 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 30002 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 10003 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 22 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 11001 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 31001 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 11002 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 31002 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 11003 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 20001 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 20002 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 20003 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 21001 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 21002 0.0.0.0: * LISTEN
Tcp6 0 0: 22: * LISTEN
 

3. Configure the shard Node
 
Connect to ipvs1 and add shards to mongos:
 
~ Mongo localhost: 30001/admin
MongoDB shell version: 2.4.3
Connecting to: localhost: 30001/admin
 
Mongos> db. runCommand ({addshard: "localhost: 10001", allowLocal: true })
{"ShardAdded": "shard0000", "OK": 1}
Mongos> db. runCommand ({addshard: "localhost: 10002", allowLocal: true })
{"ShardAdded": "shard0001", "OK": 1}
Mongos> db. runCommand ({addshard: "localhost: 10003", allowLocal: true })
{"ShardAdded": "shard0002", "OK": 1}
 


Incorrect syntax: (MongoDB2.0.4 does not support this syntax)
 
Mongos> db. runCommand ({addshard: "shard1/localhost: 10001, localhost: 10002", name: "s1", allowLocal: true })
{
"OK": 0,
"Errmsg": "couldn 'tconnect to new shard socket exception [CONNECT_ERROR] for shard1/localhost: 10001, localhost: 10002"
}
 

View part information
 
Mongos> db. runCommand ({listshards: 1 })
{
"Shards ":[
{
"_ Id": "shard0000 ",
"Host": "localhost: 10001"
},
{
"_ Id": "shard0001 ",
"Host": "localhost: 10002"
},
{
"_ Id": "shard0002 ",
"Host": "localhost: 10003"
}
],
"OK": 1
}
 
Enable database sharding: fensme
 
Mongos> db. runCommand ({"enablesharding": "fensme "})
{"OK": 1}
 

Note: Once a database is enabled, mongos places different data sets in the database on different shards. Only a dataset is partitioned. Otherwise, all data in a dataset is placed in one partition.
 
Enable dataset sharding: fensme. users
 
Mongos> db. runCommand ({"shardcollection": "fensme. users", "key": {"_ id": 1, "uid": 1 }})
{"Collectionsharded": "fensme. users", "OK": 1}
 

View shard status
 
Mongos> db. printShardingStatus ()
--- Sharding Status ---
Sharding version :{
"_ Id": 1,
"Version": 3,
"MinCompatibleVersion": 3,
"CurrentVersion": 4,
"ClusterId": ObjectId ("51a8d3287034310ad2f6a94e ")
}
Shards:
{"_ Id": "shard0000", "host": "localhost: 10001 "}
{"_ Id": "shard0001", "host": "localhost: 10002 "}
{"_ Id": "shard0002", "host": "localhost: 10003 "}
Databases:
{"_ Id": "admin", "partitioned": false, "primary": "config "}
{"_ Id": "fensme", "partitioned": true, "primary": "shard0000 "}
Fensme. users
Shard key: {"_ id": 1, "uid": 1}
Chunks:
Shard0000 1
{"_ Id": {"$ minKey": 1}, "uid": {"$ minKey": 1 }}-- >>{ "_ id ": {"$ maxKey": 1}, "uid": {"$ maxKey": 1 }}on: shard0000 {"t": 1, "I": 0}
 
The fensme Database supports shard, the primary shard is shard0000, and the corresponding host is localhost: 10001.

MongoDB details: click here
MongoDB: click here

MongoDB backup and recovery

CentOS compilation and installation of MongoDB

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

How to create a new database and set in MongoDB

MongoDB beginners must read (both concepts and practices)

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

  • 1
  • 2
  • Next Page

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.