Replica Set Deployment Tutorials—副本設置佈署教學

資料來源:http://docs.mongodb.org/manual/administration/replica-set-deployment/

The following tutorials provide information in deploying replica sets.

Deploy a Replica Set—佈署一個副本設置

Configure a three-member replica set for either a development or production system.

Note

設置一個由三個成員數組成的副本來進行開發或生產(production)階段的系統

資料來源:http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

Procedures—步驟

These procedures assume you already have instances of MongoDB installed on the systems you will add as members of your replica set. If you have not already installed MongoDB, see the installation tutorials.

Note

請先確認你已經安裝了mongodb,如果你還沒安裝請參考 installation tutorials

Deploy a Development or Test Replica Set

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

Note

這個範例會創見一個新的副本集(replica set)命名為 rs0

1.Before creating your replica set, verify that every member can successfully connect to every other member. The network configuration must allow all possible connections between any two members. To test connectivity, see Test Connections Between all Members .

2.Start three instances of mongod as members of a replica set named rs0, as described in this step. For ephemeral tests and the purposes of this guide, you may run the mongod的 instances in separate windows of GNU Screen. OS X and most Linux distributions come with screen installed by default [2] systems.

Note

1.開始前要先測試所有成員之間的連線是否正常, 請參考 測試所有成員間的連線

2.在示範中啟動三台伺服器來作為rs0的成員,在本指南與這個簡短的測試中,你可以執行這些 mongod的 實際範例在單獨的GNU畫面。 在os X 以及大多數發行的linux版本的系統預設。

a.Create the necessary data directories by issuing a command similar to the following:

Note

建立資料夾目錄:

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

b.Issue the following commands, each in a distinct screen window:

Note

在每台伺服器的終端機內各自打入以下指令:

mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128
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. If you are already using these ports, select different ports.

The –smallfiles and –oplogSize reduce the disk space that each mongod uses for the purposes of testing. For more information on these and other configuration options, see Configuration File Options.

Note

上述指令會開始在指定的port上執行rs0的副本集,如果正在使用該port口的請關掉或另行指定。

–smallfiles 與 –oplogSize 這兩個參數是用來減少每個mongod用於測試的磁碟空間。對於這些設定選項請參考 配置文件

Note

如果執行後出現error訊息表示27017已被佔用,可以先用

ps aux |grep mongo

Note

確認一下是否有mongo正在被執行。

然後以:

service mongodb stop

/etc/init.d/mongodb stop

停止正在執行的mongodb

Note

上述指令執行後如果如果出現:

[rsStart] replSet can’t get local.system.replset config from self or any seed (EMPTYCONFIG)

的訊息,是在說你要執行的副本集沒有設定,將指令以背景執行如下,

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

Note

或是以另一個視窗執行,然後以新視窗開啟mongodb以:

$/etc/init.d/mongodb start

Note

之後接續以下設定。

Warning

重要:

除非有另外特別說明,否則這些教學過程僅用於測試佈署使用。

3.Open a mongo shell and connect to the first mongod instance, with the following command:

Note

在第一台(主要)機器上用以下指令進入進入mongo的shell:

mongo --port 27017

4.Create a replica set configuration object in the mongo shell environment to use to initiate the replica set with the following sequence of operations:

Note

在mongo內創見一個副本集的配置設定object,用來啟動副本設置(replica set),請依照下面的方式:

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

5.Use rs.initiate() to initiate a replica set consisting of the current member and using the default configuration:

Note

使用 rs.initiate() 命令來啟動一個包含現在副本成員在內的預設副本設置(replica set)

rs.initiate( rsconf )

6.Display the current replica configuration :

Note

用這個指令顯示現在的 副本設定

rs.conf()

7.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:

Note

新增成員到這個副本集,換掉下面的<hostname>換成你要設定的主機名稱。

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

After these commands return you have a fully functional replica set. New replica sets elect a primary within a few seconds.

Note

送出指令後你會有一個完整的副本集,並會在幾秒鐘內確認誰是這個副本集的primary 。

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

Note

在任何時候都可以用 rs.status() 來確認副本集的狀態。

Deploy a Production Replica Set

Production replica sets are very similar to the development or testing deployment described above, with the following differences:

Note

生產(Production)階段的副本集與開發測試階段的佈署是非常相似的,主要區別如下述:

  • Each member of the replica set resides on its own machine, and the MongoDB processes all bind to port 27017, which is the standard MongoDB port.
  • Each member of the replica set must be accessible by way of resolvable DNS or hostnames in the following scheme:
mongodb0.example.net

mongodb1.example.net

mongodb2.example.net

Note

  • 每位成員都在不同的機器上,mongo的port都綁定在27017的標準預設端口。
  • 在此方案內,副本集內的成員都必須是可以透過DNS或是hostname訪問。
mongodb0.example.net

mongodb1.example.net

mongodb2.example.net

Configure DNS names appropriately, or set up your systems’ /etc/hosts file to reflect this configuration.

Note

如果要照此教學使用,請更改您的hostname,(在 /etc/hosts 內) 以符合上述名稱。

Note

具體作法就是在 /etc/hosts 文件中將連同自己在內的ip位置與hostname寫入

如上述假設各自ip為:

mongodb0.example.net => 192.168.1.101

mongodb1.example.net => 192.168.1.102

mongodb2.example.net => 192.168.1.103

則在所有的機器的 hosts 文件內就要加入:

192.168.1.101 mongodb0.example.net

192.168.1.102 mongodb1.example.net

192.168.1.103 mongodb2.example.net

  • You specify run-time configuration on each system in a configuration file stored in /etc/mongodb.conf or in a related location. You do not specify run-time configuration through command line options.

Note

你可以將每個系統上執行時的 設定文件 儲存在 /etc/mongodb.conf 或其他相關位置。

不存程設定文件的話每次執行都要在命令列上執行。

For each MongoDB instance, use the following configuration. Set configuration values appropriate to your systems:

Note

對於每個範例請參考下面的設定,設定適當的系統設定值:

port = 27017

bind_ip = 10.8.0.10

dbpath = /srv/mongodb/

fork = true

replSet = rs0

You do not need to specify an interface with bind_ip . However, if you do not specify an interface, MongoDB listens for connections on all available IPv4 interfaces. Modify bind_ip to reflect a secure interface on your system that is able to access all other members of the set and on which all other members of the replica set can access the current member. The DNS or host names must point and resolve to this IP address. Configure network rules or a virtual private network (i.e. “VPN”) to permit this access.

For more documentation on run time options used above and on additional configuration options, see Configuration File Options .

Note

上面的設定中,你可以不指定 bind_ip 的port,預設上 bind_id 會直接監聽所有ipv4可使用的連接口(port),你可以修改bind_ip反應到你系統上的一個安全的連接口,用來讓 副本集內的成員可以正確的訪問到這個成員。

DNS或是hostname都必要要能解析這個ip地址,設定網路規則或是虛擬的私人網路(VPN),以允許這些訪問。

更多設定請參考 設定文件

To deploy a production replica set:

Before creating your replica set, verify that every member can successfully connect to every other member. The network configuration must allow all possible connections between any two members. To test connectivity, see Test Connections Between all Members.

On each system start the mongod process by issuing a command similar to following:

Note

佈署一個生產(production)階段的副本集合:

要創建副本集,確認每一個成員可以成功的連結的其他任一成員,網路配置必須允許任意兩個成員之間所有可能的連接。 請參考 測試所有成員之間的連接

用以下指令啟動mongodb的進程:

mongod --config /etc/mongodb.conf

Open a mongo shell connected to this host:

mongo

Use rs.initiate() to initiate a replica set consisting of the current member and using the default configuration:

Note

使用mongodb.conf的設定檔內的設定:

rs.initiate()

Display the current replica configuration:

Note

用下面的指令確認:

rs.conf()

Add two members to the replica set by issuing a sequence of commands similar to the following:

Note

用下面的指令新增另外兩台伺服器到副本集內。

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

After these commands return you have a fully functional replica set. New replica sets elect a primary within a few seconds.

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