一、配置环境
1.两台安装了mongodb的CentOS服务器;(安装参考http://281816327.blog.51cto.com/907015/1598270)
2.两台服务器的ip分别为192.168.1.112、192.168.1.113;
3.两台服务器防火墙放开mongodb的默认27017的端口;
二、配置服务器
两台服务器都做如下的配置
创建/data/db文件夹
1
|
mkdir /data && mkdir /data/db
|
修改配置文件
1
|
vim /etc/mongodb .conf
|
其中的重要参数修改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#where to log logpath= /var/log/mongodb/mongod .log
logappend= true
# fork and run in background fork= true
dbpath= /data/db
# location of pidfile pidfilepath= /var/run/mongodb/mongod .pid
# Listen to local interface only. Comment out to listen on all interfaces. #bind_ip=127.0.0.1 # Enable the HTTP interface (Defaults to port 28017). httpinterface= true
# in replicated mongo databases, specify the replica set name here replSet=spock |
启动MongoDB:
1
|
mongod –f /etc/mongodb .conf –-rest
|
三、配置副本集
在192.168.1.122上进入mongodb的shell,测试连接192.168.1.113并创建一个config文档:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@localhost ~] # mongo
MongoDB shell version: 2.6.6 connecting to: test > db=(new Mongo( "192.168.1.113:27017" )).getDB( "test" )
test > config={ "_id" : "spock" ,
... "members" :[
... { "_id" :0, "host" : "192.168.1.112:27017" },
... { "_id" :1, "host" : "192.168.1.113:27017" }]}
{ "_id" : "spock" ,
"members" : [
{
"_id" : 0,
"host" : "192.168.1.112:27017" },
{
"_id" : 1,
"host" : "192.168.1.113:27017" }
]
} |
初始化副本集:
1
2
3
4
5
|
> rs.initiate(config) { "info" : "Config now saved locally. Should come online in about a minute." ,
"ok" : 1
} |
四、验证结果
在192.168.1.112上进入MongoDB shell:
1
2
3
4
|
[root@localhost ~] # mongo
MongoDB shell version: 2.6.6 connecting to: test spock:SECONDARY> |
在192.168.1.112上进入MongoDB shell:
1
2
3
4
|
[root@localhost ~] # mongo
MongoDB shell version: 2.6.7 connecting to: test spock:PRIMARY> |
可以看得出spock:SECONDARY和spock:PRIMARY关键字,说明副本集配置完成;
本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1610153,如需转载请自行联系原作者