1.引子
mongodb的集群搭建方式主要有三种,主从模式,Replica set模式,sharding模式, 三种模式各有优劣,适用于不同的场合,属
Replica set应用最为广泛,主从模式现在用的较少,sharding模式最为完备,但配置维护较为复杂。本节我们来看下Replica Set模式的搭建方法。
2.Replica Set模式
Replica Set模式主要包括3个部分,主节点,备节点,仲裁节点.
主节点相当于主库,所有插入,查询,修改操作都可以在主节点执行。
备节点相当于从库,用来做备份,也可以承担查询的功能,减轻主节点的压力,
仲裁节点用于当主节点宕机后,从众多备份节点中选择一个节点作为主节点,仲裁节点不承担数据的增删改查功能,主要用来选举主节点。
接下来我们看看Replica Set环境的搭建的搭建方法
3.环境搭建
首先我们准备三台机器,我这里的机器的操作系统是CentOS 32位,ip地址为192.168.159.135, 192.168.159.136, 192.168.159.137,
这里以192.168.159.135为主节点,以192.168.159.136为从节点,192.168.159.137为仲裁节点
在三台机器上首先分别安装mongodb,我这里安装在usr/local目录下,然后在每台机器的mongodb的bin文件夹下上创建mongodb的配置文件mongodb.conf的内容如下
storageEngine=mmapv1
dbpath=/usr/local/mongodb1/db
logpath=/usr/local/mongodb1/logs/mongodb.log
pidfilepath=/usr/local/mongodb1/mongodb.pid
#keyFile=/opt/mongodb-linux-x86_64-3.2.9/bin/mongodb_keyfile
directoryperdb=true
logappend=true
replSet=rs1
bind_ip=192.168.159.135
port=27017
oplogSize=100
fork=true
noprealloc=true
auth=false
storageEngine=mmapv1
dbpath=/usr/local/mongodb1/db
logpath=/usr/local/mongodb1/logs/mongodb.log
pidfilepath=/usr/local/mongodb1/mongodb.pid
#keyFile=/opt/mongodb-linux-x86_64-3.2.9/bin/mongodb_keyfile
directoryperdb=true
logappend=true
replSet=rs1
bind_ip=192.168.159.136
port=27017
oplogSize=100
fork=true
noprealloc=true
auth=false
storageEngine=mmapv1
dbpath=/usr/local/mongodb1/db
logpath=/usr/local/mongodb1/logs/mongodb.log
pidfilepath=/usr/local/mongodb1/mongodb.pid
#keyFile=/opt/mongodb-linux-x86_64-3.2.9/bin/mongodb_keyfile
directoryperdb=true
logappend=true
replSet=rs1
bind_ip=192.168.159.137
port=27017
oplogSize=100
fork=true
noprealloc=true
auth=false
配置文件创建完成后,在每台机器启动mongodb服务
./mongod -f mongodb.conf
服务启动后,我们可以开展配置,主,备,仲裁节点
连接到任意一台服务器,切换到admin数据库
./mongo 192.168.159.135
use admin
cfg={ _id:"rs1", members:[ {_id:0,host:'192.168.159.135:27017',priority:2}, {_id:1,host:'192.168.159.136:27017',priority:1}, {_id:2,host:'192.168.159.137:27017',arbiterOnly:true}] };
rs.initiate(cfg)
执行成功后,出现如下信息
{ "ok" : 1 }
priority表示优先级别,数值越大,表示是主节点
arbiterOnly:true表示仲裁节点
可以执行rs.status()来检查执行配置结果信息,如果执行成功,会出现如下信息
{
"set" : "rs1",
"date" : ISODate("2016-10-24T06:08:10.946Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.159.135:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 335,
"optime" : {
"ts" : Timestamp(1477289289, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-24T06:08:09Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1477289288, 1),
"electionDate" : ISODate("2016-10-24T06:08:08Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.159.136:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 13,
"optime" : {
"ts" : Timestamp(1477289289, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-24T06:08:09Z"),
"lastHeartbeat" : ISODate("2016-10-24T06:08:10.897Z"),
"lastHeartbeatRecv" : ISODate("2016-10-24T06:08:10.851Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.159.135:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.159.137:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 13,
"lastHeartbeat" : ISODate("2016-10-24T06:08:10.896Z"),
"lastHeartbeatRecv" : ISODate("2016-10-24T06:08:09.846Z"),
"pingMs" : NumberLong(1),
"configVersion" : 1
}
],
"ok" : 1
}
"stateStr" : "PRIMARY"表示主节点, "stateStr" : "SECONDARY"表示从节点, "stateStr" : "ARBITER",表示仲裁节点
4.客户端连接
客户端连接主节点,插入数据,插入后,可以看到数据在备份节点上也进行了同步。
./mongo 192.168.159.135
use user
db.users.insert({Name: "zhang",Age: 50})
也可以使用客户端连接工具进行操作
robomongo
备注
1. 关闭mongodb命令
[root@localhost bin]# ps -ef | grep mongod
root 2966 1 0 19:40 ? 00:01:30 ./mongod -f mongodb.conf
root 5741 2547 0 22:58 pts/1 00:00:00 grep mongod
[root@localhost bin]# kill -9 2966
2.出现如下错误,可以清空db文件夹下试试
note: noprealloc may hurt performance in many applications
2016-10-23T19:41:26.822-0700 I CONTROL [main]
2016-10-23T19:41:26.822-0700 W CONTROL [main] 32-bit servers don't have journaling enabled by default. Please use --journal if you want
durability.
2016-10-23T19:41:26.822-0700 I CONTROL [main]
about to fork child process, waiting until server is ready for connections.
forked process: 6756
ERROR: child process failed, exited with error number 100
You have new mail in /var/spool/mail/root