Centos6.5 Building MongoDB Shards

Source: Internet
Author: User

650) this.width=650; "title=" 1e3136050d3c8e88ff77577967c7beb1 "style=" border-left-0px; border-right-width:0px; border-bottom-width:0px; border-top-width:0px "border=" 0 "alt=" 1e3136050d3c8e88ff77577967c7beb1 "src=" Http://s3.51cto.com/wyfs02/M02/83/AC /wkiom1d6hoeq-7d2aaa2w7tvkgq339.png "height=" 272 "/>

Https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tgz

Is the version I used

1. Prepare 3 Examples:

10.0.0.13

10.0.0.18

10.0.0.19

2, planning 5 components corresponding port number, because a machine needs to deploy MONGOs, config server, shard1, Shard2, shard3, so need to use port to differentiate.

This port can be freely defined, in this article MONGOs is 3717, config server is 3710, Shard1 is 3711, Shard2 is 3712, Shard3 is 3713

3. Start the configuration server on each server separately.

/opt/mongodb-cluster/bin/mongod--configsvr--dbpath/opt/mongodb-cluster/config/data--port 3710--logpath/opt/ Mongodb-cluster/config/var/config.log--fork

4. Start the MONGOs server separately on each server.

/opt/mongodb-cluster/bin/mongos--configdb 10.0.0.13:3710,10.0.0.18:3710,10.0.0.19:3710--port 3717--logpath/opt/ Mongodb-cluster/mongos/var/mongos.log--fork

5. Configure the replica set for each shard.

#在每个机器里分别设置分片1服务器及副本集shard1

/opt/mongodb-cluster/bin/mongod--shardsvr--replset shard1--port 3711--dbpath/opt/mongodb-cluster/shard1/data-- Logpath/opt/mongodb-cluster/shard1/var/shard1.log--fork

#在每个机器里分别设置分片2服务器及副本集shard2

/opt/mongodb-cluster/bin/mongod--shardsvr--replset shard2--port 3712--dbpath/opt/mongodb-cluster/shard2/data-- Logpath/opt/mongodb-cluster/shard2/var/shard2.log--fork

#在每个机器里分别设置分片3服务器及副本集shard3

/opt/mongodb-cluster/bin/mongod--shardsvr--replset shard3--port 3713--dbpath/opt/mongodb-cluster/shard3/data-- Logpath/opt/mongodb-cluster/shard3/var/shard3.log--fork

#设置第一个分片副本集

/opt/mongodb-cluster/bin/mongo 127.0.0.1:3711

#使用admin数据库

Use admin

#定义副本集配置

Config = {_id: "Shard1", members:[

{_id:0,host: "10.0.0.13:3711"},

{_id:1,host: "10.0.0.18:3711"},

{_id:2,host: "10.0.0.19:3711", arbiteronly:true}

]

}

#初始化副本集配置

Rs.initiate (config);

#设置第二个分片副本集

/opt/mongodb-cluster/bin/mongo 127.0.0.1:3712

#使用admin数据库

Use admin

#定义副本集配置

Config = {_id: "Shard2", members:[

{_id:0,host: "10.0.0.13:3712", arbiteronly:true},

{_id:1,host: "10.0.0.18:3712"},

{_id:2,host: "10.0.0.19:3712"}

]

}

#初始化副本集配置

Rs.initiate (config);

#设置第三个分片副本集

/opt/mongodb-cluster/bin/mongo 127.0.0.1:3713

#使用admin数据库

Use admin

#定义副本集配置

Config = {_id: "Shard3", members:[

{_id:0,host: "10.0.0.13:3713"},

{_id:1,host: "10.0.0.18:3713", arbiteronly:true},

{_id:2,host: "10.0.0.19:3713"}

]

}

#初始化副本集配置

Rs.initiate (config);

6, currently built MongoDB configuration server, routing server, each shard server, but the application to connect to the MONGOs routing server does not use the Shard mechanism, also need to set the Shard configuration in the program, let the Shard take effect.

#连接到mongos

/opt/mongodb-cluster/bin/mongo 127.0.0.1:3717

#使用admin数据库

User admin

#串联路由服务器与分配副本集1

Db.runcommand ({addshard: "shard1/10.0.0.13:3711,10.0.0.18:3711,10.0.0.19:3711"});

If Shard is a single server, join with a command such as Db.runcommand ({addshard: "[:]"}), if Shard is a replica set, with Db.runcommand ({addshard: "replicasetname/[:p ort][,serverhostname2[:p ORT],...] "}); Such a format is represented.

#串联路由服务器与分配副本集2

Db.runcommand ({addshard: "shard2/10.0.0.13:3712,10.0.0.18:3712,10.0.0.19:3712"});

#串联路由服务器与分配副本集3

Db.runcommand ({addshard: "shard3/10.0.0.13:3713,10.0.0.18:3713,10.0.0.19:3713"});

#查看分片服务器的配置

Db.runcommand ({listshards:1});

7, the current configuration services, routing services, Shard services, replica set services have been concatenated, but our goal is to insert data, data can be automatically fragmented, so a little bit, a little bit ... Connected on MONGOs, ready to have the specified database, specified collection shards in effect.

#指定testdb分片生效

Db.runcommand ({enablesharding: "TestDB"});

#指定数据库里需要分片的集合和片键

Db.runcommand ({shardcollection: "Testdb.table1", key: {id:1}})

    • We set the Table1 table for TestDB to be fragmented, automatically sharding to shard1 according to the ID, shard2,shard3 above. This is set because not all MONGODB databases and tables require sharding!

8. Test the result of the Shard configuration.

#连接mongos服务器

/opt/mongodb-cluster/bin/mongo 127.0.0.1:3717

#使用testdb

Use TestDB;

#插入测试数据

for (var i = 1; I <= 100000; i++)

Db.table1.save ({id:i, "test1": "Testval1"});

#查看分片情况如下, some irrelevant information is omitted.

Db.table1.stats ();

Here is my own for convenience, write the Startup management script, attached to the back.

[email protected] sbin]# LL
Total 28
Drwxr-xr-x 2 root root 4096 Jul 4 15:35 Bak
-rwxrwxr-x 1 Mongod mongod 3945 May 18:51 config
-rwxrwxr-x 1 mongod mongod 3945 18:51 MONGOs
-rwxrwxr-x 1 mongod mongod 1181 19:04 Node_mongo
-rwxrwxr-x 1 mongod mongod 3945 18:51 shard1
-rwxrwxr-x 1 mongod mongod 3945 18:51 shard2
-rwxrwxr-x 1 mongod mongod 3945 18:51 shard3
[Email protected] sbin]# pwd
/opt/mongodb-cluster/sbin
[Email protected] sbin]#

Attachment is a node installation profile: http://down.51cto.com/data/2226910

Centos6.5 Building MongoDB Shards

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.