### mongodb的主从配置(不带auth认证)
### 注意事项:
- 服务器节点之前时间要同步
- 开启防火墙的一定要允许通过相关端口
- 开启selinux的也要进行设置
- 建立双击互信模式最好不过
### master配置文件
-
- 添加了master = true 这一行即可
-
- 多网卡机器bind_ip这一行尽可能写成一个具体地址(最好写内网地址),因为slave上是根据这个同步的
1
2
3
4
5
6
7
8
9
10
11
|
[root@blog ~]# sed -e '/^#/d;/^$/d' /etc/mongodb.conf
bind_ip = 192.168.58.10
port = 27017
fork = true
pidfilepath = /var/run/mongodb/mongodb.pid
logpath = /var/log/mongodb/mongodb.log
dbpath =/mongo/data
journal = true
master = true
nohttpinterface = true
[root@blog ~]#
|
### slave配置文件
-
添加了以下两行
- source = 192.168.58.10:27017
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@redis ~]# sed -e '/^#/d;/^$/d' /etc/mongodb.conf
bind_ip = 192.168.58.30
port = 27017
fork = true
pidfilepath = /var/run/mongodb/mongodb.pid
logpath = /var/log/mongodb/mongodb.log
dbpath =/var/lib/mongodb
journal = true
nohttpinterface = true
slave = true
source = 192.168.58.10:27017
[root@redis ~]#
|
### 重启两节点的mongodb服务进行初始化操作并验证数据同步情况
-
master
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@blog ~]#
[root@blog ~]# hostname
blog.unix178.com
[root@blog ~]# mongo
MongoDB shell version: 2.4.6
connecting to: test
> show dbs
admin0.203125GB
local10.0732421875GB
test0.203125GB
zhuima0.203125GB
>
|
-
slave
1
2
3
4
5
6
7
8
9
10
|
[root@redis ~]# hostname
redis.unix178.com
[root@redis ~]# mongo
MongoDB shell version: 2.4.6
connecting to: test
> show dbs
admin0.203125GB
local0.078125GB
zhuima0.203125GB
>
|
### 查看同步情况
-
master服务器上
1
2
3
4
5
6
7
8
9
10
|
[root@blog ~]# mongo 192.168.58.30
MongoDB shell version: 2.4.6
connecting to: 192.168.58.30/test
> show dbs
admin0.203125GB
local0.078125GB
nick0.203125GB
test0.203125GB
zhuima0.203125GB
>
|
-
slave服务器上
1
2
3
4
5
6
7
8
9
10
|
[root@redis ~]# mongo 192.168.58.10
MongoDB shell version: 2.4.6
connecting to: 192.168.58.10/test
> show dbs;
admin0.203125GB
local10.0732421875GB
nick0.203125GB
test0.203125GB
zhuima0.203125GB
>
|
### 添加新的数据进行验证
-
master上添加数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@redis ~]# mongo 192.168.58.10
MongoDB shell version: 2.4.6
connecting to: 192.168.58.10/test
> show dbs;
admin0.203125GB
local10.0732421875GB
nick0.203125GB
test0.203125GB
zhuima0.203125GB
> use nick
switched to db nick
> use newdb
switched to db newdb
> db.newdb.insert({new:"yes",info:"nothing"})
> show collections
newdb
system.indexes
> db.newdb.find()
{ "_id" : ObjectId("53cbd4b35974b01658718b2d"), "new" : "yes", "info" : "nothing" }
>
|
-
slave上验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@blog ~]# mongo 192.168.58.30
MongoDB shell version: 2.4.6
connecting to: 192.168.58.30/test
> show dbs
admin0.203125GB
local0.078125GB
nick0.203125GB
test0.203125GB
zhuima0.203125GB
> show dbs
admin0.203125GB
local0.078125GB
newdb0.203125GB
nick0.203125GB
test0.203125GB
zhuima0.203125GB
> use newdb
switched to db newdb
> show collections
newdb
system.indexes
> db.newdb.find()
{ "_id" : ObjectId("53cbd4b35974b01658718b2d"), "new" : "yes", "info" : "nothing" }
>
|
### 后记
-
mongodb官方已经不再推荐使用master/salve模式的结构
-
官方推荐使用replica_set复制集模式
-
仅供参考
本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1441037,如需转载请自行联系原作者