Replica Set is a replica set of terms, definitions database cluster having a plurality of nodes, the nodes having a main achieve automatic failover from the copy (master-slave replication) and between the nodes.
Such a structure typically requires member having an odd number of members (with or without an Arbiter node), selected to ensure proper PRIMARY (primary) databases.
Selected DB will handle all incoming writes, and in its oplog, each auxiliary (slave) replica members can access and copy storage oplog information about them to apply to their data sets.
Front
-
To create a Replica set, at least three MongoDB instance, check out the official website Installation Guide.
-
This article will always use the sudo command, in general, please give MongoDB service to create a standard user mongod.
Network Configuration
Data consistency is reached, each instance of the cluster node needs to communicate with other nodes, an example data transmission with three examples:
① replica set each member to use private IP, deployed in the same data center, which is the recommended way.
② replica set for each node using the public IP network, nodes are deployed in different data centers (when there is a network delay Replication), generally used in this way strong deployment disaster recovery, if this way, the host needs to be configured between SSL / TLS communication or via vpn
replicas set node certification
In this section you will use openssl to create a key file for authentication between cluster members, MongoDB recommend using x.509 certificates encrypted connection.
① produce key documents
openssl rand -base64 756 > mongo-keyfile
The generated key files are copied to each member of the replication set
② ensure replication set members can access the key files in the same path:
sudo mkdir /opt/mongo
sudo mv ~/mongo-keyfile /opt/mongo
sudo chmod 400 /opt/mongo/mongo-keyfile
③ default MongoDB installed using a standard account mongod
, make sure the file has ownership mongod
sudo chown mongod:mongod /opt/mongo/mongo-keyfile
Creating the Admin User
Log in MongoDB are you going to set the Primary node, enter admin database, create an administrator user with root privileges
use admin
db.createUser({user: "mongo-admin", pwd: "password", roles:[{role: "root", db: "admin"}]})
Configure MongoDB
Modify mongod.conf replication set for each member:
net:
port: 27017
bindIp: 127.0.0.1,192.0.2.1
security:
keyFile: /opt/mongo/mongo-keyfile
replication: replSetName: rs0
Specify the key file, replication set name;
Restart Service
sudo systemctl restart mongod
Start the cluster, add nodes
Using the administrator account created before landing Primary MongoDB service node:
mongo mongo -u admin -p---a to the Antioch of us have tabase admin
① initialize add a cluster node
rs.initiate()
rs.add("mongo-repl-2")
rs.add("mongo-repl-3")
Instead of using the above node hostsname ip address mapping entry hosts you need to add a node in the node / etc / hosts.
② use rs.conf () or rs.Status () cluster configuration and status verification
Buy and sell Replica Set
Completing the above steps, MongoDB Replica Set three examples have been set up better.
Log in to do some routine Primary node and shift (by the way Jianlou some of your unexpected gesture)
① input test data
use exampleDB
for (var i = 0; i <= 10; i++) db.exampleCollection.insert( { x : i } )
ExampleDB will implicitly create and document collections exampleCollection.
Please note, Collection created by default is uncapped .
Cap capped collection is a collection of fixed size to support high throughput operation, these operations to insert and retrieve the insertion order according to the document, the work in the form of a circular buffer (once reached the set of allocated space, will come through the new maneuvers override old documentation Space).
② observe the Secondary node to synchronize data
Use an administrator account to create a landing Secondary node, direct inquiries will be reported:
Because the default Replica set to read and write were established occurred in Primary nodes (a node is the role of Secondary: redundancy, failover) ;
But MongoDB replica set supports read preference settings on the client (read preferences), most of the support specified read preference Driver reads the connection string in the preferences, this setting can achieve master-slave reader a real sense of separation.
Corresponds to a shell session, we need to open a session based views Secondary readable
db.getMongo().setSlaveOk()
or
Reading preferences using shell commands
Come back to
The practical operation of this article, you can have a complete set up MongoDB Replica Set, and generally mastered most of the characteristics of the replica set: master reads and writes, the secondary node redundant backup; support for open read on the secondary node.
Later we chat replica set failover, the replica set keep-alive heartbeat, posture-related asynchronous replication.