MongoDB Shard Replica Set Build

Source: Internet
Author: User
Tags mongodb

1. Installing MongoDB
Tar xvf mongodb-linux-x86_64-enterprise-rhel70-3.6.2.tgz-c/usr/local/
Cd/usr/local
MV Mongodb-linux-x86_64-enterprise-rhel70-3.6.2/mongodb
Mkdir-p/usr/local/mongodb/conf
Mkdir-p/usr/local/mongodb/mongos/log
Mkdir-p/usr/local/mongodb/config/data
Mkdir-p/usr/local/mongodb/config/log
Mkdir-p/usr/local/mongodb/shard1/data
Mkdir-p/usr/local/mongodb/shard1/log
Mkdir-p/usr/local/mongodb/shard2/data
Mkdir-p/usr/local/mongodb/shard2/log
Mkdir-p/usr/local/mongodb/shard3/data
Mkdir-p/usr/local/mongodb/shard3/log
Chown-r Mongo.mongo/usr/local/mongodb

2. config server configuration
Vim/usr/local/mongodb/conf/config.conf
#配置文件内容
Pidfilepath =/usr/local/mongodb/config/log/configsrv.pid
DBPath =/usr/local/mongodb/config/data
LogPath =/usr/local/mongodb/config/log/congigsrv.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 21000
Fork = True
Configsvr = True
Replset=bsbgps
maxconns=20000

#启动三台服务器的config Server
Mongod-f/usr/local/mongodb/conf/config.conf

#初始化配置副本集
#连接
Mongo--port 21000
#config变量
Config = {
_id: "Bsbgps",
Members: [
{_id:0, Host: "188.188.1.135:21000"},
{_id:1, Host: "188.188.1.136:21000"},
{_id:2, Host: "188.188.1.137:21000"}
]

#初始化副本集
Rs.initiate (config)

3, configuring the Shard replica set
A. Set the first shard replica set
vim/usr/local/mongodb/conf/shard1.conf
#配置文件内容
Pidfilepath =/usr/local/ Mongodb/shard1/log/shard1.pid
DBPath =/usr/local/mongodb/shard1/data
LogPath =/usr/local/mongodb/shard1/ Log/shard1.log
Logappend = True
Bind_ip = 0.0.0.0
Port = 27001
Fork = True
Replset=shard1
Shardsvr = True
maxconns=20000
#启动三台服务器的shard1 server
mongod-f/usr/local/mongodb/conf/shard1.conf
#登陆非仲裁服务器, Initializes the replica set
Mongo--port 27001
#使用admin数据库
Use admin
#定义副本集配置, the third node of "Arbiteronly": True for the quorum node.
Config = {
_id: "Shard1",
Members: [
{_id:0, Host: "188.188.1.135:27001"},
{_id:1, Host: "188.188 .1.136:27001 "},
{_id:2, Host:" 188.188.1.137:27001 ", arbiteronly:true}
]
}
#初始化副本集配置
Rs.initiate ( config);

B. Setting up a second shard replica set
vim/usr/local/mongodb/conf/shard2.conf
#配置文件内容
Pidfilepath =/usr/local/mongodb/shard2/ Log/shard2.pid
DBPath =/usr/local/mongodb/shard2/data
LogPath =/usr/local/mongodb/shard2/log/shard2.log
Logappend = True
Bind_ip = 0.0.0.0
Port = 27002
Fork = True
Replset=shard2
Shardsvr = True
maxconns=20000
#启动三台服务器的shard2 Server
mongod-f/usr/local/mongodb/conf/shard2.conf
#登陆非仲裁服务器, initializing the replica set
MONGO--port 27002
#使用admin数据库
Use admin
#定义副本集配置
Config = {
_id: ' Shard2 ',
Members: [
{_id:0, ho ST: "188.188.1.135:27002", arbiteronly:true},
{_id:1, Host: "188.188.1.136:27002"},
{_id:2, Host: "188.18 8.1.137:27002 "}
]
}
#初始化副本集配置
rs.initiate (config);

C. Set up a third shard replica set
vim/usr/local/mongodb/conf/shard3.conf
#配置文件内容
Pidfilepath =/usr/local/mongodb/shard3/ Log/shard3.pid
DBPath =/usr/local/mongodb/shard3/data
LogPath =/usr/local/mongodb/shard3/log/shard3.log
Logappend = True
Bind_ip = 0.0.0.0
Port = 27003
Fork = True
Replset=shard3
Shardsvr = True
maxconns=20000
#启动三台服务器的shard3 Server
mongod-f/usr/local/mongodb/conf/shard3.conf
#登陆非仲裁服务器, initializing the replica set
MONGO--port 27003
#使用admin数据库
Use admin
#定义副本集配置
Config = {
_id: ' Shard3 ',
Members: [
{_id:0, ho ST: "188.188.1.135:27003"},
{_id:1, Host: "188.188.1.136:27003", arbiteronly:true},
{_id:2, Host: "188.188 .1.137:27003 "}
]
}
#初始化副本集配置
rs.initiate (config);

4. Configure the routing server MONGOs
Start the configuration server and the Shard server before starting the routing instance: (Three machines)
Vim/usr/local/mongodb/conf/mongos.conf
#内容, the Listener configuration server can only have 1 or 3 configs as the replica set name of the configuration server
Pidfilepath =/usr/local/mongodb/mongos/log/mongos.pid
LogPath =/usr/local/mongodb/mongos/log/mongos.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 20000
Fork = True
ConfigDB = bsbgps/188.188.1.135:21000,188.188.1.136:21000,188.188.1.137:21000
maxconns=20000
#启动三台服务器的mongos Server
Mongos-f/usr/local/mongodb/conf/mongos.conf

5. Enable Sharding
At present, the 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.
Log on to any one of the MONGOs
MONGO--port 20000
#使用admin数据库
Use admin
#串联路由服务器与分配副本集
Sh.addshard ("shard1/188.188.1.135:27001,188.188.1.136:27001,188.188.1.137:27001")
Sh.addshard ("shard2/188.188.1.135:27002,188.188.1.136:27002,188.188.1.137:27002")
Sh.addshard ("shard3/188.188.1.135:27003,188.188.1.136:27003,188.188.1.137:27003")
#查看集群状态
Sh.status ()

6. Testing
#指定ywtestdb分片生效
Db.runcommand ({enablesharding: "Ywtestdb"});
#指定数据库里需要分片的集合和片键
Db.runcommand ({shardcollection: "YWTESTDB.TESTTB", key: {id:1}})
#插入测试数据
Use Ywtestdb
for (var i = 1; i <=; i++) {
Db.testtb.save ({id:i, "time": new Date (). getFullYear () + "-" + (new Date (). GetMonth () +1) + "-" +new date (). GetDate () + "" + New Date (). tolocaletimestring ()})
}
#查看分片情况如下, some irrelevant information is omitted.
Db.testtb.stats ();

Reference blog:
Http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html
https://www.jianshu.com/p/baed80df9300

MongoDB Shard Replica 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.