共计 7145 个字符,预计需要花费 18 分钟才能阅读完成。
准备工作
软件环境
OS:Oracle Linux 6.5
MongoDB:mongodb3.2.3
部署架构
模拟有三台服务器
10.1.5.123:28001 主节点
10.1.5.123:28002 从节点
10.1.5.123:28003 选举节点
复制集架构:一主,一从,一选举节点
下载安装
下载地址:https://www.mongodb.org/downloads
mongodb-linux-x86_64-rhel62-3.2.3.tgz
部署步骤
1. 创建目录及用户
[root@devtest mongodb]# mkdir conf data log
[root@devtest data]# mkdir 28001 28002 28003
– 创建 mongo 用户
[root@devtest conf]# groupadd mongod
[root@devtest conf]# useradd -g mongod mongod
2. 创建配置文件
– 配置三个实例的配置文件(修改相应的端口号即可)
– 节点 1
$ vi /home/mongod/mongodb3.2.3/conf/28001.conf
port=28001
bind_ip=10.1.5.123
logpath=/home/mongod/mongodb3.2.3/log/28001.log
dbpath=/home/mongod/mongodb3.2.3/data/28001/
logappend=true
pidfilepath=/home/mongod/mongodb3.2.3/data/28001/28001.pid
fork=true
oplogSize=1024
replSet=MyMongo
– 节点 2
$ vi /home/mongod/mongodb3.2.3/conf/28002.conf
port=28002
bind_ip=10.1.5.123
logpath=/home/mongod/mongodb3.2.3/log/28002.log
dbpath=/home/mongod/mongodb3.2.3/data/28002/
logappend=true
pidfilepath=/home/mongod/mongodb3.2.3/data/28002/28002.pid
fork=true
oplogSize=1024
replSet=MyMongo
– 节点 3
$ vi /home/mongod/mongodb3.2.3/conf/28003.conf
port=28003
bind_ip=10.1.5.123
logpath=/home/mongod/mongodb3.2.3/log/28003.log
dbpath=/home/mongod/mongodb3.2.3/data/28003/
logappend=true
pidfilepath=/home/mongod/mongodb3.2.3/data/28003/28003.pid
fork=true
oplogSize=1024
replSet=MyMongo
3. 启动 mongo 复制集
$ mongod -f /home/mongod/mongodb3.2.3/conf/28001.conf
$ mongod -f /home/mongod/mongodb3.2.3/conf/28002.conf
$ mongod -f /home/mongod/mongodb3.2.3/conf/28003.conf
4. 初始化复制集
[mongod@devtest ~]$ mongo 10.1.5.123:28001/admin
MongoDB shell version: 3.2.3
connecting to: 10.1.5.123:28001/admin
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
> db
admin
> config = {
… “_id”:”MyMongo”,
… members:[
…{“_id”:0,host:”10.1.5.123:28001″},
…{“_id”:1,host:”10.1.5.123:28002″},
… {“_id”:2,host:”10.1.5.123:28003″}]
… }
{
“id” : “MyMongo”,
“members” : [
{
“_id” : 0,
“host” :”10.1.5.123:28001″
},
{
“_id” : 1,
“host” :”10.1.5.123:28002″
},
{
“_id” : 2,
“host” :”10.1.5.123:28003″
}
]
}
– 查看复制集成员
> config.members
[
{
“_id” : 0,
“host” :”10.1.5.123:28001″
},
{
“_id” : 1,
“host” :”10.1.5.123:28002″
},
{
“_id” : 2,
“host” :”10.1.5.123:28003″
}
]
– 把节点 3 修改为 arbiter 选举节点
> config.members[2]
{“_id” : 2, “host” :”10.1.5.123:28003″}
> config.members[2] ={“_id”:2,”host”:”10.1.5.123:28003″,arbiterOnly:true}
{“id” : 0, “host” :”10.1.5.123:28003″, “arbiterOnly” : true}
– 进行复制集初始化
> rs.initiate(config)
{“ok” : 1}
– 查看复制集状态
MyMongo:OTHER> rs.status()
{
“set” : “MyMongo”,
“date” :ISODate(“2016-03-15T09:42:47.643Z”),
“myState” : 1,
“term” : NumberLong(1),
“heartbeatIntervalMillis” :NumberLong(2000),
“members” : [
{
“_id” : 0,
“name” :”10.1.5.123:28001″,
“health” : 1,
“state” : 1,
“stateStr” :”PRIMARY”,
“uptime” :1812,
“optime” : {
“ts”: Timestamp(1458034899, 1),
“t” :NumberLong(1)
},
“optimeDate”: ISODate(“2016-03-15T09:41:39Z”),
“infoMessage”: “could not find member to sync from”,
“electionTime” : Timestamp(1458034898, 1),
“electionDate”: ISODate(“2016-03-15T09:41:38Z”),
“configVersion” : 1,
“self” : true
},
{
“_id” : 1,
“name” :”10.1.5.123:28002″,
“health” : 1,
“state” : 2,
“stateStr” :”SECONDARY”,
“uptime” :80,
“optime” : {
“ts”: Timestamp(1458034899, 1),
“t” :NumberLong(1)
},
“optimeDate”: ISODate(“2016-03-15T09:41:39Z”),
“lastHeartbeat” :ISODate(“2016-03-15T09:42:46.947Z”),
“lastHeartbeatRecv” :ISODate(“2016-03-15T09:42:46.445Z”),
“pingMs” :NumberLong(0),
“syncingTo” :”10.1.5.123:28001″,
“configVersion” : 1
},
{
“_id” : 2,
“name” :”10.1.5.123:28003″,
“health” : 1,
“state” : 7,
“stateStr” :”ARBITER”,
“uptime” :80,
“lastHeartbeat” :ISODate(“2016-03-15T09:42:46.946Z”),
“lastHeartbeatRecv” :ISODate(“2016-03-15T09:42:44.428Z”),
“pingMs” :NumberLong(0),
“configVersion”: 1
}
],
“ok” : 1
}
MyMongo:PRIMARY>
5. 验证复制集同步
分别登录两从节点查看登录状态,分别为 secondary 和 arbite
下面登录从 secondary 节点进行测试:
在从节点进行查询时报下面的错误,因为在默认情况下,不通过驱动连接 mongodb 从节点数据库时,如果不开始 slaveOK=true,是无法读取从节点数据的。
[mongod@devtest conf]$ mongo10.1.5.123:28002/admin
MongoDB shell version: 3.2.3
connecting to:10.1.5.123:28002/admin
MyMongo:SECONDARY> show dbs
2016-03-15T17:49:47.854+0800 EQUERY [thread1] Error: listDatabasesfailed:{“ok” : 0, “errmsg” : “not master andslaveOk=false”, “code” : 13435} :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1
shellHelper.show@src/mongo/shell/utils.js:700:19
shellHelper@src/mongo/shell/utils.js:594:15
@(shellhelp2):1:1
MyMongo:SECONDARY>rs.slaveOk(true)
MyMongo:SECONDARY> show dbs
local 0.000GB
test 0.000GB
MyMongo:SECONDARY> use test
switched to db test
MyMongo:SECONDARY> showtables
t1
MyMongo:SECONDARY>db.t1.find()
{“_id” :ObjectId(“56e7dc310ebd75f9ef3546c1”), “name” :”licz” }
下面登录从 arbite 节点进行测试:
然后登录 arbite 节点,会发现数据没有同步,因为 arbite 不参与数据库的同步,即不存储数据
但 local 数据库大小已经改变了,因为 local 库存储命名空间(local.ns 文件)的内容。
MyMongo:ARBITER> rs.slaveOk(true)
MyMongo:ARBITER> show dbs
local 0.000GB
6. 模拟主 - 从故障切换
– 杀掉当前主节点为 28001 端口
[mongod@devtest ~]$ ps -ef|grep mongo
root 7186 7150 0 09:15 pts/2 00:00:00 su – mongod
mongod 7187 7186 0 09:15 pts/2 00:00:00 -bash
mongod 7306 7187 0 09:16 pts/2 00:00:00 ps -ef
mongod 7307 7187 0 09:16 pts/2 00:00:00 grep mongo
mongod 16253 1 2 Mar15 ? 00:22:00 mongod -f/home/mongod/mongodb3.2.3/conf/28001.conf
mongod 16299 1 2 Mar15 ? 00:21:50 mongod -f/home/mongod/mongodb3.2.3/conf/28002.conf
mongod 16345 1 2 Mar15 ? 00:20:05 mongod -f/home/mongod/mongodb3.2.3/conf/28003.conf
[mongod@devtest ~]$ kill -9 16253
– 发现主节点已经切换至 28002 端口
[mongod@devtest ~]$ mongo 10.1.5.123:28002/admin
MongoDB shell version: 3.2.3
connecting to: 10.1.5.123:28002/admin
MyMongo:PRIMARY>
– 再次启动 28001 端口并登录,发现 28001 已为从节点
[mongod@devtest ~]$ mongod -f /home/mongod/mongodb3.2.3/conf/28001.conf
about to fork child process, waiting untilserver is ready for connections.
forked process: 21410
child process started successfully, parentexiting
[mongod@devtest ~]$ mongo 10.1.5.123:28001/admin
MongoDB shell version: 3.2.3
connecting to: 10.1.5.123:28001/admin
MyMongo:SECONDARY>
更多 MongoDB 相关教程见以下内容:
CentOS 编译安装 MongoDB 与 mongoDB 的 php 扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm
CentOS 6 使用 yum 安装 MongoDB 及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm
Ubuntu 13.04 下安装 MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm
MongoDB 入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm
Ubunu 14.04 下 MongoDB 的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm
《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm
Nagios 监控 MongoDB 分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm
基于 CentOS 6.5 操作系统搭建 MongoDB 服务 http://www.linuxidc.com/Linux/2014-11/108900.htm
MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130010.htm