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

Getting started with mongodb

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Mongodb introduction

MongoDB is a high-performance, open source, schemalless document database, which is one of the most popular NoSql databases. It can be used to replace the traditional relational database or key / value storage in many scenarios. Mongo is developed using C++. The official website address of Mongo is: / / www.mongodb.org/

NoSql, whose full name is Not Only Sql, refers to a non-relational database. The next generation database mainly addresses several key points: non-relational, distributed, open source, and horizontally scalable. Originally intended for large-scale web applications, the campaign began in early 2009 with features such as: free mode, support for easy replication, simple API, ultimate consistency (non-ACID), large-capacity data, etc. NoSQL is most used by us when key-value storage, of course, there are other document, column storage, schema database, xml database and so on.

Characteristics

High performance, easy to deploy, easy to use, very convenient to store data. The main functional features are:

For collection storage, it is easy to store data of object type.

Mode freedom.

Dynamic query is supported.

Full indexing is supported, including internal objects.

Query is supported.

Replication and failure recovery are supported.

Use efficient binary data storage, including large objects such as video, etc.

Automatically handle fragments to support scalability at the cloud computing level

Drivers for Python,PHP,Ruby,Java,C,C#,Javascript,Perl and C++ are supported, and drivers for platforms such as Erlang and .NET are also available in the community.

The file is stored in BSON (an extension of JSON).

Can be accessed through the network.

Function

Collection-oriented storage: suitable for storing objects and data in JSON form.

Dynamic query: Mongo supports rich query expressions. Query instructions use tags in the form of JSON to easily query objects and arrays embedded in a document.

Complete indexing support: including document embedded objects and arrays. Mongo's query optimizer parses the query expression and generates an efficient query plan.

Query monitoring: Mongo includes a monitoring tool for analyzing the performance of database operations.

Replication and automatic failover: Mongo database supports data replication between servers, master-slave mode and mutual replication between servers. The primary goal of replication is to provide redundancy and automatic failover.

Efficient traditional storage: supports binary data and large objects (such as photos or pictures)

Automatic sharding to support cloud-level scalability: the automatic sharding feature supports horizontal database clusters and can dynamically add additional machines.

Applicable scenario

Website data: Mongo is very suitable for real-time insertion, update and query, and has the replication and high scalability required for website real-time data storage.

Caching: due to its high performance, Mongo is also suitable as a cache layer for the information infrastructure.

Large-size, low-value data: it may be expensive to use traditional relational databases to store some data. before that, programmers often choose traditional files for storage.

Highly scalable scenario: Mongo is ideal for databases consisting of dozens or hundreds of servers. Mongo's roadmap already includes built-in support for the MapReduce engine.

For object and JSON data storage: Mongo's BSON data format is very suitable for document format storage and query.

Restrictions on the use of MongoDB

A highly transactional system, such as a banking or accounting system. At present, the traditional relational database is more suitable for applications that require a large number of atomic complex transactions.

Traditional business intelligence applications: BI databases for specific problems will produce highly optimized queries. For such applications, data warehouse may be a more appropriate choice.

Installation and operation

When downloading the corresponding version on the official website, you must distinguish between 32-bit and 64-bit.

Take the 32-bit tar.gz package as an example here.

Tar

Change the name of mv to mongodb

Move mv to the / opt directory and modify the permission to 777

Mkdir-p / data/db, and modify the permission to 777

Vi / .bashrc

Join: export PATH=/opt/mongodb/bin:$PATH

Restart the machine

Enter the command mongod to start, you can see the port, and the browser can access the IP+ port (do not close the window)

Delete the mongod.lock under / data/db if it cannot be started

Basic operation of mongoVUE

MongoVUE is a client management tool for MongoDB under windwos, and it is generally used to operate.

Name write casually, server write your server's ip, port number, the default port of mongodb is 27017, then connect to OK

System structure

The document (document) of MongoDB is equivalent to a row of records in a relational database.

Multiple documents form a collection, which is equivalent to tables in a relational database.

Multiple collections (collection), logically organized together, are databases (database).

One MongoDB instance supports multiple databases (database).

Single condition query

In query

Multi-condition query and

Multi-condition query or

Show query field

Sort (1 ascending;-1 descending)

Insert record

Modify record

Delete record

Enter the condition and run it to delete it

Mongodb Monitoring-- mongostat

Getmore: usually occurs in a query with a large result set, the first query returns part of the results, and the subsequent results are obtained through getmore

Flushs: the number of times synchronization is performed per second to write data to the hard disk.

The size of the data that mapped maps to memory, in MB

Vsize: virtual memory usage (in MB)

Res: physical memory usage (in MB)

Faults: the number of access failures per second (only available in Linux). The data is swapped out of physical memory and put into swap. Do not exceed 100. otherwise, the machine memory is too small, resulting in frequent swap writes. At this point, you need to upgrade memory or expand

Locked db: the percentage of time locked up. Try to keep it below 10%.

Idx miss%: percentage of index misses. If it is too high, consider whether the design of the index is unreasonable.

Qr | qw ar | aw: when the load of mongo is high, it is too late to process the command, so mongo puts the command in the queue. Qr | length of queue qw waiting for read / write ar | number of aw clients performing read / write operations.

Conn: how many connections are there

Mongodb profiler

Similar to slow query in mysql

Turn on profile

Db.setProfilingLevel (n)

N:

0-turn off performance analysis, the test environment can be opened, and the build environment is closed, which has a great impact on performance

1-Open the slow query log and execute statements that take more than 100 milliseconds

2-Open all operation logs

Db.setProfilingLevel (level, slowms)

The Mongo Profile record is directly stored in the system db, and the record location local "collections" system.profile

Key indicators and common optimization

Millis: this command takes time to execute and is recorded in milliseconds

ResponseLength: the length returned by the query

Nscanned: how many objects are scanned during the query operation

Nreturned: the result object returned from the query

If nscanned (the number of records scanned) is much greater than nreturned (the number of records returned), then we should consider using index to optimize record location.

Db. Table name .determinreIndex ({age:1}) / / 1 (ascending order);-1 (descending order)

The advice for creating indexes is: if you read very little, try not to add indexes, because the more indexes you have, the slower the write operation will be. If there is a lot of reading, it is cost-effective to create an index.

When the returned result set is very large, that is, the response length value is quite large, it will degrade the performance. When making a find query, you need to add a second query parameter to get only the fields that need to be displayed.

The slow query may be caused by an index problem, or the data may not be in memory (disk read).

Maximum number of connections

Db.serverStatus () connections

Current value + available value is the current maximum number of mongodb connections

Add a parameter to the startup-- maxConns=3000

Don't forget that the maximum number of open file connections in linux will affect the setting here. Be sure to turn it up as well.

Mongo master-slave replication

The architecture is similar to the master-slave replication of mysql, and the principle is as follows:

Replication of mongodb requires at least two servers or nodes. One of them is the master, which is responsible for processing the client request, and the rest is the slave, which is responsible for mapping the data of the master node. The primary node records all actions performed on it. The slave node periodically polls the master node for operations, and then performs these operations on its own copy of the data.

Open oplog

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

Internet Technology

Wechat

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

12
Report