Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb Master-Slave replication _ Power Node Java College arrangement

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Starting with this article, we focus on the deployment technology of mongodb.

We know that sql server can achieve read-write separation, dual-computer hot backup and cluster deployment, and of course mongodb can also do it. In practical applications, we do not want the database to use a single point of deployment. It is very bad if the database is down or destroyed.

One: master-slave replication

1: first take a look at the model diagram

2: from the above graph, we can analyze that this architecture has the following benefits:

Data backup.

Data recovery.

Separation of reading and writing.

3: let's practice one by one.

In practical application, we must be multi-server deployment, limited to our own lazy installation of virtual machines, and put it into practice on one machine.

Step 1: we put the mongodb folder on disk D and disk E, and simulate it on multiple servers.

Step 2: start mongodb on D disk and designate the database as the primary database. In fact, the command is very simple: > mongodb-- dbpath='XXX'-- master

The port is still 27017 by default.

Step 3: start the mongodb on the E disk in the same way, specify the database as the slave database, and the command is very simple, of course, we need to change a port, for example: 8888.

Source represents the address of the primary database.

> mongod-dbpath=xxxx-port=8888-slave-source=127.0.0.1:27017

Step 4: from the red area in the picture, we find a statement like "applied 1 operations", and it occurs at an interval of 10 seconds, which means that every 10 seconds for the dependent database.

Synchronize the data to the master database, which is based on finding the "OpLog" log of the master database, and the word "sync_pullOpLog" can be found in the red area of the picture.

The next thing we have to do is to test, surprised to find that the data has been updated synchronously, cool.

4: if I want to add a slave database, but I don't want to specify it at startup, but specify it later, can mongodb do it? The answer is definitely yes.

Both our master and slave databases have a collection called local, which is mainly used to store internal replication information.

OK, then let's give it a try. I'll copy a copy of mongodb's running program on F disk. There are a lot of cmd windows, so don't mess it up.

Look at the log above, indicating that there is no master database, it doesn't matter, one day we find our conscience and give him a subsidy in the later stage, , open another cmd window, that is, the sentence.

Add a host address in sources, and finally find that the data is also synchronized to 127.0.0.1 VOL5555, a slave database.

5: read-write separation

This method is implemented in larger architectures, but it is actually very simple in mongodb. By default, dependent databases do not support data reading, but it doesn't matter.

We are provided with a "slaveOkay" in the driver so that we can read the slave database to reduce the performance pressure on the master database, which is not demonstrated here.

Two: copy set

This is also a very powerful X master-slave cluster, but it is still different from the above cluster in two points.

The cluster does not have a specific master database

If a master database goes down, a slave database will be selected as the top of the master database in the cluster, which has the function of automatic fault recovery.

OK, let's try it now. First, close all the cmd windows and clean up all the files under db.

Step 1: since we want to set up a cluster, we have to choose a cluster name. Here we will use our company name shopex.-- replSet means to let the server know that there are other databases under the shopex.

Here, open the mongodb program in D disk, port is 2222. The specified port 3333 is another database server under the shopex cluster.

Step 2: since it is said above that 3333 is another database server, don't worry, open it now. Here, open the mongodb program of the E disk.

Step 3: ok, take a look at the red area of the log above. It seems that we haven't finished yet. Yes, the log message tells us to initialize the "replica set". Since the log says so, I will.

To do this, connect to any server you want, but be sure to enter the admin collection.

Step 4: after the startup is successful, we need to see who can become the primary database server. We can see that the one with port 2222 has become the primary database server.

Step 5: we know that there is an arbitration server in sql server, so there is also an arbitration server in mongodb. Like sql server, arbitration only participates in voting. Here we

Use the mongodb of the F disk as the arbitration server, and then specify any server port in the shopex cluster, which is 2222.

Then we append it with rs.addArb () in the admin collection.

Once appended, we use rs.status () to check the status of the servers in the cluster. In the figure, we can clearly see who is the master, slave, or arbitration.

Isn't it said that the cluster has automatic failure recovery? Then we can try it. Press Ctrl+C on the cmd server at port 2222 to KO the server, and immediately we find out

The slave server on port 3333 can be topped up, and finally you can also use rs.status () again to see the status of the servers in the cluster.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report