MongoDB Shard Set Building

Source: Internet
Author: User

The topology diagram is as follows:

You can see that there are four components: MONGOs, config server, shard, replica set.

MONGOs, the entrance to the database cluster request, all requests are coordinated through MONGOs, do not need to add a route selector in the application, MONGOs itself is a request distribution center, it is responsible for the corresponding data request request forwarded to the corresponding Shard server. In a production environment there is usually more mongos as the entrance to the request, preventing one of the other MongoDB requests from being hung out of operation.

Config server, as its name implies, configures the configuration of servers, storing all database meta-information (routing, sharding). The mongos itself does not have a physical storage Shard server and data routing information, but is cached in memory, and the configuration server actually stores the data. MONGOs the first boot or shutdown reboot will load configuration information from config server, and if configuration server information changes will notify all MONGOs to update their status, so that MONGOs can continue to route accurately. In a production environment there are usually multiple config server configuration servers, because it stores the metadata of the Shard route, this can not be lost! Even if you hang one of them, as long as there is inventory, the MongoDB cluster will not hang off.

Shard, this is the legendary shard. The above mentioned a machine even if the ability to have a large ceiling, as the military war, a person can drink blood bottle also can not spell the other one's division. As the saying goes Three stooges the top of Zhuge Liang, this time the strength of the team is highlighted. In the Internet, too, a common machine can do more than one machine to do.

The environment is configured as follows:
Host IP Services and ports
Server A 192.168.10.88
Mongod shard1:27017
Mongod shard2:27018
Mongod shard3:27019
Mongod config1:20000
mongs1:30000

Server B 192.168.10.89
Mongod shard1:27017
Mongod shard2:27018
Mongod shard3:27019
Mongod config2:20000
mongs2:30000

Server C 192.168.10.90
Mongod shard1:27017
Mongod shard2:27018
Mongod shard3:27019
Mongod config3:20000
mongs3:30000
1. Create a Data Catalog
On server A:
Mkdir-p/data0/mongodbdata/shard1
Mkdir-p/data0/mongodbdata/shard2
Mkdir-p/data0/mongodbdata/shard3
Mkdir-p/data0/mongodbdata/config
On server B:
Mkdir-p/data0/mongodbdata/shard1
Mkdir-p/data0/mongodbdata/shard2
Mkdir-p/data0/mongodbdata/shard3
Mkdir-p/data0/mongodbdata/config
On server C:
Mkdir-p/data0/mongodbdata/shard1
Mkdir-p/data0/mongodbdata/shard2
Mkdir-p/data0/mongodbdata/shard3
Mkdir-p/data0/mongodbdata/config
2. Configure Replica Sets
2.1 Configuring the replica sets used by the Shard1
On server A:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard1--port 27017--dbpath/data0/mongodbdata/shard1--logpath/ Data0/mongodbdata/shard1/shard1.log--logappend--fork
On server B:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard1--port 27017--dbpath/data0/mongodbdata/shard1--logpath/ Data0/mongodbdata/shard1/shard1.log--logappend--fork
[Email protected] bin]#
On server C:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard1--port 27017--dbpath/data0/mongodbdata/shard1--logpath/ Data0/mongodbdata/shard1/shard1.log--logappend--fork
[Email protected] bin]#
Using MONGO to connect the 27017 port of the Mongod to one of the machines, initialize replica sets "Shard1" and execute:
./mongo--port 27017
MongoDB Shell version:2.6.6
Connecting To:127.0.0.1:27017/test
> config = {_id: ' Shard1 ', Members: [
{_id:0, Host: ' 192.168.10.88:27017 '},
{_id:1, Host: ' 192.168.10.89:27017 '},
{_id:2, Host: ' 192.168.10.90:27017 '}]
}

> rs.initiate (config)
{
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}
2.2 Configuring the replica sets used by the Shard2
On server A:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard2--port 27018--dbpath/data0/mongodbdata/shard2--logpath/ Data0/mongodbdata/shard2/shard2.log--logappend--fork
[Email protected] bin]#
On server B:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard2--port 27018--dbpath/data0/mongodbdata/shard2--logpath/ Data0/mongodbdata/shard2/shard2.log--logappend--fork
[Email protected] bin]#
On server C:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard2--port 27018--dbpath/data0/mongodbdata/shard2--logpath/ Data0/mongodbdata/shard2/shard2.log--logappend--fork
[Email protected] bin]#
Using MONGO to connect the 27018 port of the Mongod to one of the machines, initialize replica sets "Shard2" and execute:
./mongo--port 27018
MongoDB Shell version:2.6.6
Connecting To:127.0.0.1:27018/test
> config = {_id: ' Shard2 ', Members: [
{_id:0, Host: ' 192.168.10.88:27018 '},
{_id:1, Host: ' 192.168.10.89:27018 '},
{_id:2, Host: ' 192.168.10.90:27018 '}]
}
> rs.initiate (config)
{
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}
2.3 Configuring the replica sets used by the Shard3
On server A:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard3--port 27019--dbpath/data0/mongodbdata/shard3--logpath/ Data0/mongodbdata/shard3/shard3.log--logappend--fork
[Email protected] bin]#
On server B:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard3--port 27019--dbpath/data0/mongodbdata/shard3--logpath/ Data0/mongodbdata/shard3/shard3.log--logappend--fork
[Email protected] bin]#
On server C:
/usr/local/mongodb/bin/mongod--shardsvr--replset shard3--port 27019--dbpath/data0/mongodbdata/shard3--logpath/ Data0/mongodbdata/shard3/shard3.log--logappend--fork
[Email protected] bin]#
Using MONGO to connect the 27019 port of the Mongod to one of the machines, initialize replica sets "Shard3" and execute:
./mongo--port 27019
MongoDB Shell version:2.6.6
Connecting To:127.0.0.1:27019/test
> config = {_id: ' Shard3 ', Members: [
{_id:0, Host: ' 192.168.10.88:27019 '},
{_id:1, Host: ' 192.168.10.89:27019 '},
{_id:2, Host: ' 192.168.10.90:27019 '}]
}
> rs.initiate (config)
{
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}

3. Configuring 3 Config Servers
Execute on server A, B, C:
/usr/local/mongodb/bin/mongod--configsvr--dbpath/data0/mongodbdata/config--port 20000--logpath/data0/ Mongodbdata/config/config.log--logappend--fork
4. Configure 3 route Process
Execute on server A, B, C:
/usr/local/mongodb/bin/mongos--configdb 192.168.10.88:20000,192.168.10.89:20000,192.168.10.90:20000--port 30000- -chunksize 1--logpath/data0/mongodbdata/mongos.log--logappend--fork
5. Configure Shard Cluster
Connect to the MONGOs process on port 30000 of one of the machines, and switch to the admin database to do the following configuration
./mongo--port 30000
MongoDB Shell version:2.6.6
Connecting To:127.0.0.1:30000/test
> Use admin
Switched to DB admin
>db.runcommand ({addshard: "shard1/192.168.10.88:27017,192.168.10.89:27017,192.168.10.90:27017"});
{"shardadded": "Shard1", "OK": 1}
>db.runcommand ({addshard: "shard2/192.168.10.88:27018,192.168.10.89:27018,192.168.10.90:27018"});
{"shardadded": "Shard2", "OK": 1}
>db.runcommand ({addshard: "shard3/192.168.10.88:27019,192.168.10.89:27019,192.168.10.90:27019"});
{"shardadded": "Shard3", "OK": 1}
Activating shards for databases and collections
Db.runcommand ({enablesharding: "Test"})
Db.runcommand ({shardcollection: "Test.users", key: {_id:1}})--->id as tablet key


Use test
for (Var i=1;i<=200000;i++) Db.users.insert ({id:i,addr_1: "Beijing", Addr_2: "Shanghai"});


Db.users.stats ()
{
"sharded": true,
"Systemflags": 1,
"UserFlags": 1,
"NS": "Test.users",
"Count":200000,
"Numextents": 18,
"Size": 22400000,
"Storagesize": 33546240,
"Totalindexsize": 6532624,
"Indexsizes": {
"_id_": 6532624
},
"Avgobjsize": 112,
"Nindexes": 1,
"Nchunks": 24,
"Shards": {
"Shard1": {
"NS": "Test.users",
"Count":60928,
"Size": 6823936,
"Avgobjsize": 112,
"Storagesize": 11182080,
"Numextents": 6,
"Nindexes": 1,
"Lastextentsize": 8388608,
"Paddingfactor": 1,
"Systemflags": 1,
"UserFlags": 1,
"Totalindexsize": 1986768,
"Indexsizes": {
"_id_": 1986768
},
"OK": 1
},
"Shard2": {
"NS": "Test.users",
"Count":73738,
"Size": 8258656,
"Avgobjsize": 112,
"Storagesize": 11182080,
"Numextents": 6,
"Nindexes": 1,
"Lastextentsize": 8388608,
"Paddingfactor": 1,
"Systemflags": 1,
"UserFlags": 1,
"Totalindexsize": 2411920,
"Indexsizes": {
"_id_": 2411920
},
"OK": 1
},
"Shard3": {
"NS": "Test.users",
"Count":65334,
"Size": 7317408,
"Avgobjsize": 112,
"Storagesize": 11182080,
"Numextents": 6,
"Nindexes": 1,
"Lastextentsize": 8388608,
"Paddingfactor": 1,
"Systemflags": 1,
"UserFlags": 1,
"Totalindexsize": 2133936,
"Indexsizes": {
"_id_": 2133936
},
"OK": 1
}
},
"OK": 1
}
Can see sharding build success, not too much seems not very uniform, so this shard is very fastidious, follow-up and further discussion.

MongoDB Shard Set Build

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.