MongoDB Shard Configuration

Source: Internet
Author: User


#本次配置时间为 2014-11-06 software version: mongodb-linux-x86_64-2.4.3.tgz

Linux:centos 5.9 64bit

##############################################################

This deployment of shards is only for the test environment: #

10.8.10.234 10.8.10.235 10.8.10.236 #

Shard1 Shard Master slaver arbiteronly #

Shard2 Shard Master slaver arbiteronly #

Shard3 Shard Master slaver arbiteronly #

#

###############################################################





#每个服务器配置:


Mkdir-p/data/mongodb/config/data

Mkdir-p/data/mongodb/config/log


Mkdir-p/data/mongodb/mongos/log


Mkdir-p/data/mongodb/shard1/data

Mkdir-p/data/mongodb/shard1/log


Mkdir-p/data/mongodb/shard2/data

Mkdir-p/data/mongodb/shard2/log


Mkdir-p/data/mongodb/shard3/data

Mkdir-p/data/mongodb/shard3/log


Tar-xvzf mongodb-linux-x86_64-2.4.3.tgz

MV Mongodb-linux-x86_64-2.4.3/data/mongodb/mongodb


#每个服务器配置服务:


/data/mongodb/mongodb/bin/mongod--configsvr--dbpath/data/mongodb/config/data--port 21000--logpath/data/mongodb/ Config/log/config.log--fork



#每台服务器 Start the Shard service:

/data/mongodb/mongodb/bin/mongod--shardsvr--replset shard1--port 22001--dbpath/data/mongodb/shard1/data--logpath /data/mongodb/shard1/log/shard1.log--fork--nojournal--oplogsize 10



/data/mongodb/mongodb/bin/mongod--shardsvr--replset shard2--port 22002--dbpath/data/mongodb/shard2/data--logpath /data/mongodb/shard2/log/shard2.log--fork--nojournal--oplogsize 10



/data/mongodb/mongodb/bin/mongod--shardsvr--replset shard3--port 22003--dbpath/data/mongodb/shard3/data--logpath /data/mongodb/shard3/log/shard3.log--fork--nojournal--oplogsize 10


# 1 or more servers start the MONGOs route, and the application connects to the routing port.

#mongos need to wait until the configuration files of 3 servers are started before they can be started, or error.

/data/mongodb/mongodb/bin/mongos--configdb 10.8.10.234:21000,10.8.10.235:21000,10.8.10.236:21000--port 20000-- Logpath/data/mongodb/mongos/log/mongos.log--fork



#################################

#登入任意服务器执行:


MONGO 127.0.0.1:22001

Use admin


Setting up Shards 1


>config = {_id: "Shard1", members:[

{_id:0,host: "10.8.10.234:22001", priority:1},

{_id:1,host: "10.8.10.235:22001", priority:1},

{_id:2,host: "10.8.10.236:22001", arbiteronly:true}

]

}


>rs.initiate (config);


#, Priority:n is set master priority can not be configured, it is recommended to put the master of the Shard on different servers, to achieve read and write pressure shunt

# Arbiteronly:true needs to be configured. After omitting, it becomes 1 master + 2 slaver does not match the scheme


############################


MONGO 127.0.0.1:22002

Use admin


Setting up Shards 2

>config = {_id: "Shard2", members:[

{_id:0,host: "10.8.10.234:22002", priority:1},

{_id:1,host: "10.8.10.235:22002", priority:1},

{_id:2,host: "10.8.10.236:22002", arbiteronly:true}

]

}


>rs.initiate (config);


#, Priority:n is set master priority can not be configured. It is recommended to place the master of the Shard on a different server for read-write pressure shunt

# Arbiteronly:true needs to be configured. When omitted, becomes 1master + 2 slaver inconsistent with the scheme


#############################


MONGO 127.0.0.1:22003

Use admin

Setting up Shards 3


>config = {_id: "Shard3", members:[

{_id:0,host: "10.8.10.234:22003", priority:1},

{_id:1,host: "10.8.10.235:22003", priority:1},

{_id:2,host: "10.8.10.236:22003", arbiteronly:true}

]

}



>rs.initiate (config);


#, Priority:n is set master priority can not be configured. It is recommended to place the master of the Shard on a different server for read-write pressure shunt

# Arbiteronly:true needs to be configured. When omitted, becomes 1master + 2 slaver inconsistent with the scheme


#########################


Connecting Mongs:bin/mongo 127.0.0.1:20000

#使用admin数据库 User admin

Serial routing server and allocation replica set 1


Db.runcommand ({addshard: "shard1/10.8.10.234:22001,10.8.10.235:22001,10.8.10.236:22001"});


Serial routing server and allocation replica set 2

Db.runcommand ({addshard: "shard2/10.8.10.234:22002,10.8.10.235:22002,10.8.10.236:22002"});


Serial routing server and allocation replica set 3

Db.runcommand ({addshard: "shard3/10.8.10.234:22003,10.8.10.235:22003,10.8.10.236:22003"});



#查看分片服务器的配置

Db.runcommand ({listshards:1});


Note: The quorum node results for each shard replica set are not listed to

##############################


# # #手动选定master备注:


Add priority when configuring shards to manually select Master


After the Shard is configured, log on to the primary node of the Shard using the following command to promote Members[1] to master.

Rs.status ();

Rs.conf ();

Cfg=rs.conf ();

Cfg.members[0].priority=1

cfg.members[1].priority=2

#cfg. members[2] The quorum machine does not have to be set

Rs.reconfig (CFG);



To create a data test:

#指定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!


Bin/mongo 127.0.0.1:20000

#使用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 ();

See data divided into 3 shards, the respective number of shards is: Shard1 "Count": 42183,shard2 "Count": 38937,shard3 "Count": 18880. It's been done! Not too much seems not very uniform, so this shard is very fastidious, follow-up and further discussion.



In order to fully utilize 3 server resources to alleviate disk IO, the above scenarios can be adapted as follows:

##############################################################

In a production environment, you can deploy the following: #

10.8.10.234 10.8.10.235 10.8.10.236 #

Shard1 Shard Master slaver arbiteronly #

Shard2 Shard arbiteronly Master slaver #

Shard3 Shard slaver arbiteronly Master #

#

###############################################################


This article is from the "Clumsy birds have" blog, make sure to keep this source http://2574526.blog.51cto.com/2564526/1573855

MongoDB Shard Configuration

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.