MongoDB is an open-source non-sql database engine. MongoDB is extensible and is a substitute for standard relational database management systems (RDBMS). A replica set can also provide access to your data when a node fails.
Install MongoDB
1. Ensure that each member of the replica set is set hostname
Nano/etc/hostname
/etc/hostname:
Europa
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:
[MongoDB]
Name=mongodb Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
For 32-bit systems, use the following configuration:
[MongoDB]
Name=mongodb Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1
4. Install MongoDB using the following command:
sudo yum install Mongo-10gen-server
Configure Network
Configure the network correctly, or you will not be able to add members to the replica set. This section details how to configure a three (3) server as a MongoDB replica set.
set up Hosts file
/etc/hosts
192.168.160.1 Mongo1
192.168.170.1 Mongo2
192.168.180.1 Mongo3
Replace the address in the example above with your own IP address. The member names in the replica set can also be named according to your needs.
Edit MONGO conf file
1. Edit the mongod.conf file to add the IP address and port number.
/etc/mongod.conf:
# fork and run in background
fork = true
bind_ip = 192.168.135.24
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 replica set information:
/etc/mongod.conf:
Replset = rs1
In this example, the replica set is Rs1, but you can change the name according to the selection.
Replica 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 number of replica set nodes is odd because it makes the election easier.
The election is the choice of which node becomes the primary node. The election occurs after the replica set is initialized and the primary node is not available. The master node is the only node that can accept write operations. If the primary node is unavailable, elect the new master node. The election operation is carried out automatically without human intervention.
Create a replica set
The mongod.conf file is created during the installation process. You need to start the daemon on each node of the replica set with this profile.
1. The order is as follows:
Mongod--config/etc/mongod.conf
After the daemon starts, the output is as follows.
[User@europa mongo]# mongod–config/etc/mongod.conf
About to fork the child process, the 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 only on one node of the replica set:
MONGO--host <mongo0>
3. At the MongoDB prompt, 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 will create the replica set in the current node. The output should resemble the following:
> rs.initiate ()
{
"Info2": "No configuration explicitly specified-making one",
"Me": "192.168.160.1:27 017 ",
" info ":" Config now saved locally. Should come online in about a minute. ",
" OK ": 1
5. To view the current configuration, run the command:
rs.conf ()
The output should resemble the following:
Rs.conf ()
{
"_id": "Rs1",
"version": 8,
"members": [
{
"_id": 0, "
Host": " 192.168.160.1:27017 "
}
]
}
6. To add members to the replica set, use the command:
Rs.add ("mongo1:27017")
Output:
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 resemble the following:
Rs1:primary> rs.conf ()
{
"_id": "Rs0",
"version": 8,
"members": [
{
"_id": 0,
"host" : "192.168.160.1:27017"
},
{
"_id": 1,
"host": "mongo1:27017"
},
{
"_id": 2,
' host ': ' mongo2:27017 '
}
]
}
Verifying replica Sets
the best way to verify that the replica set is normal and that the nodes are communicating normally is to create a new test database. By default, when you connect to MongoDB, the existing test database is used. In order to save the new database, you need to add data. The process of creating and inserting data is as follows:
1. Create a database
Use <products>
Replace the variable products with any name you like.
2. Add Data
Db.products.insert ({item: "Paint", qty:10})
If you are not on the master 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 the other nodes of the replica set to see if the newly created database has been replicated.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.