This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
After last
This article builds a 3-node MongoDB replication set by practical operation. Normally, the different nodes of the replica set are on different servers, but because this is a case study, the difference between building a replica set on one machine is that you need to start three MongoDB instances on the same machine listening to three different port numbers, and nothing else is different.
The main steps are as follows:
- Install the mongo
- Configure the port number, data directory, and replication set information in the configuration file
- Starting a MongoDb Instance
- Configuring a Replication Set
- test
Install the mongo
Installing MongoDB is very simple. You can either download MongoDB for your operating system or install it using the package manager.
In this case, we installed it in a Linux environment using the.tgz package downloaded from the official website.
As shown in the figure above, select the corresponding operating system and version number to download. Or copy the download link and use curl to download.
# download
$ curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-5.0.0.tgz
# decompression$tar - XVF mongo - Linux - x86_64 - rhel80-5.0.0. TGZMove to a specified directory (optional)$mongo mv - Linux - x86_64 - rhel80-5.0.0 / / opt /Copy the code
You can also add the bin directory in the MongoDB directory to the system variables to facilitate later use of commands such as mongod.
$ export PATH=$PATH: / opt/mongo - Linux - x86_64 rhel80-5.0.0 / binCopy the code
Configuring a MongoDB Instance
Because we are starting three MongoDB instances, we need to create three data directories and configuration files. Generally, MongoDB data will be stored in the /data/db directory. Therefore, you can create directories /data/db1, /data/ DB2, and /data/db3 for these three instances.
$mkdir -p/data/db {1, 2, 3}Copy the code
Then, profiles for three instances can also be created in their data directories, let’s take one example
# /data/db1/mongod.conf
systemLog:
destination: file
path: /data/db1/mongod.log
logAppend: true
storage:
dbPath: /data/db1
net:
bindIp: 0.0. 0. 0
port: 28017
replication:
replSetName: rs0
processManagement:
fork: true
Copy the code
The above is the configuration file for the db1 instance, and the other two are similar. A few points to look out for here:
path: /data/db1/mongod.log
Configure a different path for each instance.dbPath: /data/db1
You configure the data directory for each instance, and you also need to configure a different path for each instance.port: 28017
This is the port number that the MongoDB service listens on. Each instance needs to listen on a different port number.replSetName: rs0
Is the name of the replica set, the same for each instance.
Start the mongo
With all three profiles created, you can start three MongoDB instances with three different configurations:
$ mongod -f /data/db1/mongo.conf
$ mongod -f /data/db2/mongo.conf
$ mongod -f /data/db3/mongo.conf
Copy the code
Here, THE three ports I use are 28017, 28018, and 28019, and I’ll use them for the rest of the operation.
Once started, the next step can begin.
Configuring a Replication Set
First link the first instance (DB1).
$ mongo --port 28017
Copy the code
Once you have entered the DB1 instance, there are two ways to configure the replication set with it as the master node.
The first:
> rs.initiate()
> rs.add("HOSTNAME:28018")
> rs.add("HOSTNAME:28019")
Copy the code
The second:
> rs.initiate({
_id: "rs0".members: [{_id: 0.host: "HOSTNAME:28017" },
{ _id: 1.host: "HOSTNAME:28018" },
{ _id: 2.host: "HOSTNAME:28019"}]})Copy the code
Either way is fine. Note that HOSTNAME refers to the HOSTNAME of your host, which can be viewed using the HOSTNAME command.
Let’s take the first method as an example:
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set"."me" : "ecs-b652-0001:28017"."ok" : 1
}
rs0:SECONDARY>
rs0:PRIMARY>
Copy the code
As you can see, after executing rs.Initiate (), some information is displayed, and then the > in front of the command line changes to rs0:SECONDARY>, and then to rs0:PRIMARY>.
Rs0 refers to the name of the replica set. When the replica set is first created, each node is a slave node by default, and since there is only one PRIMARY node in the current replica set, it is subsequently elected as the PRIMARY node.
Then add the other two nodes.
rs0:PRIMARY> rs.add("HOSTNAME:28018")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1626613607, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1626613607, 1)
}
Copy the code
Just add two base points in the same way, making sure the hostname and port numbers are correct.
test
So let’s test this.
Insert data into primary node first:
rs0:PRIMARY> db.test.insertOne({a:1})
{
"acknowledged" : true,
"insertedId" : ObjectId("60f428b929941628aa6be1b0")
}
rs0:PRIMARY> db.test.insertOne({a:2})
{
"acknowledged" : true,
"insertedId" : ObjectId("60f428c529941628aa6be1b1")
}
Copy the code
After joining a slave node, query the data just inserted:
rs0:SECONDARY> db.test.find()
Error: error: {
"topologyVersion" : {
"processId" : ObjectId("60f425f5994233d8ce517f71"),
"counter" : NumberLong(6)
},
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotPrimaryNoSecondaryOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1626614043, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1626614043, 1)
}
Copy the code
An error is reported because the slave node is not allowed to perform queries by default.
You can perform the following operations first:
rs0:SECONDARY> rs.secondaryOk()
Copy the code
Executing a query
rs0:SECONDARY> db.test.find()
{ "_id" : ObjectId("60f428b929941628aa6be1b0"), "a" : 1 }
{ "_id" : ObjectId("60f428c529941628aa6be1b1"), "a" : 2 }
Copy the code
You can query the data