Apsaradb for mongodb basic series-detailed setup and explanation of master-slave Replication

Source: Internet
Author: User

It was a time before the last mongodb, and I had to update it as soon as possible. However, due to various things, it was temporarily put on hold. Looking back at my first blog, I was shocked by oh, my god, and my friends half a year ago.

But I still have the courage to go on ......

I have also introduced java operations on mongodb in my previous blog, so this blog will not introduce addition, deletion, modification, and query under the command line. Because mongodb integrates shell commands and has good integration with javascript, you can perform simple javascript operations under the command line.

For example, for loop. For (var I = 0; I <3; I ++) {db. my. insert ({name: "test", age: 18})} Where my is a custom collection. You can try it. No problem. If there is any problem, call me directly.

The reason is that I will not introduce this one more, because I can directly view its api in the console, and mongodb has a way to complete the full function, similar to the java ide environment, which makes it easier for us again.

In the console, you can press the tab key once to automatically complete mongodb. This is the benefit of shell commands. At the same time, in the console, directly type the corresponding method and print its api without parentheses. For example, db. my. update. Try it.

OK, go to today's topic.

Master-slave replication: master-slave mode. In general, the master server copies data to the slave server, thus ensuring the synchronization and consistency between the two. This advantage is self-evident. If the master server is broken, haha, don't be afraid, there will be miles from the server. However, the conversion from the server to the server fails. [How do I feel like a concuous? O (∩ _ ∩) O]

However, let's not talk about his shortcomings first. Let's look at his advantages. people cannot be greedy, but they can't eat fat people.

Master-slave replication, automatic synchronization, so we don't need to manually maintain it. How can we ensure data security in previous projects? First, insert the data to this database. Because the data is very important at that time, you need to back up the data, then insert success, and then insert another database. So when testing the insert function, you can have a cup of coffee ~ \ (Too many rows )/~ La
Manually test this function.

1. preparation phase: because of a server, two folders are created to simulate the master folder and slave folder. Note that two different ports are enabled.

2. Start the master node: mongod -- dbpath E: \ mastersavle \ master -- port 10000 -- master
Start slave node: mongod -- dbpath E: \ mastersavle \ master \ slave -- port 10001 -- slave -- source localhost: 10000

Note that there are two consoles.

The startup effect is as follows:

3. open another window to connect to the 10000 master server. Then we insert a piece of data to see the effect.

By default, the test server is connected. use a mysql-like use to switch databases. Use my; Note: execute this statement. If my database exists, switch directly. If it does not exist, create an implicit statement. If you check the folder, you cannot see it because it is implicit, only after you insert data can you see this my database.

Db. my. insert ({name: "test"}); insert a piece of data and create a my set (table). You can run the db command to view the current database. You can use db. showtables or db. showcollections; To View tables in the database. System. index has this table by default. This is the table that stores the index. We will discuss it later.

4. After inserting data into the master server, let's look at the effect in the database:

Note: You can only query data on the server, but cannot insert, delete, or update data.

5. In fact, mongodb records oplog for all our operations and stores it in the local oplog. $ main set of special databases. Note: Only the operations for changing the database status are saved in the local database. The insert, delete, and update. query operations find () are not recorded with us.

View oplog records: [another master-slave replication mode is also saved in this collection]

6. Introduce the data structure of the oplog. $ main set. What do these mean? How can we find useful information? Why should we know this?

Because of the replication mechanism, you can view the oplog information to maintain Database Synchronization consistency.

For example, this set has the attribute key: ts, op, n, o

In fact: ts: represents the 8-byte timestamp. It is very important to indicate this value by 4-byte unix timestamp + 4-byte auto-increment count. In the election (such as when the master is down) in the new primary, the secondary with the largest ts is selected as the new primary. [Next we will introduce another master-slave mode replica set]

Op: 1-byte operation type. For example, I indicates insert and d indicates delete. [For example, we can view in the log that op is I, which is exactly the information we just inserted]

Ns: The namespace of the operation. [Namespace is the current data. Current collection. Use db. my. insert to create my set by default]

O: The document corresponding to the operation, that is, the content of the current operation (for example, the field and value to be updated during the update operation [A specific information of our insert ])

O2: The where condition when performing the update operation. This attribute is available only during update. [This attribute is displayed]

Op can be one of the following situations:

"I": insert

"U": update

"D": delete

"C": db cmd

"Db": Declares the current database (where ns is set to => database name + '.')

"N": no op, that is, an empty operation, which is periodically executed to ensure timeliness

7. display other op values in log records, such as deletion and update. [Note]

Op: d delete operation. O: Delete the specific entry. Because _ id is unique.

Op: u update operation. O: update the record. o2: update the field.

Ah, I have been talking about N long times. I will update another master-slave replication mode in the next blog: replica set.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.