How to Read Data From a Slave Mongo Db
1. Mongodb master-slave replication
Mongodb replication is the process of synchronizing data on multiple servers.
Replication provides redundant backup of data and stores data copies on multiple servers, which improves information availability and ensures information security.
Replication also allows you to recover data from hardware failures and service outages.
Official document https://docs.mongodb.com/manu
ane.1 what is replication?
Secure data
High information availability (24 * 7)
disaster recovery
No reanimation for maintenance (eastward.g. backup, index rebuild, pinch)
Distributed read information
1.2 mongodb replication principle
Mongodb requires at least two nodes for replication. One of them is the master node, which is responsible for processing customer requests, and the other is the slave node, which is responsible for copying the data on the master node.
The common collocations of mongodb nodes are: i master and i slave, one master and many slaves.
All operations recorded past the master node are oplog. The slave node polls the master node regularly to obtain these operations, then performs these operations on its own data copy, and then as to ensure that the information of the slave node is consistent with that of the master node.
The mongodb replication structure is as follows:
In the above construction chart, the customer reads data from the master node. When the customer writes data to the master node, the master node and the slave node interact to ensure information consistency.
i.iii replica gear up features
Cluster of N nodes
Whatever node can be the primary node
All writes are on the principal node
Automatic failover
Automated recovery
1.four mongodb replica gear up settings
ane. Shut down the running mongodb server.
service mongod stop two. Node construction
First of all, y'all need to become to the folder where the mongodb data files are stored and create three databases to simulate three unlike machines. The path of the blogger is equally follows
mkdir -p /data/db/node1 mkdir -p /data/db/node2 mkdir -p /data/db/node3 3. Start iii databases (dbpaths), and port (– port 1000x), cluster name (– replset Gabriel), turn off log selection (– nojournal), start in the mode of daemons, and automatically pull up (– fork), log directory (– logpath)
mongod --dbpath /data/db/node1 --port 10001 --replSet gabriel --nojournal --fork --logpath /information/db/node1.log mongod --dbpath /data/db/node2 --port 10002 --replSet gabriel --nojournal --fork --logpath /data/db/node2.log mongod --dbpath /information/db/node3 --port 10003 --replSet gabriel --nojournal --fork --logpath /data/db/node3.log iv. Past the way, connect a server for initialization. Here, the blogger connects to port 10001
Access under terminal
mongo localhost:10001 Enter initialization method later on entering
MongoDB Enterprise gabriel:OTHER> rs.initiate({_id:"gabriel",members:[ {_id:one,host:"localhost:10001"}, {_id:2,host:"localhost:10002"}, {_id:three,host:"localhost:10003"}, ]}) The following message was received successfully.
{ "ok" : 1, "operationTime" : Timestamp(1517221411, i), "$clusterTime" : { "clusterTime" : Timestamp(1517221411, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } MongoDB Enterprise gabriel:OTHER> You volition detect that the output on the final has changed.
//From a single > // changed. gabriel:OTHER> five. Query status
MongoDB Enterprise gabriel:OTHER> rs.condition() { "set" : "gabriel", "date" : ISODate("2018-01-29T10:33:21.227Z"), "myState" : i, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1517221984, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1517221984, 1), "t" : NumberLong(one) }, "appliedOpTime" : { "ts" : Timestamp(1517221994, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1517221994, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 1, "proper noun" : "localhost:10001", "health" : one, "land" : one, "stateStr" : "PRIMARY", "uptime" : 659, "optime" : { "ts" : Timestamp(1517221994, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-01-29T10:33:14Z"), "electionTime" : Timestamp(1517221422, 1), "electionDate" : ISODate("2018-01-29T10:23:42Z"), "configVersion" : one, "self" : truthful }, { "_id" : 2, "proper noun" : "localhost:10002", "health" : 1, "country" : 2, "stateStr" : "SECONDARY", "uptime" : 589, "optime" : { "ts" : Timestamp(1517221994, one), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1517221984, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-01-29T10:33:14Z"), "optimeDurableDate" : ISODate("2018-01-29T10:33:04Z"), "lastHeartbeat" : ISODate("2018-01-29T10:33:xx.972Z"), "lastHeartbeatRecv" : ISODate("2018-01-29T10:33:19.923Z"), "pingMs" : NumberLong(0), "syncingTo" : "localhost:10001", "configVersion" : 1 }, { "_id" : 3, "name" : "localhost:10003", "wellness" : 1, "country" : 2, "stateStr" : "SECONDARY", "uptime" : 589, "optime" : { "ts" : Timestamp(1517221994, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1517221984, i), "t" : NumberLong(one) }, "optimeDate" : ISODate("2018-01-29T10:33:14Z"), "optimeDurableDate" : ISODate("2018-01-29T10:33:04Z"), "lastHeartbeat" : ISODate("2018-01-29T10:33:xx.972Z"), "lastHeartbeatRecv" : ISODate("2018-01-29T10:33:19.921Z"), "pingMs" : NumberLong(0), "syncingTo" : "localhost:10001", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1517221994, 1), "$clusterTime" : { "clusterTime" : Timestamp(1517221994, ane), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } In the return, the parameter prepare is followed by the cluster proper noun. Under each member, you can see their respective situations, where statestr is the function and the primary node is (primary).
half-dozen. Enter the master node to insert data and enter the slave node to view data
Interface of blogger primary node in 10001
mongo localhost:10001 insert data
MongoDB Enterprise gabriel:PRIMARY> use exam switched to db test DB. Col. insert ({Title: 'mongodb tutorial', Description: 'mongodb is a NoSQL database', Past: 'Xiaoluo technology notes - focus on inquiry and cognition sharing of evolution technology', url: 'http://www.yuwowugua.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) MongoDB Enterprise gabriel:PRIMARY> db.col.find() {"u id": objectid ("5a6ef998525d903d07a00cdf"), "title": "mongodb tutorial", "clarification": "mongodb is a NoSQL database", "by": "Xiao Luo'due south technical notes - inquiry and knowledge sharing focusing on evolution technology", "URL": "http://www.yuwowugua.com", "tags": ["mongodb", "database", "NoSQL"], "likes": 100} MongoDB Enterprise gabriel:PRIMARY> Blogger switch slave node 10002
mongo localhost:10002 When you switch to the slave node, you volition find that using show DBS will result in an error. Because you lot have not nonetheless opened the permission, enter rs.slaveok(); and you lot can access it successfully.
MongoDB Enterprise gabriel:SECONDARY> show dbs 2018-01-29T10:40:37.362+0000 Eastward QUERY [thread1] Fault: listDatabases failed:{ "operationTime" : Timestamp(1517222434, 1), "ok" : 0, "errmsg" : "non master and slaveOk=faux", "lawmaking" : 13435, "codeName" : "NotMasterNoSlaveOk", "$clusterTime" : { "clusterTime" : Timestamp(1517222434, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } : [email protected]/mongo/shell/utils.js:25:13 [email protected]/mongo/shell/mongo.js:65:1 [email protected]/mongo/crush/utils.js:813:19 [email protected]/mongo/vanquish/utils.js:703:15 @(shellhelp2):1:one MongoDB Enterprise gabriel:SECONDARY> MongoDB Enterprise gabriel:SECONDARY> rs.slaveOk() Look again
MongoDB Enterprise gabriel:SECONDARY> show dbs admin 0.000GB config 0.000GB local 0.000GB examination 0.000GB MongoDB Enterprise gabriel:SECONDARY> Switch to the test database and bank check that the data has been synchronized
MongoDB Enterprise gabriel:SECONDARY> use test switched to db examination MongoDB Enterprise gabriel:SECONDARY> db.col.find() {"u id": objectid ("5a6ef998525d903d07a00cdf"), "championship": "mongodb tutorial", "clarification": "mongodb is a NoSQL database", "by": "Xiaoluo engineering science notes - research and knowledge sharing focusing on development technology", "URL": "http://www.yowowugua.com", "tags": ["mongodb", "database", "NoSQL"], "likes": 100} MongoDB Enterprise gabriel:SECONDARY> The above is a simple principal-slave replication establishment procedure. At present you can run into the data inserted by the master server in the slave server.
The same problem of switching slave node 10003
Delete from node
rs.remove('ip:port') After shutting down the master server and restarting it, yous will detect that the original slave server is inverse into a slave server, and the newly started server (the original slave server) is changed into a slave server
2. Mongodb automatic failover
Kickoff, through rs.status(), you tin run into that the primary node is 10001, and the primary node "proper noun":
"Localhost: 10001", "statestr": "primary" adjacent stop the 10001 primary node and test the failover
MongoDB Enterprise gabriel:PRIMARY> rs.status() { "prepare" : "gabriel", "date" : ISODate("2018-01-30T02:39:58.468Z"), "myState" : 1, "term" : NumberLong(i), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1517279986, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1517279986, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1517279996, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1517279996, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 1, "proper name" : "localhost:10001", "health" : 1, "state" : one, "stateStr" : "PRIMARY", "uptime" : 58656, "optime" : { "ts" : Timestamp(1517279996, one), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-01-30T02:39:56Z"), "electionTime" : Timestamp(1517221422, one), "electionDate" : ISODate("2018-01-29T10:23:42Z"), "configVersion" : i, "cocky" : true }, { "_id" : 2, "name" : "localhost:10002", "wellness" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 58586, "optime" : { "ts" : Timestamp(1517279996, one), "t" : NumberLong(i) }, "optimeDurable" : { "ts" : Timestamp(1517279986, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-01-30T02:39:56Z"), "optimeDurableDate" : ISODate("2018-01-30T02:39:46Z"), "lastHeartbeat" : ISODate("2018-01-30T02:39:58.289Z"), "lastHeartbeatRecv" : ISODate("2018-01-30T02:39:57.220Z"), "pingMs" : NumberLong(0), "syncingTo" : "localhost:10001", "configVersion" : 1 }, { "_id" : 3, "name" : "localhost:10003", "health" : 0, "state" : 8, "stateStr" : "(not reachable/good for you)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-one) }, "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-01-30T02:39:58.304Z"), "lastHeartbeatRecv" : ISODate("2018-01-30T02:39:21.208Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Connection refused", "configVersion" : -one } ], "ok" : i, "operationTime" : Timestamp(1517279996, 1), "$clusterTime" : { "clusterTime" : Timestamp(1517279996, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } MongoDB Enterprise gabriel:PRIMARY> Connect to master
mongo localhost:10001 Prove all databases
MongoDB Enterprise gabriel:PRIMARY> show dbs admin 0.000GB config 0.000GB local 0.000GB exam 0.000GB Switch to admin
MongoDB Enterprise gabriel:PRIMARY> use admin switched to db admin To stop the database, you must enter the admin Library
MongoDB Enterprise gabriel:PRIMARY> db.shutdownServer() response
2018-01-30T02:51:34.503+0000 I NETWORK [thread1] trying reconnect to localhost:10001 (127.0.0.1) failed 2018-01-30T02:51:35.398+0000 I NETWORK [thread1] Socket recv() Connexion reset by peer 127.0.0.1:10001 2018-01-30T02:51:35.398+0000 I NETWORK [thread1] SocketException: remote: (NONE):0 error: SocketException socket exception [RECV_ERROR] server [127.0.0.1:10001] 2018-01-30T02:51:35.399+0000 I NETWORK [thread1] reconnect localhost:10001 (127.0.0.1) failed failed 2018-01-30T02:51:35.404+0000 I NETWORK [thread1] trying reconnect to localhost:10001 (127.0.0.i) failed 2018-01-30T02:51:35.404+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:10001, in(checking socket for error after poll), reason: Connection refused 2018-01-30T02:51:35.404+0000 I NETWORK [thread1] reconnect localhost:10001 (127.0.0.1) failed failed MongoDB Enterprise > Check whether information technology really stops, and find that there is no 10001 node process
[e-mail protected]:# ps -ef | grep mongo root 5554 1 0 Jan29 ? 00:03:34 mongod --dbpath /data/db/node2 --port 10002 --replSet gabriel --nojournal --fork --logpath /information/db/node2.log root 12284 i 0 02:43 ? 00:00:02 mongod --dbpath /data/db/node3 --port 10003 --replSet gabriel --nojournal --fork --logpath /data/db/node3.log root 12436 5132 0 02:53 pts/1 00:00:00 grep --color=machine mongo [email protected]:# netstat -nltp Active Net connections (only servers) Proto Recv-Q Send-Q Local Accost Foreign Address State PID/Plan name tcp 0 0 127.0.0.1:10002 0.0.0.0:* Mind 5554/mongod tcp 0 0 127.0.0.one:10003 0.0.0.0:* Listen 12284/mongod [e-mail protected]:/data/db# Bank check whether the mistake specialty
[e-mail protected]:# mongo localhost:10001 View master-slave status
MongoDB Enterprise gabriel:SECONDARY> rs.status() { "set" : "gabriel", "engagement" : ISODate("2018-01-30T02:56:48.074Z"), "myState" : 2, "term" : NumberLong(2), "syncingTo" : "localhost:10003", "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1517280995, one), "t" : NumberLong(2) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1517280995, one), "t" : NumberLong(2) }, "appliedOpTime" : { "ts" : Timestamp(1517281005, one), "t" : NumberLong(2) }, "durableOpTime" : { "ts" : Timestamp(1517280995, 1), "t" : NumberLong(2) } }, "members" : [ { "_id" : 1, "name" : "localhost:10001", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-ane) }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2018-01-30T02:56:47.605Z"), "lastHeartbeatRecv" : ISODate("2018-01-30T02:51:34.519Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Connection refused", "configVersion" : -i }, { "_id" : 2, "proper name" : "localhost:10002", "health" : 1, "state" : ii, "stateStr" : "SECONDARY", "uptime" : 59660, "optime" : { "ts" : Timestamp(1517281005, i), "t" : NumberLong(two) }, "optimeDate" : ISODate("2018-01-30T02:56:45Z"), "syncingTo" : "localhost:10003", "configVersion" : 1, "self" : true }, { "_id" : 3, "name" : "localhost:10003", "health" : 1, "state" : 1, "stateStr" : "Master", "uptime" : 784, "optime" : { "ts" : Timestamp(1517281005, i), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1517281005, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2018-01-30T02:56:45Z"), "optimeDurableDate" : ISODate("2018-01-30T02:56:45Z"), "lastHeartbeat" : ISODate("2018-01-30T02:56:46.486Z"), "lastHeartbeatRecv" : ISODate("2018-01-30T02:56:47.147Z"), "pingMs" : NumberLong(0), "electionTime" : Timestamp(1517280703, 1), "electionDate" : ISODate("2018-01-30T02:51:43Z"), "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1517281005, 1), "$clusterTime" : { "clusterTime" : Timestamp(1517281005, ane), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } MongoDB Enterprise gabriel:SECONDARY> Found "proper noun": "localhost: 10001", "statestr": "(non reachable / Health)", the wellness status is already "unreachable"
The main node has been switched to 10003 node
"_id" : 3, "proper noun" : "localhost:10003", "health" : 1, "state" : 1, "stateStr" : "Principal", Restart node 10001
mongod --dbpath /data/db/node1 --port 10001 --replSet gabriel --nojournal --fork --logpath /information/db/node1.log
Source: https://developpaper.com/mongodb-master-slave-replication-and-automatic-failover/
Postar um comentário for "How to Read Data From a Slave Mongo Db"