Creating a replica set

Starting a MongoDB server as part of a replica set is as easy as setting it in the configuration via the command line:

> mongod --replSet "xmr_cluster"

This is fine for development purposes. For production environments it's recommended that we use a configuration file instead:

> mongod --config <path-to-config>

Here, <path-to-config> can be as follows:

/etc/mongod.conf

This configuration file has to be in YAML format.

YAML does not support tabs. Convert tabs to spaces using your editor of choice.

A sample, simple as possible configuration file looks as follows:

systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27017
replication:
oplogSizeMB: <int>
replSetName: <string>

Root level options define the section which leaf level options apply to, by nesting.

Regarding replication, the mandatory options are oplogSizeMB (the oplog size for the member in MB) and replSetName (the replica set name, such as xmr_cluster).

We can also set the following on the same level as replSetName:

secondaryIndexPrefetch: <string>

This is only available for the MMAPv1 storage engine and refers to the indexes on secondaries that will get loaded into memory before applying operations from the oplog.

It defaults to all and available options are none and _id_only to load no indexes into memory and only load the default index created on _id fields.

enableMajorityReadConcern: <boolean>

This is the configuration setting to enable read preference of majority for this member.

After we have started all replica set processes on different nodes, we log in to one of the nodes using mongo from the command line with the appropriate host:port.

Then we need to initiate the cluster from one member.

If we use configuration files it is as follows:

> rs.initiate()

Or we can pass in configuration as a document parameter:

> rs.initiate( {
_id : "xmr_cluster",
members: [ { _id : 0, host : "host:port" } ]
})
We can verify that the cluster got initiated by using rs.conf() in the shell.

Following that, we add each other member to our replica set using the host:port that we have defined in our networking setup:

> rs.add("host2:port2")
> rs.add("host3:port3")
The minimum number of servers we must use for a HA replica set is 3. We could replace one of the servers with an arbiter but this is not recommended. Once we add all servers and wait a bit, we can check the status of our cluster by using rs.status().
Oplog by default will be 5% of free disk space. If we want to define it when we create our replica set we can do so by passing the command-line parameter --oplogSizeMB or replication.oplogSizeMB in our configuration file. An oplog size can not be more than 50 GB.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.8.91