Build a copy of mongodb service, multiple ports on one server
One, create a replica set
1. Create a folder
C:\MongoDB\Server\bin> mkdir C:\MongoDB\mdb0
C:\MongoDB\Server\bin> mkdir C:\MongoDB\mdb1
C:\MongoDB\Server\bin> mkdir C:\MongoDB\mdb2
2. Close the server port and
open the windows PowerShell
as an administrator. The following are all open as an administrator . When the server is
not closed, it will start as the server. In the fourth step, an error occurred during configuration initialization and the prompt "This node was not started with the replSet option "
Restart from step 2
C:\Windows\system32> net stop mongodb
3. Run the mongodb service manually. All three service ports must be run.
Open a new Windows PowerShell and enter the installation directory. Run the service (three independent Shells)
C:\MongoDB\Server\bin> ./mongod --replSet mdb --port 27017 --dbpath C:\MongoDB\mdb0 --smallfiles --oplogSize 128
C:\MongoDB\Server\bin> ./mongod --replSet mdb --port 27018 --dbpath C:\MongoDB\mdb1 --smallfiles --oplogSize 128
C:\MongoDB\Server\bin> ./mongod --replSet mdb --port 27019 --dbpath C:\MongoDB\mdb2 --smallfiles --oplogSize 128
The server connection must be in the OK state, otherwise the process is terminated, (see the operation steps) , and the mongodb service is restarted
4. Configure
New Shell to enter mongo
C:\MongoDB\Server\bin> ./mongo --port 27017
rs.initiate() initializes the replica set object
MongoDB Enterprise > rs.initiate({_id:'mdb',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'}]})
result
{
"ok" : 1,
"operationTime" : Timestamp(1576656206, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1576656206, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Set the replica node to be readable, enter mongo 27018, 27019 and execute respectively to obtain information
db.getMongo().setSlaveOk()
View connection status
MongoDB Enterprise mdb:PRIMARY> rs.status()
View master copy
MongoDB Enterprise mdb:PRIMARY> db.isMaster()
Two, increase the replica set
a) Create a folder: C:\MongoDB\Server\bin> mkdir C:\MongoDB\mdb3
b) Create a port opening service: New windows Shell ./mongod --replSet mdb --port 27020 --dbpath C:\MongoDB \mdb3 --smallfiles --oplogSize 128
c) Increase the replica set: MongoDB Enterprise mdb:PRIMARY> rs.add('127.0.0.1:27021')
3. Remove the replica set:
a) Create a new windows Shell, enter the replica node, and disable the service
C:\MongoDB\Server\bin> ./mongo --port 27020
MongoDB Enterprise mdb:SECONDARY> use admin
MongoDB Enterprise mdb:SECONDARY> db.shutdownServer()
b) Remove the corresponding node
MongoDB Enterprise mdb:PRIMARY> rs.remove('127.0.0.1:27020') from the main server
Modify master-slave
a) Obtain config and modify priority to change the black recognition weight
conf=rs.config()
conf.members[0].priority=10
conf.members[1].priority=5
conf.members[2].priority=1
rs.reconfig(conf)