MongoDB learning [a] -MongoDB Introduction and Installation

A, MongoDB Profile

What is 1.MongoDB

MongoDB is a powerful, flexible and easy to expand universal database, MongoDB is written in C ++ language, is an open source database distributed file system based storage.

In the case of high load, add more nodes, you can ensure server performance.

MongoDB is designed to provide scalable, high-performance data storage solution for WEB applications.

MongoDB the data is stored as a document data structure of the key (key => value) pairs. MongoDB document similar to JSON object. Field value can contain other documents, arrays and array of documents.

2.MongoDB features

1. Ease of use

MongoDB is written in C ++, is an open source database system based on a distributed file storage, it is not a relational database. In the case of high load, add more nodes, you can ensure server performance.

MongoDB is a document (document-oriented) oriented database, rather than a relational database.

Do not use a relational mainly in order to get better scalability. Of course, there are other benefits, compared with relational databases, document-oriented database is no longer "line" (row) is replaced by a more flexible concept of "document" (document) model.

By embedding documents and arrays in the document, document-oriented approach using only one record to the performance of complex hierarchical relationships, which is a modern object-oriented language developers a consistent view of the data.

Further, there is no predefined pattern (predefined schema): document key (key) and the value (value) is no longer a fixed type and size. Since there is no fixed pattern, add or remove fields become easier.

Usually because developers can quickly iterate so the development process to be accelerated. Moreover, the experiments easier. Developers can try a lot of data model, selected from one of the best.

2. Easy Extended

Size of the application data sets is growing at an incredible rate. With the decline of available memory bandwidth and increase prices, even a small-scale applications, the amount of data needs to be stored may be shocking, even beyond the processing power of many databases. Over the past very rare T-level data, it is now commonplace.

As the amount of data that needs to be stored continues to grow, developers face a problem: how to extend the database is divided into scale-up and scale out, scale-up is the most labor-saving practices, but the drawback is that mainframes are generally very expensive, and when the amount of data upon reaching the physical limits of the machine, spend more money can not buy more machines, and this time select the scale more appropriate, but the machine too much scale brought about another problem that needs to be managed.

The design uses MongoDB scale. Document-oriented data model so that it can easily be split between multiple data servers. MongoDB can automatically process data and load across the cluster, automatically reassign documents, as well as the user's request is routed to the correct machine. In this way, developers can concentrate on writing the application, without the need to consider how to expand the problem.

If a cluster requires larger capacity, only need to add a new cluster server, MongoDB will automatically transfer existing data to the new server.

3. The feature-rich

MongoDB as a general-purpose database, in addition to the ability to create, read, update and delete data, but also provides a series of ever-expanding unique features.

  • index

Support universal secondary indexes, allowing a variety of quick query and provide a unique index, a composite index, geospatial indexes, full-text indexing

  • polymerization

Support Polymer pipes, the user can simply create complex set of segments, and automatically optimized database

  • Special collection types

Support the existence of a limited time collection, suitable for data that will expire at some point, as the session session. Similarly, MongoDB also supports a set of fixed size , for storing recent data, such as log

  • File Storage

It supports a very easy to use protocol for storing large files and file metadata. MongoDB does not have some of the very common relational database features, such as links to join and complex multi-line transactions. Function is omitted in consideration of the architecture, or for better scalability, because it is difficult to efficiently achieve both functions in a distributed system

4. superior performance

A major goal of MongoDB is to provide superior performance, which largely determines the MongoDB design. MongoDB put as much memory as a cache cache, the view for each query automatically select the correct index.

In short all aspects of the design are aimed at maintaining its high performance, although very powerful MongoDB and attempts to preserve many features of a relational database, but it does not pursue all the features of a relational database.

Whenever possible, the database server will process logic to the client. This is designed to streamline the way one of the reasons MongoDB can achieve such high performance.

Two, MongoDB installation

1. Download and install

1. Download the installation package

MongoDB provides a 32-bit and 64-bit system can be used for pre-compiled binary packages, you can install the downloaded from the official website MongoDB, MongoDB precompiled binaries Download: https://www.mongodb.com/download-center#community

2. Install

Specify the installation path, I've installed in D: \ ProgramFiles \ MongoDB, add D: \ ProgramFiles \ MongoDB \ bin to the environment variable.

3. Create a directory and folder

D:\ProgramFiles\MongoDB\data\db
D:\ProgramFiles\MongoDB\log\mongod.log

 

4. Create a new profile mongo.cfg, reference https://docs.mongodb.com/manual/reference/configuration-options/

systemLog:
   destination: file
   path: "D:\ProgramFiles\MongoDB\log\mongod.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "D:\ProgramFiles\MongoDB\data\db"
net:
   bindIp: 0.0.0.0
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false

 

5. Production System Services

mongod --config "D:\ProgramFiles\MongoDB\mongod.cfg" --bind_ip 0.0.0.0 --install

 

Or command line directly specify the configuration

mongod --bind_ip 0.0.0.0 --port 27017 --logpath D:\ProgramFiles\MongoDB\log\mongod.log --logappend --dbpath    D:\ProgramFiles\MongoDB\data\db --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install

 

6. Start the MongoDB service

net start MongoDB
net stop MongoDB

 

7. Log MongoDB

mongo 
link: HTTP: // www.runoob.com/mongodb/mongodb-window-install.html 
  When there is no login account password, the default is the administrator login. Because the system just do not have time to install the service specified
   --auth (not specified certification authority that is not to say), (the equivalent of mysql skip the authorization to start the same table)

 

2. Account Management

Account Management: https://docs.mongodb.com/master/tutorial/enable-authentication/

1. Create a user has permission

ADMIN use 
db.createUser ( 
    { 
        User: " root " , the root can easily write #
         pwd : " 123 " , 
        Roles: [{Role: " root " , DB: " ADMIN " }] # permissions, role is the root note is administrator, 
    } 
) 
use Test 
db.createUser ( 
    { 
        User: " Egon " ,
         pwd : " 123 " , 
        Roles: [ 
        {Role: "readWrite" , Db: " test " }, # library has read and write permissions for the test, operating its own library has read and write permissions 
        {Role: " the Read " , db: " db1 " } 
        ] # db1 database for read access, the operation of other libraries has read access 
    } 
)

 

2. Restart the database

mongod --remove
mongod --config "C:\mongodb\mongod.cfg" --bind_ip 0.0.0.0 --install --auth
​
# 或者
mongod --bind_ip 0.0.0.0 --port 27017 --logpath D:\MongoDB\log\mongod.log --logappend --dbpath
D:\MongoDB\data\db --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install --auth

 

3. Log in, pay attention to use double quotes instead of single quotes

# Way a 
mongo --port 27017 -u " root " -p " 123 " --authenticationDatabase " ADMIN " 
# Second way: After you log in with db.auth ( " account " , " password " login) 
mongo 
use ADMIN 
db. the auth ( " the root " , " 123 " )

 

Recommended blog https://www.cnblogs.com/zhoujinyi/p/4610050.html

Create an account + password authentication mechanism open

3. command line shell

1 .mongo 127.0 . 0.1 : 27017 / config # connect to any database config
 2 .mongo - Nodb # does not connect to any database
 3 after starting, running new Mongo (when required. Hostname ) command can connect to the desired the mongod
 > Conn = new new Mongo ( ' 127.0.0.1:27017 ' ) 
Connection to 127.0 . 0.1 : 27017 
> conn.getDB DB = ( ' ADMIN ' ) 
ADMIN 
. 4 .help View help
 . 5 .mongo is a simplified JavaScript shell, you can execute JavaScript scripts

 

 

 

Guess you like

Origin www.cnblogs.com/ryxiong-blog/p/11204194.html