1. Master-slave replication of mongodb:
Main server:
mongod –dbpath “F:\mongodb\data” –port 27017
From the server:
mongod –slave –dbpath “F:\mongodb-slave\data” –port 10001 –source localhost:27017 –only test
Option Description:
–only
Specify to copy a database on the slave node (by default, all databases are copied)
–slavedelay
Used on slave nodes to add delay (seconds) when applying master node operations
–fastsync
Start the slave node based on the data snapshot of the master node. If the data directory is initially a data snapshot of the master node, starting the slave node with this option is much faster than a full synchronization.
–autoresync
Automatically resyncs if the slave gets out of sync with the master
–oplogSize
The size of the primary node oplog (MB)
Generally, there are no more than 12 slave nodes in a master-slave service group, so as to avoid thousands of slave nodes from initiating queries on a single master node and dragging down the master node
2. Replica set of mongodb:
A replica set is a master-slave cluster with automatic failure recovery.
The most obvious difference between a master-slave cluster and a replica set is that the replica set does not have a fixed master node. The replica set can be regarded as a cluster. The entire cluster will elect a master node, and other nodes will be activated when it fails to work properly.
Note: you can’t use localhost as a mongodb address member, we need to find the hostname (fornane-PC)
In addition, give the mongodb cluster a name, such as: blort
The main steps to configure a mongodb replica cluster are divided into two parts:
Part 1: Start the node service
The first node start command:
mongod –dbpath “F:\mongodb-node1\data” –port 10001 –replSet blort/fornane-PC:10002
The second node start command:
mongod –dbpath “F:\mongodb-node2\data” –port 10002 –replSet blort/fornane-PC:10001
Part II: Initialize configuration information
This part is actually to initialize the configuration information of the entire cluster to the primary node of mongodb. This information is recorded in the system.replset collection of the local library of the primary node (active node), which can be viewed by the following command:
use local;
db.system.replset.find();
or
rs.conf();
The following are commands for initializing cluster information (executed in the shell):
db.runCommand({
“replSetInitiate”: {
“_id”: “blort”,
“members”: [{
“_id”: 1,
“host”: “fornane-PC:10001”
},
{
“_id”: 2,
“host”: “fornane-PC:10002”
}]
}
})
Of course you can also use
rs.initiate({
_id : “blort”,
members: [
{“_id”: 1,”host”: “PC-201007141658:10001”},
{“_id”: 2,”host”: “PC-201007141658:10002”}
]
})
Ok, so far the configuration of the mongodb cluster with two nodes is complete, you can connect to the two servers through the shell to view the status, note that data can only be read and written in the primary node.
To test whether it is successful, you only need to create a new db in the primary node, and then check whether the db is successfully synchronized on the secondary node after writing the data!
To query data in the secondary node, you need to execute the following command (read extension):
db.getMongo().setSlaveOk();
or:
rs.slaveOk();
The method of adding a third node based on the above two nodes:
Step 1: Start the node service and broadcast to any of the other existing nodes:
mongod –dbpath “F:\mongodb-node3\data” –port 10003 –replSet blort/fornane-PC:10002
Step 2: Modify the system.replset information in the current primary node, and add the information of the newly added node:
rs.add(“fornane-PC:10003”)
At this point, the third node is added, and the method of adding more subsequent nodes is consistent with this.
The test method is the same as above.
The following are the data synchronization results of each node:
node 1:
node 2:
node 3:
The cluster information is as follows:
Note: 1. The host name is PC-201007141658
2.
After the initial primary node (10001) is shut down, the original secondary node (10003) is elected as the primary node
Attached are commonly used commands to view cluster information:
rs.status(); current status
rs.conf(); current configuration information
rs.slaveOk(); read extension
rs.remove(“fornane-PC:10003”); delete node
refer to:
http://cn.docs.mongodb.org/manual/reference/method/js-replication/
3. Automatic sharding of mongodb
The structure is as follows:
In this way, only Mongos needs to be exposed, and specific queries, inserts, etc. are all automatically sharded by mongodb
Deployment steps:
1. Start Config Server
mongod –dbpath “F:/mongodb-shard/config-server/data” –port 20000
2. Start Mongo Server
mongod –port 30000 –configdb localhost:20000
3. Start Mongod Server
mongod –dbpath “F:/mongodb-shard/mongod-server/data” –port 10000
4. Connect to Mongos Server through the shell
mongo –port localhost:30000/admin
Note: use admin to log in, otherwise there will be no permission later
Check if connected to Mongos Server
sh._checkMongos()
If an error is reported, execute first
use admin
Reference: http://docs.mongodb.org/manual/reference/method/js-sharding/
5. Add shards
sh.addShard(“localhost:10000”)
or
db.runCommand({addShard : “localhost:10000” , allowLocal : true})
6. Split data
sh. shardCollection(“foo”);
sh.shardCollection(“foo.users”, { _id:1} , unique : true )
or
db. runCommand({“enablesharding”:foo})
db.runCommand({“shardcollection”:”foo.users”, “key”:{“_id”:1}, “unique”: true})
ok, the sharding configuration is complete here.
Of course, in order to see the specific fragmentation, we thought about adding a Mongod Server and execute it after startup
sh.addShard(“localhost:10001”)
Finally, connect mongodb through java, and write 1030002 records, you can see the effect, java
Mongo mongo = new Mongo(“localhost”, 30000);
DB db = mongo.getDB(“foo”);
DBCollection users = db.getCollection(“users”);
for(int i =0 ; i<1000000 ; i++) {
DBObject user = new BasicDBObject();
user.put(“username”, “cbpan” + Math.random());
users. save(user);
}
DBCursor cur = users. find();
System.out.println(cur.count());
Final test results:
by fornane