Use Mongoose Connection in MongoDB replica set and Nodejs replica

Source: Internet
Author: User


First. MONGODB Replication INTRODUCTION


The first sentence on the official website is replication is the process of synchronizing data across multiple Servers. Translation is replication (replication) is the process of synchronizing across multiple servers, the rationale is that a primary server and many from the server through the synchronization of logs to achieve the purpose of data consistency, and there is only one primary server, in MongoDB is also called the Master node (primary node) is responsible for the write operation, and from the server, also called the secondary node (secondary nodes) just copy the primary server called Oplog log to synchronize with the master server's data, if the primary server is unfortunate, then the MONGO engine will automatically trigger an election, Select a new primary server (or more than half of the votes), if the election is not completed, the same number of votes, the entire cluster will become read-only status cannot be written. MONGO also has a role called the Arbiter (arbiter), which does not store and synchronize data, just as a maintenance authority or heartbeat data exists to avoid too few nodes can not complete the election, it also helps the master and slave server to determine the state, which can be used in some poor performance of the machine or virtual machine to play.



As a front-end above is the best explanation I can make. The following directly open, simple steps a few lines of code to get everyone to build a MONGO replica





Second. MongoDB replica set


1. System Introduction



A.mac OSX 10.10



B.mongodb 3.2.0






2. Node Build point



First, you need to go to the folder of your choice of MongoDB data files to create a new three database to simulate three non-pass machines, Bo Master's path is as follows






Cd/data/dbmkdir  nodeone nodetwo nodethree


3. Start three databases (DBPath), and Port (--port 1000x), cluster name (--replset Gabriel), turn off log option (--nojournal), daemon mode start, will automatically pull up (--fork), Log directory (--logpath).




mongod --dbpath /data/db/node1 --port 10001 --replSet gabriel --nojournal --fork --logpath /data/db/node1.log
mongod --dbpath /data/db/node2 --port 10002 --replSet gabriel --nojournal --fork --logpath /data/db/node2.log
mongod --dbpath /data/db/node3 --port 10003 --replSet gabriel --nojournal --fork --logpath /data/db/node3.log


4. Connect a server, do the initialization operation, here Bo Master connected to 10001 Port



//Enter under the terminal
Mongo localhost:10001
/ / Enter the initialization method after entering
Rs.initiate({_id:"gabriel",members:[
{_id:1,host:"localhost:10001"},
{_id:2,host:"localhost:10002"},
{_id:3,host:"localhost:10003"},
]})


The following information was successfully received.




{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}


You will notice that the output on the terminal has changed.



From a single >//into a gabriel:other>.


5. Query Status



Rs.status ()


In return, the parameter set is followed by the cluster name, where each of the members can see their respective situation, where statestr is the role and the primary node is (PRIMARY).



6. Insert data into the master node to view data from the node



//The master node of the blog is on the 10001 interface.
Mongo localhost:10001
/ / Switch the database, insert a piece of data, enter in order
  Use gabdb
Db.user.insert({dataid:10001})
Db.user.find()


Switch to the From node, you will find that using show DBS will error, because you have not opened the permission, enter Rs.slaveok (), can be successfully accessed.



/ / Switch from the node
Mongo localhost:10002

/ / No permission query
Show dbs
/ / error
2016-01-06T14:48:53.155+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :

//Open
Gabriel:SECONDARY> rs.slaveOk()
Gabriel:SECONDARY> show dbs


The above is a simple setup process, and now you can see the data inserted by the master server from the server.








Third. Connect the replica database with Mongoose in Nodejs


1. Introduction to the release



A.nodejs 0.10.25



B.mongoose 3.8.8






2. Configure the following



Var mongoose = require(‘mongoose‘),
//mongoose configuration
Var opts = {
   Db: { native_parser: true },
   Server: {
     poolSize: 5 ,
      Auto_reconnect: true,
      socketOptions: {keepAlive: 1}
   },
   Replset: { rs_name: ‘gabriel’ }
}
//mongoose connection
App.db = mongoose.connect(config.mongodb.repUri, opts);
App.db.on(‘error‘, console.error.bind(console, ‘mongoose connection error: ‘));
App.db.once(‘open‘, function () {
     //and... we have a data store
});
//config.mongodb.repUri is as follows
Exports.mongodb = {
 
   repUri: "mongodb://localhost:10001,localhost:10002,localhost:10003/gabdb"
};


So the process as described above, there are questions can be left message to tell, feel good point recommended ~



Use Mongoose Connection in MongoDB replica set and Nodejs replica


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.