CentOS 6.4 creates a copy set of Mongodb

  • 2020-06-07 05:29:28
  • OfStack

MongoDB is an open source non-ES3en database engine. MongoDB is extensible and an alternative to the standard relational Database management System (RDBMS). Replica sets enable you to provide access to your data in the event of node failure.

Install MongoDB

1. Ensure that hostname is set for each member of the replica set
nano /etc/hostname

2. Create a file to hold the configuration information for the MongoDB repository:
sudo touch /etc/yum.repos.d/mongodb.repo

3. If you are running a 64-bit system, use the following configuration:

name=MongoDB Repository

For 32-bit systems, use the following configuration:

name=MongoDB Repository

4. Install MongoDB using the following command:
sudo yum install mongo-10gen-server

Configure the network

Configure the network correctly or you will not be able to add members to the replica set. This section details how to configure 3 (3) servers as MongoDB replicas.
Set up the hosts file
/etc/hosts mongo1 mongo2 mongo3

Use your own IP address instead of the address in the example above. The member names in the replica set can also be set according to your needs.

Edit the Mongo Conf file
Edit the ES65en.conf file to add the IP address and port number.
/ etc mongod. conf:

# fork and run in background
fork = true
bind_ip =
port = 27017

Enter the private IP address of your server in bind ip. If bind_ip does not exist, you need to add it. Leave the default port number 27017 and uncomment the line fork = true.
2. Still scroll to the bottom of the mongodb.conf file and add the copy set information:
/ etc mongod. conf:
replSet = rs1

In this example, the replica set is rs1, but you can change the name depending on your choice.

A copy of the set

The replica set will allow your data to "copy" or propagate to all other nodes in the collection. It provides redundancy in the event of a system failure. It is recommended that the replica set has an odd number of nodes because this makes voting easier.
The election is to choose which node becomes the master node. Elections occur after the replica set is initialized and when the primary node is unavailable. The primary node is the only one that can accept writes. If the primary node is not available, an election is held to select a new primary node. Elections are conducted automatically without human intervention.

Creating a replica set

The ES108en. conf file is created during installation. This configuration file is needed to start the daemon on each node of the replica set.

1. The orders are as follows:

mongod --config /etc/mongod.conf

When the daemon starts, the output is as follows.
[user@europa mongo]# mongod? config /etc/ mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 20955
all output going to: /var/log/mongo/mongod.log
child process started successfully, parent exiting

2. Start the MongoDB client on only one node of the replica set:
mongo --host < mongo0 >

3. At the prompt of MongoDB, use the command to switch to admin:
use admin

You should see the message switched to db admin.

4. Run the rs. initiate() command, which creates a replica set in the current node. The output should look like the following:

> rs.initiate()
 " info2 "  :  " no configuration explicitly specified  -  making one " ,
 " me "  :  " " ,
 " info "  :  " Config now saved locally. Should come online in about a minute. " ,
 " ok "  : 1

5. To view the current configuration, run the command:

The output should look like the following:

  "_id" : "rs1",
  "version" : 8,
  "members" : [
      "_id" : 0,
      "host" : ""

6. To add a member to a replica set, use the command:

rs1:PRIMARY > rs. add (" mongo2:27017 ")
{" ok ": 1}

7. To verify that the node has been added correctly, run the rs.conf () command again. The output should look like the following:

rs1:PRIMARY> rs.conf()
 " _id "  :  " rs0 " ,
 " version "  : 8,
 " members "  : [
 " _id "  : 0,
 " host "  :  " " 
 " _id "  : 1,
 " host "  :  " mongo1:27017 " 
 " _id "  : 2,
 " host "  :  " mongo2:27017 " 

Verify copy set
The best way to verify that the replica set is healthy and that the nodes are communicating properly is to create a new test database. By default, when you connect to MongoDB, the existing test database will be used. To save the new database, you need to add data. The process of creating and inserting data is as follows:
Create a database
use < products >
Replace the variable products with whatever name you like.
2. Add data
db.products.insert( {item: "paint", qty: 10 } )
If you are not on the primary node of the replica set, you will receive the message not master. Switch to the master node and run the command again. Now use the command:
show dbs
Displays a list of databases. Your new should appear in the list. Connect to other nodes in the replica set to see if the newly created database has been replicated.

Related articles: