Note that MongoDB's role is created and configured for later use
Brief introduction
Replica Set , Chinese translation is called a copy set, but I don't like translating English into Chinese, it always feels strange. In fact, in short, the cluster contains a number of data, to ensure that the main node hangs, the standby node can continue to provide data services, provided the premise is that the data needs and the primary node consistent
MongoDB (M) represents the primary node, MongoDB (S) represents the standby node, and MongoDB (A) represents the quorum node. The primary and standby node stores data, and the quorum node does not store data. The client connects the primary node to the standby node at the same time and does not connect the quorum node.
By default, the master node provides all the additions and deletions, and the standby node does not provide any services. However, you can provide a query service by setting up the standby node, which can reduce the pressure on the primary node, and when the client makes a data query, the request is automatically forwarded to the standby node. This setting is called the Read Preference Modes, and the Java client provides a simple way to configure it without having to manipulate the database directly.
The quorum node is a special node that does not store data by itself, and the main purpose is to decide which standby node is promoted to the primary node after the master node is hung, so the client does not need to connect to this node. Although there is only one standby node, a quorum node is still required to raise the node level. I do not believe that there must be a quorum node, but I have tried not to quorum node, the master node hangs the standby node or the standby node, so we still need it.
After the introduction of the cluster scheme, we now start building.
Replica set Setup
1. Prepare the configuration file
# mongod.conf# for documentation of all options, see:# http://docs.mongodb.org/manual/reference/ configuration-options/# where to write logging data.systemLog:destination:file logappend:true path:/home/dd/mongodb /logs/mongod.log #日志文件存放目录 # Where and how to store Data.storage:dbPath:/home/dd/mongodb/db #数据文件存放目录 journal:enabl ed:true# engine:# mmapv1:# wiredtiger:# How the process runsprocessManagement:fork:true #以守护程序的方式启用, which runs in the background pidfil Epath:/home/dd/mongodb/mongod.pid # Location of pidfile# network interfacesnet:port:27017 #端口 bindip:0.0.0.0 # Lis Ten to local interface only, comment to listen on all interfaces. #security: #authorization: Enabled #operationProfiling: Strong>replication:
Replsetname:matchdata1 #sharding: # enterprise-only Options#auditlog: #snmp:
Red indicates the name of the replica set, which is the name of the replica set to identify whether it belongs to the same cluster
2. Start a MongoDB database ready
Bin/mongod–f mongo.conf
3. Connect to MONGO Admin Library
Bin/mongo Ip:port/admin
4. Increase user
Use Admindb.createuser ({User: "root", pwd: "Root", roles:["root"}) and use Balldb.createuser ( { User: "Basket", pwd: "Basket", roles: [{role: "ReadWrite", DB: "Ballmatch"}] } )
5. Generate KeyFile File
Executing on the server
OpenSSL Rand –base64 753 > mongodb.keyfilechmod keyfile
Configure KeyFile to the MONGO configuration file while enabling authentication
Security: authorization:enabled Keyfile:/home/dd/mongodb/mongodb.keyfile
A machine without this file cannot be added to the replica set, the KeyFile is turned on, the Auth is opened implicitly, and the connection replica set needs to be authenticated.
6. Restart MONGO Service
7, copy to other servers, modify the entitlement directory
8. Add replica Set Configuration
cfg={_id: "MatchData1", members:[{_id:0, Host: ' 192.168.10.242:37017 ', priority:1},{_id:1, Host: ' 192.168.10.242:37018 ', priority:2}, {_id:2, Host: ' 192.168.10.242:37019 ', arbiteronly:true}]};rs.initiate (CFG);
CFG: variable name, any
_ID: Replica set name configured in "matchData1" configuration file
ip+ Port for Host:mongodb
Priority: Precedence, the higher the number priority (0-100), the highest priority will be the initial primary node, shown as primary. If you do not want some members to become primary during failover, set their priority to 0 (0 will never become the primary node)
When the replica set switchover fails, the following code can be reconfigured:
Rs.reconfig (CFG, {force:true});
9. Replica set status
Rs.status () "Health": 1, #代表机器正常 "statestr": "PRIMARY", #代表是主节点, can read and write, which has the following state 1. STARTUP: The configuration has not yet loaded 2, just joined to the replication set . STARTUP2: Configuration has been loaded, initialized; 3. RECOVERING: Recovering, not applicable read 4. Arbiter: Arbitrator 5. Down: The node cannot reach 6. UNKNOWN: No other node state is acquired, and it is not known what state, typically occurs in schema 7 with only two members. Removed: Remove replication set 8. ROLLBACK: Data rollback, at the end of rollback, is transferred to recovering or secondary state 9. FATAL: Error. View Log grep "Replset FATAL" to find the wrong reason and redo Sync 10. PRIMARY: Master node 11. Secondary: Backup node
10. Maintenance
The premise is that this ip:port must be a MongoDB instance that uses the same relpset name
Add a copy and enter it under Login to the master node
Deleting replicas
Rs.remove ("Ip:port");
New quorum node
Rs.addarb ("Ip:port");
Note: Nodes that are newly added to the replica set automatically synchronize the existing permissions and data for the current replica set
MongoDB 3.2.8 Replica Set Build