6. Architecture management mongodb master-Slave clusters can be divided into two types: 1: master-Slave replication (master-Slave) -- The Slave server will not take the initiative to become the master server, you need to set up 2: replicaSets replication (replica set) -- if the master server fails, an slave server will be elected as the master server 1: master-slave replication mongodb supports passing through different
6. Architecture management mongodb master-Slave clusters can be divided into two types: 1: master-Slave replication (master-Slave) -- The Slave server will not take the initiative to become the master server, you need to set up 2: replica Sets replication (replica set) -- if the master server fails, an slave server will be elected as the master server 1: master-slave replication mongodb supports passing through different
Vi. Architecture Management
Mongodb master-slave clusters are divided into two types:
1: master-Slave replication (master-Slave) -- The Slave server will not automatically change to the master server. You need to set the Slave server.
2: replica Sets (replica set) -- if the master server fails, an slave server is elected as the master server.
I. Master-slave Replication
Mongodb supports failover and redundancy through asynchronous replication on multiple machines.
Only one in multiple machines is used for write operations. This situation guarantees data consistency for mongodb.
Machines that assume the primary role can distribute read operations to slave.
One master, one slave, one master, and multiple slaves
You only need to add the-master parameter when a server is started, and add the-slave and-source parameters to the other server to implement synchronization.
This method is not recommended for the latest mongodb version.
Start the simulation (start the simulation directly without user verification ):
1: open two terminals (install two mongodb databases)
* -- You can simulate it on the same computer, but just create the corresponding directory of dbpath logpath.
Create a file under mongodb/(you only need to open two terminal links and do not need to install multiple mongodb databases)
Bin (mongodb built-in file) data1 data2 dblog1 dblog2 -- 1 is the file used by the master service; 2 is the file used by the slave server.
2: Master (set a master server)
. /Mongod -- master -- dbpath =/usr/local/mongodb/data1/-- logpath =/usr/local/mongodb/dblog1 -- fork -- port 20001 (port enables a corresponding port)
3: From -- source to "source", link the server and Port
./Mongod -- slave -- source 127.0.0.100: 20001 -- dbpath =/usr/local/mongodb/data2/-- logpath =/usr/local/mongodb/dblog2 -- fork -- port 20002
From:./mongo -- port 20002
4: MASTER:./mongo -- port 20001
Db. c1.insert ({name: "user1 "});
Db. c1.insert ({name: "user2 "});
5: The c1 set created by the master has arrived.
Db. c1.find ();
Db. c1.insert ({name: "user3"}); -- the slave server cannot be inserted. The "not master" error is returned"
To reduce the pressure on the master server, you can back up the backup or anything from the server (for example, if the data is large, a backup will increase the pressure on the master server by dozens of minutes ):
./Mongodump -- port 20002-d test backup test Database
** In this way, the master server can be operated during addition, deletion, and modification, and the link to the slave server during read.
Ii. Replica Sets replication (Replica set)
Mongodb 1.6 has developed the replica set, which mainly includes fault automatic failover and Automatic Repair of member nodes.
The most significant difference between data consistency is that the replica set does not have a fixed master node, which is a master node elected by the entire cluster.
It is strongly recommended to change other nodes. (After the master node fails, one of the master servers is automatically selected as the master server)
Deploy Replica Sets:
1): Create the corresponding file mongodb/(create the key, data, and log folders) -- the key is the group member who saves the same Meeting, used for election of the master server
Bin key (create key file: touch key1, touch key2) data (create folder data1, data2) log (create folder dblog1, dblog2)
2): import the two keys in the key Directory (for linux commands, You can manually create them in Windows)
Echo "123456"> key1
Echo "123456"> key2
Chmod 600 * -- remember: These two keys must be set to 600 (under the key file)
3): After preparation, start the game (enable 2 mongodb services)
Rs1: defines a group. The group number is rs1.
. /Mongod -- replSet rs1 -- keyFile =/usr/local/mongodb/key/key1 -- port 20001 -- dbpath =/usr/local/mongodb/data/data1/-- logpath =/local /mongodb/log/dblog1 -- fork
When Yes, start one:
. /Mongod -- replSet rs1 -- keyFile =/usr/local/mongodb/key/key2 -- port 20002 -- dbpath =/usr/local/mongodb/data/data2/-- logpath =/local /mongodb/log/dblog2 -- fork
* The replica set is started.
4): two members will have a meeting automatically. Now, the configuration of the meeting room is created.
./Mongo -- port 20001 ------> (20002 is fine, which machine can be used)
Configure to create a replica set (group list ):
Config_rs1 = {
_ Id: "rs1 ",
Members :['
{_ Id: 0, host: "localhost: 20001", priority: 1}, -- indicates the priority. The higher the number, the higher the priority.
{_ Id: 1, host: "localhost: 20002", priority: 2 },
']
} -- Press enter. You can directly press enter to view config_rs1.
Initialize replica set configuration (group list ):
Rs. initiate (config_rs1); -- press enter and press Enter. You will find that the ">" symbol of mongodb is changed to "SECONDARY>" (slave) or "PRIMARY>" (master)
Why do they become symbols? (Mongodb's ">" symbol to "SECONDARY>" (slave) or "PRIMARY>" (master )):
When you initialize the replica set, they will elect localhost: 20002 as the master server after the meeting (the higher the priority number, the higher the priority ),
Therefore, localhost: 20002 is elected as the master server "PRIMARY>"
5): Data Testing
./Mongo -- port 20002 (master)
Db. c1.insert ({name: "user1 "});
Db. c1.insert ({name: "user2 "});
Db. c1.find ();
Rs. status (); -- view the status of the replica set
Log on to the slave server (synchronize the added or deleted data from the slave server ):
./Mongo -- port 20001
Show tables; -- if an error is reported, you do not have the permission.
Rs. slaveOk (); -- In the replica set Master/Slave, you can click this command to confirm that your identity is from the server.
Show tables; --
Db. c1.find (); -- the master data can be read.
Db. c1.insert ({name: "user2"}); -- the master and slave nodes of the replica set are the same. data cannot be inserted from the replica set because it is not the master server.
Failover:
More than two mongodb servers are required;
The replica set is better than the traditional master-salve in that it can perform automatic failover of faults.
If we stop a member in the replica set, the remaining Members will automatically elect a new member to act as the master database.