Mkdir-p/home/mongodb/conf
Mkdir-p/home/mongodb/mongos/log
Mkdir-p/home/mongodb/config/data
Mkdir-p/home/mongodb/config/log
Mkdir-p/home/mongodb/shard1/data
Mkdir-p/home/mongodb/shard1/log
Mkdir-p/home/mongodb/shard2/data
Mkdir-p/home/mongodb/shard2/log
Mkdir-p/home/mongodb/shard3/data
Mkdir-p/home/mongodb/shard3/log
#config. conf
Pidfilepath =/home/mongodb/config/log/configsrv.pid
DBPath =/home/mongodb/config/data
LogPath =/home/mongodb/config/log/congigsrv.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 23000
Fork = True
Configsvr = True
Replset=config-server
maxconns=20000
Configuration config-server:
Start config server for three servers
Mongod-f/home/mongodb/conf/config.conf
Log on to any configuration server, initialize the configuration replica set
#连接
MONGO--port 23000
#config变量
Config = {
_id: "Config-server",
Members: [
{_id:0, Host: "192.168.147.134:23000"},
{_id:1, Host: "192.168.147.135:23000"},
{_id:2, Host: "192.168.147.136:23000"}
]
}
#初始化副本集
Rs.initiate (config)
#shard1. conf
Pidfilepath =/home/mongodb/shard1/log/shard1.pid
DBPath =/home/mongodb/shard1/data
LogPath =/home/mongodb/shard1/log/shard1.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 23001
Fork = True
Replset=shard1-server
Shardsvr = True
maxconns=20000
Configuration shard1-server: # #shard2-server shard3-server Similar
Shard1 server that starts three servers
Mongod-f/home/mongodb/conf/shard1.conf
Log on to any server and initialize the replica set
MONGO--port 23001
#使用admin数据库
Use admin
#定义副本集配置, the third node of "Arbiteronly": true represents its quorum node.
Config = {
_id: "Shard1-server",
Members: [
{_id:0, Host: "192.168.147.135:23001"},
{_id:1, Host: "192.168.147.134:23001"},
{_id:2, Host: "192.168.147.136:23001", arbiteronly:true}
]
}
#初始化副本集配置
Rs.initiate (config);
#shard2. conf
Pidfilepath =/home/mongodb/shard2/log/shard2.pid
DBPath =/home/mongodb/shard2/data
LogPath =/home/mongodb/shard2/log/shard2.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 23002
Fork = True
Replset=shard2-server
Shardsvr = True
maxconns=20000
#shard3. conf
Pidfilepath =/home/mongodb/shard3/log/shard3.pid
DBPath =/home/mongodb/shard3/data
LogPath =/home/mongodb/shard3/log/shard3.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 23003
Fork = True
Replset=shard3-server
Shardsvr = True
maxconns=20000
#mongos. conf
Pidfilepath =/home/mongodb/mongos/log/mongos.pid
LogPath =/home/mongodb/mongos/log/mongos.log
Logappend = True
BIND_IP = 0.0.0.0
Port = 40000
Fork = True
ConfigDB = config-server/192.168.147.134:23000,192.168.147.135:23000,192.168.147.135:23000
maxconns=20000
# #配置mongos
MONGOs server that starts three servers
Mongos-f/home/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 40000
#使用admin数据库
User admin
#串联路由服务器与分配副本集
Sh.addshard ("shard1-server/192.168.147.135:23001,192.168.147.134:23001,192.168.147.136:23001")
Sh.addshard ("shard2-server/192.168.147.135:23002,192.168.147.134:23002,192.168.147.136:23002")
Sh.addshard ("shard3-server/192.168.147.135:23003,192.168.147.134:23003,192.168.147.136:23003")
#查看集群状态
Sh.status ()
# # #启动关闭
The start-up sequence for MongoDB is to start the configuration server first, start the Shard, and finally start MONGOs.
Mongod-f/usr/local/mongodb/conf/config.conf
Mongod-f/usr/local/mongodb/conf/shard1.conf
Mongod-f/usr/local/mongodb/conf/shard2.conf
Mongod-f/usr/local/mongodb/conf/shard3.conf
Mongod-f/usr/local/mongodb/conf/mongos.conf
When off, direct killall kills all processes
Killall Mongod
Killall MONGOs
Test:
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!
#指定testdb分片生效
Db.runcommand ({enablesharding: "TestDB"});
#指定数据库里需要分片的集合和片键
Db.runcommand ({shardcollection: "Testdb.table1", key: {id:1}})
for (var i = 1; I <= 100000; i++) {
Db.table1.save ({id:i, "test1": "Testval1"})}
Db.table1.stats () to see the data fall on the Shard.
Reference:
https://zhuanlan.zhihu.com/p/28600032
Http://www.mongodb.org.cn/tutorial/53.html
This article is from the "Nginx Installation Optimization" blog, please be sure to keep this source http://mrdeng.blog.51cto.com/3736360/1971578
MongoDB Cluster build shard + replica set