Problem? Using MongoDB to manipulate the concepts of clusters, master-slave replication, replica sets, distributed storage, Java calls MongoDB
I. cluster concept and use
Multi-State computer, external service, dealing with load balancing problems.
a cluster is a set of independent computers interconnected by high-speed networks, which form a group and can serve them externally.
second, the concept of master-slave replication and use
How do you understand it? What to do on the main server and what to do from the server.
Master configuration (only transactional operations are performed)
from the configuration (only read operations can be performed, transaction operations cannot be performed)
Log in to both servers separately, and open two cmd
Transactional operations to the primary server, and read from the server ( read only ) to view data synchronization
Cons: No fail-back functionality
third, the concept and use of replica sets
A replica set is a master-slave cluster with automatic recovery.
The biggest difference between master and slave clusters and replica sets is that the replica set does not have a fixed " master node "; the entire cluster chooses a "master node ", and when it fails, it the rest of the other nodes selected from the node for the "Master node" to continue to service, masking a way to achieve the 7x24 Continuous service work, replica lumped has an active point (Primary ) and one or more backup nodes (secondary).
Create a node set: First set up folders Mongodb/logs/node1, Node2, Node3 and Mongodb/logs/dbs/node1, Node2, Node3
Take three nodes as an example:
Node 1:
host:localhost:10001
Log File:d:\mongodb\logs\node1\logs.txt
Data File:d:\mongodb\dbs\node1
Node 2:
host:localhost:10002
Log File:d:\mongodb\logs\node2\logs.txt
Data File:d:\mongodb\dbs\node2
Node 3:
host:localhost:10003
Log File:d:\mongodb\logs\node3\logs.txt
Data File:d:\mongodb\dbs\node3
Start node:
Open three cmd
Start node 1:mongod--dbpath D:\mongodb\dbs\node1--logpath D:\mongodb\logs\node1\logs.txt--logappend--port 10001-- Replset itcast/localhost:10002 --master//boot node 2:mongod--dbpath D:\mongodb\dbs\node2--logpath D:\mongodb\logs\ Node2\logs.txt--logappend--port 10002--replset itcast/localhost:10001//start node 3: mongod--dbpath D:\mongodb\dbs\ Node3--logpath D:\mongodb\logs\node3\logs.txt--logappend--port 10003--replset itcast/localhost:10001,localhost : 10002
Also open CMD login service, and initialize three nodes
See if this node is the primary node
Do an insert operation to see if the data is synchronized, or just the primary node for transactional operations.
Then hang one in it, then look at that as the master node, and can do transaction operations, priority top up.
iv. Distributed storage concepts and usagemainly in order to reduce the database data volume of pressure, the role of pressure, the specific concept of Baidu has
Create a new three Distributed node folder (I put it on three disks character)
Create a new normal node (also placed on the disk character), storing the configuration information (route recognition)
Start the Configuration node first
Start a routing node, and connect the configuration node:
All that's left is a distributed three node.
First node:
A second node:
A third node:
Now it's time to initialize the three nodes in the configuration node, which is equivalent to the configuration:
Log in to the Routing node service, and the entry configuration is automatically stored in the configuration node before they are associated.
Initialize three nodes
allowlocal:true: means that you can connect to these three distributed nodes without routing or access
And key indicates that age is a shard , shardcollection indicates that the collection that needs to distribute the store is that, add 10 million documents
Then three client connections to three distributed nodes , you can see the data as follows:
will be a test. Person collection, the road will be based on their own judgment through the age shard, balanced three distributed nodes to store data, and finally access data.
v.Java calls MongoDB
Import Package:Mongo-2.10.1.jar
Package Com.itcast.mongodbdao;import Java.net.unknownhostexception;import Org.bson.types.objectid;import Org.junit.test;import Com.mongodb.basicdbobject;import Com.mongodb.db;import Com.mongodb.dbcollection;import Com.mongodb.dbcursor;import Com.mongodb.dbobject;import Com.mongodb.mongo;import Com.mongodb.util.JSON;public Class Mongodbdao {@Testpublic void Testadd () throws unknownhostexception {Mongo Mongo = new Mongo ("localhost", 27017);D b d b = Mongo.getdb ("test");D bcollection collection = db.getcollection ("person"); Basicdbobject dbobject = new Basicdbobject ();d bobject.put ("name", "Li San1");d bobject.put ("Descr", "Life Friendship"); Collection.insert (DBObject); Mongo.close ();} @Testpublic void Find () throws Unknownhostexception {Mongo Mongo = new Mongo ("localhost", 27017);//Query All DATABASESYSTEM.O UT.PRINTLN ("Query all Database"), for (String name:mongo.getDatabaseNames ()) {System.out.println (name);} Query all clustered Collections System.out.println ("Query all Clustered Collections");D b DB = mongo.getdb ("test"); for (String name:db.getCollectionNames ()){System.out.println (name);} SYSTEM.OUT.PRINTLN ("Query all Data");D bcollection collection = db.getcollection ("person");D bcursor cursor = Collection.find (); while (Cursor.hasnext ()) {DBObject object = Cursor.next (); System.out.println (Object.ToString ()); System.out.println (Object.get ("name"));} System.out.println ("Number of documents:" +cursor.count ()); System.out.println ("ID:" +cursor.getcursorid ()); System.out.println ("JSON Data:" +json.serialize (cursor)); Mongo.close ();} @Testpublic void Delete () throws Unknownhostexception{mongo Mongo = new Mongo ("localhost", 27017);D b DB = Mongo.getdb ("tes T ");D bcollection collection = db.getcollection (" person "); Basicdbobject dbobject = new Basicdbobject ();d bobject.put ("name", "Li San1"); Collection.remove (dbobject);} @Testpublic void Update () throws Unknownhostexception{mongo Mongo = new Mongo ("localhost", 27017);D b DB = Mongo.getdb ("tes T ");D bcollection collection = db.getcollection (" person "); Basicdbobject dbobject = new Basicdbobject ();d bobject.put ("name", "Li Junjun"); Collection.update (NEW basicdbobject ("_id", New ObjectId ("5741c13f8c89776a5081a962")), DBObject, True, true);//First true if the database does not exist, add it, The second true only modifies the first day, true if more than one is modified}}
Vi. SummaryIn This article, the concept is not introduced too much, mainly is the operation of attention, and the whole process of understanding. cluster, master-slave replication, replica set, distributed storage concepts must be understood, the actual web in the interview summary often asked.
The enhancement of the MongoDB of NOSQL "II"