Replicattion set is that multiple servers maintain the same copy of the data, increasing the availability of the server.
Replication Set Setup whole process
0: Create a directory
Mkdir-p/DATA/R0/DATA/R1/DATA/R2
1: Start 3 instances and declare instances to be part of a replication set
./bin/mongod--port 27017--dbpath/data/r0--smallfiles--replset RSA--fork--logpath/var/log/mongo17.log
./bin/mongod--port 27018--dbpath/data/r1--smallfiles--replset RSA--fork--logpath/var/log/mongo18.log
./bin/mongod--port 27019--dbpath/data/r2--smallfiles--replset RSA--fork--logpath/var/log/mongo19.log
2: Start Mongo.exe, feel free to connect to a server, start the configuration
Use admin
var rsconf = {
_id: ' RSA ',
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 '
}
]
}
3: Initialize according to configuration
Rs.initiate (rsconf);
4: View status
Rs.status ();
We got three nodes "Statestr": "Secondary", "statestr": "Secondary", "Statestr": "PRIMARY",
5: Add node
Rs.add (' 192.168.1.201:27018 ');
Rs.add (' 192.168.1.201:27019 ');
6: Delete node
Rs.remove (' 192.168.1.201:27019 ');
7: Primary Node inserts data
>use Test
>db.user.insert ({uid:1,name: ' Lily '});
8: Connection Secondary Query synchronization situation
./bin/mongo--port 27019
>use Test
>show collections;
Rsa:secondary> Show Collections;
2015-11-10t21:56:14.075+0800 E QUERY error:listdatabases failed:{"note": "
From ExecCommand "," OK ": 0," errmsg ":" Not Master "}
At Error (<anonymous>)
At Mongo.getdbs (src/mongo/shell/mongo.js:47:15)
At Shellhelper.show (src/mongo/shell/utils.js:630:33)
At Shellhelper (src/mongo/shell/utils.js:524:36)
At (SHELLHELP2): 1:1 at src/mongo/shell/mongo.js:47
8.1 The above error occurs because slave is not allowed to read and write by default
>rs.slaveok ();
>show collections;
See data that is consistent with primary
MongoDB replication set replica set (Master-slave Replication) (8)