[MongoDB] building a Mongo database replica set (similar to a cluster) on the windows platform (III), mongodbmongo
I have written two blogs about how to build a Mongo database replica set on windows.
Article 1: How to build a Mongo database replica set on windows
Article 2: Data Synchronization and fault adaptive testing
In this article, we will summarize the replica set and analyze its working principles.
I. Common scenarios
Lost network connection between applications and databases
Scheduled shutdown, power failure, Database Service hard disk failure, etc.
Replication allows failover. Replication allows you to balance read loads between replicas and ensure that the replication nodes are synchronized with the master node.
Ii. Working Principle
Replica sets depend on two basic mechanisms: oplog and heartbeat. oplog make data replication possible, while "heartbeat" monitors the health status and initiates failover;
2.1 about oplog
Oplog is the key to MongoDB replication. oplog is a fixed set located in the local database of each replication node. It records all changes to the database and writes data to the master node each time, it will automatically add a love record to the oplog of the master node, where the blog has enough information to reproduce the data. Once the write operation is copied to a slave node, the oplog of the slave Node also saves a record.
All replica set metadata and oplog are saved in the local database because they cannot be copied;
Then we will look at oplog in detail.
Note that each slave node has its own oplog, And the slave node uses the long polling method to immediately apply new entries from the master node oplog. If the cluster node cannot find the point to be synchronized in the oplog of the master node, the replication will be stopped permanently. This will cause the following exceptions in the log:
Replcation data too stale, halting
Caught syncException
Adjust the oplog size. You can use the db. getReplicationInfo () command to view the allocated oplog space and use the following command to change the default oplog size.
mongod.exe --replSet myapp --oplogSize 1024
2.2 heartbeat detection and Failover
The heartbeat detection of replica sets helps with election and failover. By default, each replica integrator can ping other members every 2 s. In this way, the system can find out its health status. Run rs. status () to view the health status.
NOTE: If both slave nodes are killed in the three nodes, the log on the master node contains the following statement:
ReplSet can't see a majority of the set,
ReplSet Secondary
This means that if there are no majority of nodes, the master node will downgrade itself to a slave node;
Iii. Management
Because the replica set has many potentially complex configuration items, we will introduce these complex configuration items in detail;
3.1 for configuration details, you can use the rs. initiate () and rs. add () Methods to initialize the replica set. Use config. members. push ({}) to add nodes;
Other methods:
3.2 failover and recovery are the process of restoring the replica set to the original state after the fault. There are two types of faults to be handled. The first type includes all non-destructive faults. Simply restart the service. The second is to clarify the fault, mainly because the data file is damaged. Otherwise, the mongodb service is shut down. If the host name and port number are not changed, re-execute the data folder and synchronize the data after the startup. If you modify attributes, you must use rs. reconfig ();
3.3 The deployment policy replica set contains a maximum of 12 nodes. The configuration of the minimum replica set that provides automatic failover is shown in the previous example. Contains two copies and one arbitration node. In the production environment, the arbitration server node can run on the application server, while the copy runs on its own machine. For most environments, this configuration is both economic and university-level.
How to Create a mongodb database with a user name and password
I'm so angry. Let's just put it down. Below is a part of my notes on mongodb.
By default, mongoDB user authentication is disabled.
Modify the/etc/cmdd. conf file. By default, any client in mongoDB can connect to port 27017 without authentication. By default, there is no administrator account. By modifying this configuration file, you can change it to permission authentication during login.
If you want to create a user for a database in mongoDB, you must first enter the database and then use the addUser command. You can also set the user to read-only (db. addUser ("jack", "jack", true), and the third parameter to indicate whether the user is a "read-only user ").
To use a super administrator, you must first connect to the admin database and log on to the administrator account, and then connect to other databases to exercise administrator privileges.
User information storage and authentication process
Similarly, MySQL saves system user information in the mysql. user table. MongoDB also saves the username and pwd of the system user in the admin. system. users collection. Pwd = md5 (username + ": mongo:" + real_password ). This is not a problem. Username And: mongo: It is equivalent to adding a salt value to the original password. Even if attackers obtain the md5 hash stored in the database, they cannot simply find the original password from the rainbow table.
Common permission management commands
1. # enter the database admin
Use admin
2. # Add or modify user passwords
Db. addUser ('name', 'pwd ')
3. # view the user list
Db. system. users. find ()
4. # User Authentication
Db. auth ('name', 'pwd ')
If this one returns 1, the authentication is successful. Only after the authentication is successful can the database be operated.
5. # delete a user
Db. removeUser ('name ')
6. # view all users
Show users
7. # view all databases
Show dbs
8. # view all collections
Show collections
9. # view the status of each collection
Db. printCollectionStats ()
10. # view master-slave replication status
Db. printReplicationInfo ()
A client tool used to connect to the mongodb database.
Jmongobroue, MonvoVUE, and windows command lines can also be used.
However, it is best to use the next enhanced cmd Tool, such as powercmd, because it cannot be displayed in the command line, and it is not necessarily displayed in the client tool. If you want to learn mongo, you 'd better learn to knock on commands instead of "click it"