MongoDB copies (copy sets) study notes

  • 2020-12-13 19:11:04
  • OfStack

This article gives an example of MongoDB replication (copy set). To share for your reference, the details are as follows:

replication set replication set,
Replication sets, multiple servers maintain the same copy of data, improving server availability.
MongoDB replication is the process of synchronizing data across multiple servers.
Replication provides redundant backups of data and stores copies of the data on multiple servers, improving data availability and ensuring data security.
Replication also allows you to recover data from hardware failures and service outages.

Setup process:

(1) Create an example

Suppose 3 sets are created, 3 instance directories and log directories are created:
mkdir /home/m17 /home/m18 /home/m19 /home/mlog
Start 3 examples, ports 27017, 27018, and 27019.


./mongod --dbpath=/home/m17 --logpath=/home/mlog/m17.log --fork --port=27017 --replSet=rs2 --smallfiles
./mongod --dbpath=/home/m18 --logpath=/home/mlog/m18.log --fork --port=27018 --replSet=rs2 --smallfiles
./mongod --dbpath=/home/m19 --logpath=/home/mlog/m19.log --fork --port=27019 --replSet=rs2 --smallfiles

Description:

Parameter --replSet is set to 1 to belong to the same copy set
Parameter -smallfiles saves space and increases speed.

You can then use ps aux | grep mongo to see the three ports that are booted up.

(2) Configuration

Configure with client connection mongo:


[test@localhost bin]$ ./mongo

To manage configuration, switch to admin:


>use admin

(Configuration is in json format)


var rsconf = {
_id:'rs2',
members:[
{"_id":0,host:'192.168.8.172:27017'},
{_id:1,host:'192.168.8.172:27018'},
{_id:2,host:'192.168.8.172:27019'}
]
}

If ip is not configured, use 127.0.0.1


var rsconf = {
_id:'rs2',
members:[
{_id:0,host:'127.0.0.1:27017'},
{_id:1,host:'127.0.0.1:27018'},
{_id:2,host:'127.0.0.1:27019'}
]
}

Once executed, use printjson(rsconf) to view the configuration just now.

Then perform initialization:


>rs.initiate(rsconf);
> rs.initiate(rsconf);
{
"ok" : 1,
"operationTime" : Timestamp(1539933041, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1539933041, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
rs2:SECONDARY>

View node:


rs.status()

Delete node:


rs.remove('127.0.0.1:27019')

Add nodes:


rs.add('127.0.0.1:27019')

Switching node:

The default is on port 27017, rs2:PRIMARY > State,
Exit mongo client command mode,
Switch to another port:


[test@localhost bin]$ ./mongo --port=27018 . 

Switch to rs2:SECONDARY > State.

Testing:

On the main service, create libraries and collections,


[test@localhost bin]$ ./mongo

0

Go look at the service

show dbs

Then I see an error. The specific error message is:

...
"errmsg" : "not master and slaveOk=false",
...

Because slave does not allow read and write by default:


[test@localhost bin]$ ./mongo

1

You can then see the libraries and collections created by the primary server.

Similarly, 27019 also needs to execute this command in order to automatically synchronize and read.

When the master server is down on 27017,
The second 27018 automatically becomes the primary server master state.
But 27019 needs to execute ES116en.slaveOk () again to synchronize the reads and writes automatically.

shell script:


[test@localhost bin]$ ./mongo

2

I hope that this article has been helpful to the MongoDB database programming.


Related articles: