|
一、环境
操作系统:
1
2
3
4
5
| # uname -r
2.6.32-358.el6.x86_64
# cat /etc/issue
CentOS release 6.4 (Final)
Kernel on an m
|
主:192.168.137.148
从:192.168.137.154
二、主从同步系统时间
1
| #ntpdate ntp.api.bz && hwclock -w --systohc
|
三、安装
1
2
3
4
5
| #wget
#tar xf mongodb-linux-x86_64-enterprise-rhel62-2.6.4.tgz
#mv mongodb-linux-x86_64-enterprise-rhel62-2.6.4 /usr/local/mongodb
#[ ! -e /data/mongodb/data ] && mkdir -p /data/mongodb/data || echo '/data/mongodb/data is exist!'
#[ ! -e /data/mongodb/mongodb.log ] && touch /data/mongodb/mongodb.log || echo '/data/mongodb/mongodb.log is exist!'
|
四、启动服务
mongodb master:
1
| #/usr/local/mongodb/bin/mongod --master --bind_ip=192.168.137.148 --dbpath=/data/mongodb/data --logpath=/data/mongodb/mongodb.log --logappend --port=27017 --fork
|
mongodb slave:
1
| #/usr/local/mongodb/bin/mongod --slave --bind_ip=192.168.137.154 --source 192.168.137.148:27017 --dbpath=/data/mongodb/data --logpath=/data/mongodb/mongodb.log --logappend --port=27017 --fork
|
五、测试
master:
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
| # /usr/local/mongodb/bin/mongo --host 192.168.137.148
MongoDB shell version: 2.6.4
connecting to: 192.168.0.23:27017/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2014-09-24T14:16:58.169+0800 [initandlisten]
2014-09-24T14:16:58.169+0800 [initandlisten] ** WARNING: You are running on a NUMA machine.
2014-09-24T14:16:58.169+0800 [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2014-09-24T14:16:58.169+0800 [initandlisten] ** numactl --interleave=all mongod [other options]
2014-09-24T14:16:58.169+0800 [initandlisten]
> show dbs; #查看数据库
local 1.203125GB
test 0.203125GB
> use test1; #新建库
switched to db test1
> db.foo.save({"id":123456,"name":'zhangsan'}) #插入数据
> db.foo.find({"id":123456}) #查询数据
{ "_id" : ObjectId("542277078091089e1cbfecbc"), "id" : 123456, "name" : "zhangsan" }
> show dbs;
local 1.203125GB
test 0.203125GB
test1 0.203125GB
|
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # /usr/local/mongodb/bin/mongo --host 192.168.137.148
MongoDB shell version: 2.6.4
connecting to: 192.168.0.23:27017/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2014-09-24T14:16:58.169+0800 [initandlisten]
2014-09-24T14:16:58.169+0800 [initandlisten] ** WARNING: You are running on a NUMA machine.
2014-09-24T14:16:58.169+0800 [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2014-09-24T14:16:58.169+0800 [initandlisten] ** numactl --interleave=all mongod [other options]
2014-09-24T14:16:58.169+0800 [initandlisten]
> show dbs;
local 0.203125GB
test 0.203125GB
test1 0.203125GB
> use test1;
switched to db test1
> db.foo.find({"id":123456})
{ "_id" : ObjectId("542277078091089e1cbfecbc"), "id" : 123456, "name" : "zhangsan" }
|
数据同步成功
六、常见操作
1)在从服务器上查询主服务器的信息
1
2
3
4
5
6
7
8
| > use local;
switched to db local
> show collections;
me
sources
system.indexes
> db.sources.find()
{ "_id" : ObjectId("541d8f661037aa7d865374d0"), "host" : "192.168.137.148:27017", "source" : "main", "syncedTo" : { "t" : 1411545377000, "i" : 1 } }
|
2)在主服务器上查询同步状态
1
2
3
4
5
6
| > db.printReplicationInfo();
configured oplog size: 944.1375732421875MB
log length start to end: 420339secs (116.76hrs)
oplog first event time: Fri Sep 19 2014 19:15:28 GMT+0800 (CST)
oplog last event time: Wed Sep 24 2014 16:01:07 GMT+0800 (CST)
now: Wed Sep 24 2014 16:01:11 GMT+0800 (CST)
|
3)在master服务器上查询集合状态
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
| > db.printCollectionStats();
foo
{
"ns" : "test1.foo",
"count" : 1,
"size" : 56,
"avgObjSize" : 56,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}
---
system.indexes
{
"ns" : "test1.system.indexes",
"count" : 1,
"size" : 64,
"avgObjSize" : 64,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 0,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"flags" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"ok" : 1
}
---
|
七、主从切换
1)停掉slave服务
2)删除local数据
1
| #rm -rf /data/mongodb/data/local.*
|
3)把从服务器以master身份启动
1
| #/usr/local/mongodb/bin/mongod --master --bind_ip=192.168.137.154 --dbpath=/data/mongodb/data --logpath=/data/mongodb/mongodb.log --logappend --port=27017 --fork
|
4)以web方式查看状态
|
|