mkdir -pv /data/mongodb/mongos/log
mkdir -pv /data/mongodb/config/{data,log}
mkdir -pv /data/mongodb/shard1/{data,log}
mkdir -pv /data/mongodb/shard2/{data,log}
mkdir -pv /data/mongodb/shard3/{data,log}
mkdir /var/run/mongodb
mkdir /etc/mongod
vim /etc/mongod/config.conf
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true
replSet = configs
maxConns = 20000
[root@db1 opt]# mongod -f /etc/mongod/config.conf
[root@db1 opt]# ps aux | grep mongod
root 11361 0.8 4.4 1051304 44364 ? Sl 18:52 0:00 mongod -f /etc/mongod/config.conf
[root@db1 opt]# netstat -nltp | grep mongod
tcp 0 0 0.0.0.0:21000 0.0.0.0:* LISTEN 11361/mongod
[root@db1 opt]# mongo --port 21000
> config = {_id:"configs",members:[{_id : 0, host : "128.0.0.15:21000"},{_id : 1, host : "128.0.0.16:21000"},{_id : 2, host : "128.0.0.17:21000"}] }
{
"_id" : "configs",
"members" : [
{
"_id" : 0,
"host" : "128.0.0.15:21000"
},
{
"_id" : 1,
"host" : "128.0.0.16:21000"
},
{
"_id" : 2,
"host" : "128.0.0.17:21000"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
configs:OTHER> rs.status()
{
"set" : "configs",
"date" : ISODate("2018-02-05T11:08:23.945Z"),
"myState" : 1,
"term" : NumberLong(1),
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "128.0.0.15:21000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 938,
"optime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T11:08:15Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1517828841, 1),
"electionDate" : ISODate("2018-02-05T11:07:21Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "128.0.0.16:21000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 72,
"optime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T11:08:15Z"),
"optimeDurableDate" : ISODate("2018-02-05T11:08:15Z"),
"lastHeartbeat" : ISODate("2018-02-05T11:08:23.635Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T11:08:22.307Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "128.0.0.15:21000",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "128.0.0.17:21000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 72,
"optime" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1517828895, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T11:08:15Z"),
"optimeDurableDate" : ISODate("2018-02-05T11:08:15Z"),
"lastHeartbeat" : ISODate("2018-02-05T11:08:23.674Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T11:08:23.306Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "128.0.0.15:21000",
"configVersion" : 1
}
],
"ok" : 1
}
configs:PRIMARY> exit
bye
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface = true
rest = true
replSet = shard1
shardsvr = true
maxConns = 20000
[root@db1 opt]# sed -i "s/shard1/shard2/g" /etc/mongod/shard3.conf
[root@db1 opt]# cat !$
cat /etc/mongod/shard2.conf
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
httpinterface = true
rest = true
replSet = shard2
shardsvr = true
maxConns = 20000
[root@db1 opt]# sed -i "s/shard1/shard3/g" /etc/mongod/shard3.conf
[root@db1 opt]# cat !$
cat /etc/mongod/shard3.conf
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
httpinterface = true
rest = true
replSet = shard3
shardsvr = true
maxConns = 20000
[root@db1 opt]# cd /etc/mongod/
[root@db1 mongod]# ls
config.conf shard1.conf shard2.conf shard3.conf
[root@db1 mongod]# scp shard2.conf shard3.conf bd2:/etc/mongod/
^C[root@db1 mongod]# scp shard2.conf shard3.conf db2:/etc/mongod/
root@db2's password:
shard2.conf 100% 269 296.5KB/s 00:00
shard3.conf 100% 269 310.3KB/s 00:00
[root@db1 mongod]# scp shard2.conf shard3.conf db3:/etc/mongod/
root@db3's password:
shard2.conf 100% 269 285.9KB/s 00:00
shard3.conf 100% 269 282.0KB/s 00:00
三台机器上都要操作
[root@db1 mongod]# mongod -f /etc/mongod/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11504
child process started successfully, parent exiting
[root@db2 opt]# mongod -f /etc/mongod/shard1.conf
[root@db3 opt]# mongod -f /etc/mongod/shard1.conf
启动shard1
[root@db1 mongod]# mongo --port 27001
> use admin
switched to db admin
> config = {_id:"shard1",members:[{_id : 0, host : "128.0.0.15:27001"},{_id : 1, host : "128.0.0.16:27001"},{_id : 2, host : "128.0.0.17:27001",arbiterOnly:true}] }
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "128.0.0.15:27001"
},
{
"_id" : 1,
"host" : "128.0.0.16:27001"
},
{
"_id" : 2,
"host" : "128.0.0.17:27001",
"arbiterOnly" : true
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2018-02-05T11:53:10.723Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1517831581, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1517831581, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1517831581, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "128.0.0.15:27001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 967,
"optime" : {
"ts" : Timestamp(1517831581, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T11:53:01Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1517831580, 1),
"electionDate" : ISODate("2018-02-05T11:53:00Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "128.0.0.16:27001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 21,
"optime" : {
"ts" : Timestamp(1517831581, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1517831581, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T11:53:01Z"),
"optimeDurableDate" : ISODate("2018-02-05T11:53:01Z"),
"lastHeartbeat" : ISODate("2018-02-05T11:53:10.139Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T11:53:10.561Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "128.0.0.15:27001",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "128.0.0.17:27001",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 21,
"lastHeartbeat" : ISODate("2018-02-05T11:53:10.164Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T11:53:06.475Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
}
],
"ok" : 1
}
启动shard2
[root@db1 mongod]# mongod -f /etc/mongod/shard2.conf
[root@db2 mongod]# mongod -f /etc/mongod/shard2.conf
[root@db3 mongod]# mongod -f /etc/mongod/shard2.conf
[root@db2 mongod]# mongo --port 27002
> use admin
switched to db admin
> config = {_id:"shard2",members:[{_id : 0, host : "128.0.0.15:27002",arbiterOnly:true},{_id : 1, host : "128.0.0.16:27002"},{_id : 2, host : "128.0.0.17:27002"}] }
{
"_id" : "shard2",
"members" : [
{
"_id" : 0,
"host" : "128.0.0.15:27002",
"arbiterOnly" : true
},
{
"_id" : 1,
"host" : "128.0.0.16:27002"
},
{
"_id" : 2,
"host" : "128.0.0.17:27002"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
shard2:OTHER> rs.status()
{
"set" : "shard2",
"date" : ISODate("2018-02-05T12:35:34.080Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1517834128, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1517834128, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1517834128, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "128.0.0.15:27002",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 37,
"lastHeartbeat" : ISODate("2018-02-05T12:35:33.244Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T12:35:33.960Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "128.0.0.16:27002",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2049,
"optime" : {
"ts" : Timestamp(1517834128, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T12:35:28Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1517834107, 1),
"electionDate" : ISODate("2018-02-05T12:35:07Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "128.0.0.17:27002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 37,
"optime" : {
"ts" : Timestamp(1517834128, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1517834128, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T12:35:28Z"),
"optimeDurableDate" : ISODate("2018-02-05T12:35:28Z"),
"lastHeartbeat" : ISODate("2018-02-05T12:35:33.244Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T12:35:34.054Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "128.0.0.16:27002",
"configVersion" : 1
}
],
"ok" : 1
}
启动shard3
[root@db1 mongod]# mongod -f /etc/mongod/shard3.conf
[root@db2mongod]# mongod -f /etc/mongod/shard3.conf
[root@db3mongod]# mongod -f /etc/mongod/shard3.conf
[root@db3 opt]# mongo --port 27003
> use admin
switched to db admin
> config = {_id:"shard3",members:[{_id : 0, host : "128.0.0.15:27003"},{_id : 1, host : "128.0.0.16:27003",arbiterOnly:true},{_id : 2, host : "128.0.0.17:27003"}] }
{
"_id" : "shard3",
"members" : [
{
"_id" : 0,
"host" : "128.0.0.15:27003"
},
{
"_id" : 1,
"host" : "128.0.0.16:27003",
"arbiterOnly" : true
},
{
"_id" : 2,
"host" : "128.0.0.17:27003"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
shard3:OTHER> rs.status()
{
"set" : "shard3",
"date" : ISODate("2018-02-05T12:42:56.776Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1517834568, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1517834568, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1517834568, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "128.0.0.15:27003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 21,
"optime" : {
"ts" : Timestamp(1517834568, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1517834568, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T12:42:48Z"),
"optimeDurableDate" : ISODate("2018-02-05T12:42:48Z"),
"lastHeartbeat" : ISODate("2018-02-05T12:42:56.392Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T12:42:53.041Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "128.0.0.17:27003",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "128.0.0.16:27003",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 21,
"lastHeartbeat" : ISODate("2018-02-05T12:42:56.392Z"),
"lastHeartbeatRecv" : ISODate("2018-02-05T12:42:51.999Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "128.0.0.17:27003",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 316,
"optime" : {
"ts" : Timestamp(1517834568, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-02-05T12:42:48Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1517834566, 1),
"electionDate" : ISODate("2018-02-05T12:42:46Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
[root@db1 mongod]# vim /etc/mongod/mongos.conf
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/128.0.0.15:21000,128.0.0.16:21000,128.0.0.17:21000
maxConns = 20000
[root@db1 mongod]# mongos -f /etc/mongod/mongos.conf
[root@db2 mongod]# mongos -f /etc/mongod/mongos.conf
[root@db3 mongod]# mongos -f /etc/mongod/mongos.conf
#把所有的分片和路由器串联
[root@db1 mongod]# mongo --port 20000
mongos> mongos> sh.addShard("shard1/128.0.0.15:27001,128.0.0.16:27001,128.0.0.17:27001")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/128.0.0.15:27002,128.0.0.16:27002,128.0.0.17:27002")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/128.0.0.15:27003,128.0.0.16:27003,128.0.0.17:27003")
{ "shardAdded" : "shard3", "ok" : 1 }
#查看集群
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5a783aebf1f1012ae074d793")
}
shards:
{ "_id" : "shard1", "host" : "shard1/128.0.0.15:27001,128.0.0.16:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/128.0.0.16:27002,128.0.0.17:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/128.0.0.15:27003,128.0.0.17:27003", "state" : 1 }
active mongoses:
"3.4.9" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Balancer lock taken at Mon Feb 05 2018 19:07:24 GMT+0800 (CST) by ConfigServer:Balancer
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases: