1
|
[root@mongo1 mongodb-2.6.5]
# yum install -y mongodb-org-server-2.6.5-1.x86_64.rpm mongodb-org-tools-2.6.5-1.x86_64.rpm mongodb-org-shell-2.6.5-1.x86_64.rpm
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@mongo1 ~]
# vim /etc/mongod.conf
logpath=
/var/log/mongodb/mongod
.log
logappend=
true
fork=
true
dbpath=
/mongodb/data
pidfilepath=
/var/run/mongodb/mongod
.pid
bind_ip=0.0.0.0
httpinterface=
true
rest=
true
replSet=rs0
replIndexPrefetch = _id_only
|
1
2
3
4
5
6
7
|
[root@mongo1 ~]
# mkdir -pv /mongodb/data
mkdir
: created directory `
/mongodb
'
mkdir
: created directory `
/mongodb/data
'
[root@mongo1 ~]
# chown -R mongod.mongod /mongodb
[root@mongo1 ~]
# service mongod start
Starting mongod: [ OK ]
|
1
2
3
4
5
6
7
8
9
10
|
[root@mongo1 ~]
# mongo --host 192.168.1.132
MongoDB shell version: 2.6.5
connecting to: 192.168.1.132:27017
/test
> rs.status()
{
"startupStatus"
: 3,
"info"
:
"run rs.initiate(...) if not yet done for the set"
,
"ok"
: 0,
"errmsg"
:
"can't get local.system.replset config from self or any seed (EMPTYCONFIG)"
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
> cfg={_id:
'rs0'
,members:[
... ... {_id:0,host:
'192.168.1.132:27017'
},
... ... {_id:1,host:
'192.168.1.139:27017'
},
... ... {_id:2,host:
'192.168.1.140:27017'
}]
... ... }
{
"_id"
:
"rs0"
,
"members"
: [
{
"_id"
: 0,
"host"
:
"192.168.1.132:27017"
},
{
"_id"
: 1,
"host"
:
"192.168.1.139:27017"
},
{
"_id"
: 2,
"host"
:
"192.168.1.140:27017"
}
]
}
#################################
> rs.initiate(cfg)
{
"info"
:
"Config now saved locally. Should come online in about a minute."
,
"ok"
: 1
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
> rs.status()
{
"set"
:
"rs0"
,
"date"
: ISODate(
"2015-09-04T23:02:13Z"
),
"myState"
: 1,
"members"
: [
#显示副本集的所有成员信息
{
"_id"
: 0,
#节点的标识符
"name"
:
"192.168.1.132:27017"
,
#节点名称
"health"
: 1,
#节点的健康状态
"state"
: 1,
"stateStr"
:
"PRIMARY"
,
#该节点为主节点
"uptime"
: 1750,
#运行时长
"optime"
: Timestamp(1441407002, 1),
#oplog最后一次操作的时间戳
"optimeDate"
: ISODate(
"2015-09-04T22:50:02Z"
),
#oplog最后一次操作的时间
"electionTime"
: Timestamp(1441407011, 1),
#选举时间
"electionDate"
: ISODate(
"2015-09-04T22:50:11Z"
),
#选举日期
"self"
:
true
#表示是否为当前节点
},
{
"_id"
: 1,
"name"
:
"192.168.1.139:27017"
,
"health"
: 1,
"state"
: 2,
"stateStr"
:
"SECONDARY"
,
#从节点
"uptime"
: 730,
"optime"
: Timestamp(1441407002, 1),
"optimeDate"
: ISODate(
"2015-09-04T22:50:02Z"
),
"lastHeartbeat"
: ISODate(
"2015-09-04T23:02:13Z"
),
"lastHeartbeatRecv"
: ISODate(
"2015-09-04T23:02:12Z"
),
"pingMs"
: 0,
"syncingTo"
:
"192.168.1.132:27017"
#指向的主节点
},
{
"_id"
: 2,
"name"
:
"192.168.1.140:27017"
,
"health"
: 1,
"state"
: 2,
"stateStr"
:
"SECONDARY"
,
"uptime"
: 730,
"optime"
: Timestamp(1441407002, 1),
"optimeDate"
: ISODate(
"2015-09-04T22:50:02Z"
),
"lastHeartbeat"
: ISODate(
"2015-09-04T23:02:13Z"
),
"lastHeartbeatRecv"
: ISODate(
"2015-09-04T23:02:12Z"
),
"pingMs"
: 0,
"syncingTo"
:
"192.168.1.132:27017"
}
],
"ok"
: 1
}
|
1
2
3
4
|
rs0:PRIMARY> use student_db
switched to db student_db
rs0:PRIMARY>
for
(i=1;i<=100000;i++) db.students.insert({name:
"student"
+i,age:(i%120),address:
"china_nb"
});
WriteResult({
"nInserted"
: 1 })
|
1
2
3
4
|
rs0:SECONDARY> use student_db
switched to db student_db
rs0:SECONDARY> db.students.findOne()
2015-09-04T19:28:10.730-0400 error: {
"$err"
:
"not master and slaveOk=false"
,
"code"
: 13435 } at src
/mongo/shell/query
.js:131
|
1
2
3
4
5
6
7
8
9
10
|
rs0:SECONDARY> rs.slaveOk()
rs0:SECONDARY> db.student.findOne()
null
rs0:SECONDARY> db.students.findOne()
{
"_id"
: ObjectId(
"55ea287ce476f31ac766a383"
),
"name"
:
"student1"
,
"age"
: 1,
"address"
:
"china_nb"
}
|
1
2
|
[root@mongo1 ~]
# service mongod stop
Stopping mongod: [ OK ]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
rs0:SECONDARY> rs.status()
{
"set"
:
"rs0"
,
"date"
: ISODate(
"2015-09-04T23:31:49Z"
),
"myState"
: 1,
"members"
: [
{
"_id"
: 0,
"name"
:
"192.168.1.132:27017"
,
"health"
: 0,
#主节点已经下线
"state"
: 8,
"stateStr"
:
"(not reachable/healthy)"
,
................
},
{
"_id"
: 1,
"name"
:
"192.168.1.139:27017"
,
"health"
: 1,
"state"
: 1,
"stateStr"
:
"PRIMARY"
,
#新选出的主节点
............
},
{
"_id"
: 2,
"name"
:
"192.168.1.140:27017"
,
"health"
: 1,
"state"
: 2,
"stateStr"
:
"SECONDARY"
,
.........
}
],
"ok"
: 1
}
|
1
2
|
rs0:PRIMARY> rs.add(
"192.168.1.138"
)
{
"ok"
: 1 }
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
{
"_id"
: 2,
"name"
:
"192.168.1.127:27017"
,
"health"
: 1,
.....................
"lastHeartbeatMessage"
:
"still initializing"
#正在初始化
}
#######################
{
"_id"
: 1,
"name"
:
"192.168.1.138:27017"
,
"health"
: 1,
........
"lastHeartbeatMessage"
:
"initial sync need a member to be primary or secondary to do our initial sync"
#同步数据
}
#######################
{
"_id"
: 1,
"name"
:
"192.168.1.138:27017"
,
"health"
: 1,
........
"syncingTo"
:
"node1.xiaoxiao.com:27017"
#同步完成
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
rs0:PRIMARY> cfg=rs.conf()
rs0:PRIMARY> cfg.members[2].priority=2
#节点的标识符为2
rs0:PRIMARY> rs.reconfig(cfg)
#更新配置
################
rs0:SECONDARY> rs.config()
{
................
{
"_id"
: 2,
"host"
:
"192.168.1.140:27017"
,
"priority"
: 2
#对应优先级
}
]
}
|
1
2
3
4
|
[root@node1 ~]
# vim /etc/mongod.conf
#replSet=rs0
#replIndexPrefetch = _id_only
configsvr =
true
|
1
2
|
[root@node1 ~]
# service mongod start
Starting mongod: [ OK ]
|
1
|
[root@node4 mongodb-2.6.5]
# yum install mongodb-org-mongos-2.6.5-1.x86_64.rpm
|
1
2
|
[root@node4 ~]
# mkdir /var/log/mongodb
[root@node4 ~]
# mongos --configdb=192.168.1.106 --fork --logpath=/var/log/mongodb/mongo.log
|
1
2
|
#replSet=rs0
#replIndexPrefetch = _id_only
|
1
2
3
4
5
|
[root@node2 ~]
# service mongod start
Starting mongod: [ OK ]
###################
[root@node3 ~]
# service mongod start
Starting mongod: [ OK ]
|
1
2
3
4
|
[root@node1 ~]
# mongo --host 192.168.1.131
MongoDB shell version: 2.6.5
connecting to: 192.168.1.131:27017
/test
mongos>
|
1
2
3
4
|
mongos> sh.addShard(
"192.168.1.127"
)
{
"shardAdded"
:
"shard0000"
,
"ok"
: 1 }
mongos> sh.addShard(
"192.168.1.138"
)
{
"shardAdded"
:
"shard0001"
,
"ok"
: 1 }
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
mongos> sh.enableSharding(
"student_db"
)
{
"ok"
: 1 }
################################
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id"
: 1,
"version"
: 4,
"minCompatibleVersion"
: 4,
"currentVersion"
: 5,
"clusterId"
: ObjectId(
"55e89a17f0cf218cb7edd0c5"
)
}
shards:
{
"_id"
:
"shard0000"
,
"host"
:
"192.168.1.127:27017"
}
{
"_id"
:
"shard0001"
,
"host"
:
"192.168.1.138:27017"
}
databases:
{
"_id"
:
"admin"
,
"partitioned"
:
false
,
"primary"
:
"config"
}
{
"_id"
:
"student_db"
,
"partitioned"
:
true
,
"primary"
:
"shard0000"
}
#显示该数据库已经支持分片
|
1
2
3
4
5
6
|
mongos> sh.shardCollection(
"student_db.students"
,{
"age"
:1})
{
"collectionsharded"
:
"student_db.student"
,
"ok"
: 1 }
#################################
#插入数据
mongos>
for
(i=1;i<=100000;i++) db.students.insert({name:
"student"
+i,age:(i%120),address:
"china_nb"
});
WriteResult({
"nInserted"
: 1 })
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id"
: 1,
"version"
: 4,
"minCompatibleVersion"
: 4,
"currentVersion"
: 5,
"clusterId"
: ObjectId(
"55e89a17f0cf218cb7edd0c5"
)
}
shards:
{
"_id"
:
"shard0000"
,
"host"
:
"192.168.1.127:27017"
}
{
"_id"
:
"shard0001"
,
"host"
:
"192.168.1.138:27017"
}
databases:
{
"_id"
:
"admin"
,
"partitioned"
:
false
,
"primary"
:
"config"
}
{
"_id"
:
"student_db"
,
"partitioned"
:
true
,
"primary"
:
"shard0000"
}
student_db.students
shard key: {
"age"
: 1 }
chunks:
shard0001 1
shard0000 2
{
"age"
: {
"$minKey"
: 1 } } -->> {
"age"
: 1 } on : shard0001 Timestamp(2, 0)
{
"age"
: 1 } -->> {
"age"
: 119 } on : shard0000 Timestamp(2, 1)
{
"age"
: 119 } -->> {
"age"
: {
"$maxKey"
: 1 } } on : shard0000 Timestamp(1, 4)
|