One installation 1.1 Download MongoDB
I personally do not like to use the wget URL, before the wget download package has a problem case
Https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.7.tgz
1.2 Upload server and unzip
TAR-ZXVF mongodb-linux-x86_64-3.4.7.tgz
1.3 Move to/data/program/software/mongodb
MV mongodb-linux-x86_64-3.4.7//data/program/software/mongodb
1.4 Go to the MongoDB directory
mkdir Dbmkdir Logs
1.5 Go to the bin directory
New configuration file: Touch mongodb.conf
dbpath=/data/program/software/mongodb/dblogpath=/data/program/software/mongodb/logs/mongodb.logport=27017fork= Truenohttpinterface=true
1.6 Start
Start on three machines, respectively.
/data/program/software/mongodb/bin/mongod--replset repset-f/data/program/software/mongodb/bin/mongodb.conf
1.7 To see if it started successfully
Ps-ef |grep MongoDB
1.8 Creating a clustered 1.8.1 login client
Select any one of the above servers to log on to the client
/data/program/software/mongodb/bin/mongo
1.8.2 switching to the admin database
Use admin
1.8.3 Building a Cluster
Define the replica set configuration variable, here _id: "Repset" and the above parameters--replset Repset consistent
config={_id: "Repset", Members:[{_id:0,host: "132.232.47.139"},{_id:1,host: "45.40.199.46"},{_id:2,host: " 132.232.40.31 "}]}
Enter the above variables and enter the following description no problem
Rs.initiate (config)
Execute the above statement if the output below shows no problem
1.8.5 View cluster status
Rs.status ()
{"Set": "Repset", "date": Isodate ("2018-08-05t12:20:02.096z"), "MyState": 1, "term": Numberlong (1), " Heartbeatintervalmillis ": Numberlong (+)," Optimes ": {" Lastcommittedoptime ": {" ts ": Timestamp (1533471594, 1)," T ": N Umberlong (1)}, "Appliedoptime": {"ts": Timestamp (1533471594, 1), "T": Numberlong (1)}, "Durableoptime": {"ts": Timestamp (1533471594, 1), "T": Numberlong (1)}, "members": [{"_id": 0, "name": "132.232.47.139:27017", "Health": 1, "state": 1, "St Atestr ":" PRIMARY "," uptime ": 4360," Optime ": {" ts ": Timestamp (1533471594, 1)," T ": Numberlong (1)}," Optimedate ": Isodat E ("2018-08-05t12:19:54z"), "InfoMessage": "Could not find member-sync from", "Electiontime": Timestamp (1533471513, 1), "Electiondate": Isodate ("2018-08-05t12:18:33z"), "ConfigVersion": 1, "Self": true},{"_id": 1, "name": " 45.40.199.46:27017 "," Health ": 1," state ": 2," statestr ":" Secondary "," uptime ": +," Optime ": {" ts ": Timestamp (15334715 94, 1), "T": Numberlong (1)}, "Optimedurable": {"ts": Timestamp (1533471594, 1), "T": Numberlong (1)}, "Optimedate": Isodate ("2018-08-05t12:19:54z"), "Optimedurabledate": Isodate (" 2018-08-05t12:19:54z ")," Lastheartbeat ": Isodate (" 2018-08-05t12:20:01.211z ")," Lastheartbeatrecv ": ISODate (" 2018-08-05t12:20:01.767z ")," Pingms ": Numberlong (0)," syncingto ":" 132.232.47.139:27017 "," configversion ": 1},{" _id " : 2, ' name ': ' 132.232.40.31:27017 ', ' health ': 1, ' state ': 2, ' statestr ': ' Secondary ', ' uptime ': ', ' optime ': {' ts ': Tim Estamp (1533471594, 1), "T": Numberlong (1)}, "Optimedurable": {"ts": Timestamp (1533471594, 1), "T": Numberlong (1)}, " Optimedate ": Isodate (" 2018-08-05t12:19:54z ")," Optimedurabledate ": Isodate (" 2018-08-05t12:19:54z ")," lastHeartbeat ": Isodate (" 2018-08-05t12:20:01.211z ")," Lastheartbeatrecv ": Isodate (" 2018-08-05t12:20:01.769z ")," PingMs ": Numberlong (0), "syncingto": "132.232.47.139:27017", "ConfigVersion": 1}], "OK": 1}
1.8.6 test cluster feature a master node server login Client
./mongo
B Connect the test database
Use test
c inserting data into the TestDB table
Db.testdb.insert ({"Test1": "Testval1"})
D connect to the replica node client
./mongo 45.40.199.46:27017
e using the test database
Use test
F Inquiry Form
Show tables
If the following error occurs
MongoDB defaults to read and write data from the master node, read not allowed on replica nodes, set replica node readable
G Set Read permissions
Db.getmongo (). Setslaveok ()
H Query the data inserted on the master node
Db.testdb.find ()
I test the cluster recovery feature 1 Stop the primary node service after logging on to other node clients to view the cluster status
{"Set": "Repset", "date": Isodate ("2018-08-05t13:15:05.136z"), "MyState": 1, "term": Numberlong (2), " Heartbeatintervalmillis ": Numberlong (+)," Optimes ": {" Lastcommittedoptime ": {" ts ": Timestamp (1533474896, 1)," T ": N Umberlong (2)}, "Appliedoptime": {"ts": Timestamp (1533474896, 1), "T": Numberlong (2)}, "Durableoptime": {"ts": Timestamp (1533474896, 1), "T": Numberlong (2)}, "members": [{"_id": 0, "name": "132.232.47.139:27017", "Health": 0, "state": 8, "St Atestr ":" (not Reachable/healthy) "," uptime ": 0," Optime ": {" ts ": Timestamp (0, 0)," T ": Numberlong ( -1)}," Optimedurable " : {"TS": Timestamp (0, 0), "T": Numberlong ( -1)}, "Optimedate": Isodate ("1970-01-01t00:00:00z"), "optimedurabledate": Isodate ("1970-01-01t00:00:00z"), "Lastheartbeat": Isodate ("2018-08-05t13:15:03.341z"), "LASTHEARTBEATRECV": Isodate ("2018-08-05t13:12:24.585z"), "Pingms": Numberlong (0), "lastheartbeatmessage": "Connection refused", " ConfigVersion ": -1},{" _id ": 1," name ":" 45.40.199.46:27017 "," Health ": 1," statE ": 1," Statestr ":" PRIMARY "," uptime ": 7653," Optime ": {" ts ": Timestamp (1533474896, 1)," T ": Numberlong (2)}," Optimedate ": Isodate (" 2018-08-05t13:14:56z ")," Electiontime ": Timestamp (1533474755, 1)," Electiondate ": Isodate (" 2018-08-05t13 : 12:35z ")," ConfigVersion ": 1," Self ": true},{" _id ": 2," name ":" 132.232.40.31:27017 "," Health ": 1," state ": 2," Statestr " : "Secondary", "uptime": 3401, "Optime": {"ts": Timestamp (1533474896, 1), "T": Numberlong (2)}, "Optimedurable": {"TS": Timestamp (1533474896, 1), "T": Numberlong (2)}, "Optimedate": Isodate ("2018-08-05t13:14:56z"), "optimedurabledate": Isodate ("2018-08-05t13:14:56z"), "Lastheartbeat": Isodate ("2018-08-05t13:15:03.282z"), "LASTHEARTBEATRECV": Isodate ("2018-08-05t13:15:03.165z"), "Pingms": Numberlong (0), "syncingto": "45.40.199.46:27017", "ConfigVersion": 1} ], "OK": 1}
Conclusion: _id=1 's service now becomes the master node, and _id=0 's service is now unavailable.
2 Once again the original 132.232.47.139 this machine to start up
{"Set": "Repset", "date": Isodate ("2018-08-05t13:19:08.073z"), "MyState": 1, "term": Numberlong (2), " Heartbeatintervalmillis ": Numberlong (+)," Optimes ": {" Lastcommittedoptime ": {" ts ": Timestamp (1533475146, 1)," T ": N Umberlong (2)}, "Appliedoptime": {"ts": Timestamp (1533475146, 1), "T": Numberlong (2)}, "Durableoptime": {"ts": Timestamp (1533475146, 1), "T": Numberlong (2)}, "members": [{"_id": 0, "name": "132.232.47.139:27017", "Health": 1, "state": 2, "St Atestr ":" Secondary "," uptime ": 4," Optime ": {" ts ": Timestamp (1533474744, 1)," T ": Numberlong (1)}," Optimedurable ": {" TS ": Timestamp (1533474744, 1)," T ": Numberlong (1)}," Optimedate ": Isodate (" 2018-08-05t13:12:24z ")," optimedurabledate ": Isodate ("2018-08-05t13:12:24z"), "Lastheartbeat": Isodate ("2018-08-05t13:19:07.545z"), "LASTHEARTBEATRECV": Isodate ("2018-08-05t13:19:03.326z"), "Pingms": Numberlong (0), "configversion": 1},{"_id": 1, "name": " 45.40.199.46:27017 "," Health ": 1," state ": 1," Statestr ":" PRIMARY "," uptime ": 7896, "Optime": {"ts": Timestamp (1533475146, 1), "T": Numberlong (2)}, "Optimedate": Isodate ("2018-08-05t13:19:06z"), " Electiontime ": Timestamp (1533474755, 1)," Electiondate ": Isodate (" 2018-08-05t13:12:35z ")," ConfigVersion ": 1," Self ": true},{"_id": 2, "name": "132.232.40.31:27017", "Health": 1, "state": 2, "statestr": "Secondary", "uptime": 3644, "Optime" : {"TS": Timestamp (1533475146, 1), "T": Numberlong (2)}, "Optimedurable": {"ts": Timestamp (1533475146, 1), "T": Numberlo Ng (2)}, "Optimedate": Isodate ("2018-08-05t13:19:06z"), "Optimedurabledate": Isodate ("2018-08-05t13:19:06z"), " Lastheartbeat ": Isodate (" 2018-08-05t13:19:07.360z ")," Lastheartbeatrecv ": Isodate (" 2018-08-05t13:19:07.255z ")," Pingms ": Numberlong (0)," syncingto ":" 45.40.199.46:27017 "," ConfigVersion ": 1}]," OK ": 1}
Conclusion: The service of _id=1 is still the primary node, and the service of _id=0 becomes the replica node
Architecture (iii) MONGODB installation configuration and cluster construction