Home  >  Article  >  Database  >  MongoDB副本集搭建以及增减机器

MongoDB副本集搭建以及增减机器

WBOY
WBOYOriginal
2016-06-07 16:44:221023browse

1. replica set 每个副本集需要设置副本集名称。 在启动MongoDB时指定。 2. 搭建步骤 2.1 启动 首先启动MongoDB 启动时设置为副本

1. replica set

每个副本集需要设置副本集名称。

在启动MongoDB时指定。

2. 搭建步骤

2.1 启动

首先启动MongoDB

启动时设置为副本集模式,并设置名称。

两种方式,通过配置文件,或命令行模式。

配置文件:

01.# mongodb.conf 
02. 
03.replSet = replsetname 

命令行模式:

mongod --replSet "replsetname" 

2.2 mongo shell

打开mongo shell

$ ./bin/mongo 

2.3 初始化

在mongo shell中Use rs.initiate():初始化副本集。

> rs.initiate(): 

2.4 验证

验证初始化是否成功。

> rs.conf(); 

返回如下结果。

01.{ 
02.  "_id" : "replsetname", 
03.  "version" : 1, 
04.  "members" : [ 
05.      { 
06.        "_id" : 1, 
07.        "host" : "mongodb0.example.net:27017" 
08.      } 
09.  ] 
10.} 

2.5 增加一个实例

使用rs.add增加实例

> rs.add("mongodb1.example.net") 

3. 查看副本集状态

通过下面命令查看

> rs.status() 

返回下面信息。

01.{ 
02.      "set" : "replisetname", 
03.      "date" : ISODate("2014-08-23T07:43:51Z"), 
04.      "myState" : 2, 
05.      "members" : [ 
06.              { 
07.                      "_id" : 0, 
08.                      "name" : "mongodb0.example.net:2701", 
09.                      "health" : 1, 
10.                      "state" : 2, 
11.                      "stateStr" : "SECONDARY", 
12.                      "uptime" : 3816405, 
13.                      "optime" : Timestamp(1408335342, 1), 
14.                      "optimeDate" : ISODate("2014-08-18T04:15:42Z"), 
15.                      "lastHeartbeat" : ISODate("2014-08-23T07:43:51Z"), 
16.                      "lastHeartbeatRecv" : ISODate("2014-08-23T07:43:50Z"), 
17.                      "pingMs" : 1, 
18.              }, 

4. 删除一个节点

删除使用remove

rs.remove("mongod3.example.net:27017") 

--------------------------------------分割线 --------------------------------------

CentOS编译安装MongoDB

CentOS 编译安装 MongoDB与mongoDB的php扩展

CentOS 6 使用 yum 安装MongoDB及服务器端配置

Ubuntu 13.04下安装MongoDB2.4.3

MongoDB入门必读(概念与实战并重)

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

--------------------------------------分割线 --------------------------------------

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn