Deploy a Replica Set for Testing and Development单机器测试副本集
cookqq ›博客列表 ›MongoDB

Deploy a Replica Set for Testing and Development单机器测试副本集

2015-11-26 17:16:42.0|分类: MongoDB|浏览量: 1920

摘要: 在一台电脑测试副本集,副本集成员开启不同的端口号27017,设置不同的mongodb数据路径

在一台电脑测试副本集,副本集成员开启不同的端口号27017,设置不同的mongodb数据路径

This procedure describes deploying a replica set in a development or test environment. For a production deployment, refer to the Deploy a Replica Set tutorial.

This tutorial describes how to create a three-member replica set from three existing mongod instances running with access control disabled.

To deploy a replica set with enabled access control, see Deploy Replica Set and Configure Authentication and Authorization. If you wish to deploy a replica set from a single MongoDB instance, see Convert a Standalone to a Replica Set. For more information on replica set deployments, see the Replication andReplica Set Deployment Architectures documentation.

Overview

Three member replica sets provide enough redundancy to survive most network partitions and other system failures. These sets also have sufficient capacity for many distributed read operations. Replica sets should always have an odd number of members. This ensures that elections will proceed smoothly. For more about designing replica sets, see the Replication overview.

The basic procedure is to start the mongod instances that will become members of the replica set, configure the replica set itself, and then add the mongod instances to it.

Requirements

For test and development systems, you can run your mongod instances on a local system, or within a virtual instance.

Before you can deploy a replica set, you must install MongoDB on each system that will be part of yourreplica set. If you have not already installed MongoDB, see the installation tutorials.

Before creating your replica set, you should verify that your network configuration allows all possible connections between each member. For a successful replica set deployment, every member must be able to connect to every other member. For instructions on how to check your connection, see Test Connections Between all Members.

Considerations

Replica Set Naming

IMPORTANT

These instructions should only be used for test or development deployments.

The examples in this procedure create a new replica set named rs0.

If your application connects to more than one replica set, each set should have a distinct name. Some drivers group replica set connections by replica set name.

You will begin by starting three mongod instances as members of a replica set named rs0.

Procedure

  1. Create the necessary data directories for each member by issuing a command similar to the following: 创建数据文件

    mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2

    This will create directories called “rs0-0”, “rs0-1”, and “rs0-2”, which will contain the instances’ database files.

  2. Start your mongod instances in their own shell windows by issuing the following commands:

    First member:

    mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128

    Second member:

    mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128

    Third member:

    mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128

    This starts each instance as a member of a replica set named rs0, each running on a distinct port, and specifies the path to your data directory with the --dbpath setting. If you are already using the suggested ports, select different ports.

    The --smallfiles and --oplogSize settings reduce the disk space that each mongod instance uses. This is ideal for testing and development deployments as it prevents overloading your machine. For more information on these and other configuration options, see Configuration File Options.

  3. Connect to one of your mongod instances through the mongo shell. You will need to indicate which instance by specifying its port number. For the sake of simplicity and clarity, you may want to choose the first one, as in the following command;

    mongo --port 27017
  4. In the mongo shell, use rs.initiate() to initiate the replica set. You can create a replica set configuration object in the mongo shell environment, as in the following example:

    rsconf = {
               _id: "rs0",
               members: [
                          {
                           _id: 0,
                           host: "<hostname>:27017"
                          }
                        ]
             }

    replacing <hostname> with your system’s hostname, and then pass the rsconf file tors.initiate() as follows:

    rs.initiate( rsconf )
  5. Display the current replica configuration by issuing the following command:

    rs.conf()

    The replica set configuration object resembles the following

    {
       "_id" : "rs0",
       "version" : 4,
       "members" : [
          {
             "_id" : 1,
             "host" : "localhost:27017"
          }
       ]}
  6. In the mongo shell connected to the primary, add the second and third mongod instances to the replica set using the rs.add() method. Replace <hostname> with your system’s hostname in the following examples:

    rs.add("<hostname>:27018")rs.add("<hostname>:27019")

    When complete, you should have a fully functional replica set. The new replica set will elect a primary.

Check the status of your replica set at any time with the rs.status() operation.

SEE ALSO

The documentation of the following shell functions for more information:

  • rs.initiate()

  • rs.conf()

  • rs.reconfig()

  • rs.add()

You may also consider the simple setup script as an example of a basic automatically-configured replica set.

Refer to Replica Set Read and Write Semantics for a detailed explanation of read and write semantics in MongoDB.


一键分享文章

分类列表

  • • struts源码分析
  • • flink
  • • struts
  • • redis
  • • kafka
  • • ubuntu
  • • zookeeper
  • • hadoop
  • • activiti
  • • linux
  • • 成长
  • • NIO
  • • 关键词提取
  • • mysql
  • • android studio
  • • zabbix
  • • 云计算
  • • mahout
  • • jmeter
  • • hive
  • • ActiveMQ
  • • lucene
  • • MongoDB
  • • netty
  • • flume
  • • 我遇到的问题
  • • GRUB
  • • nginx
  • • 大家好的文章
  • • android
  • • tomcat
  • • Python
  • • luke
  • • android源码编译
  • • 安全
  • • MPAndroidChart
  • • swing
  • • POI
  • • powerdesigner
  • • jquery
  • • html
  • • java
  • • eclipse
  • • shell
  • • jvm
  • • highcharts
  • • 设计模式
  • • 列式数据库
  • • spring cloud
  • • docker+node.js+zookeeper构建微服务
版权所有 cookqq 感谢访问 支持开源 京ICP备15030920号
CopyRight 2015-2018 cookqq.com All Right Reserved.