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

The detailed explanation of the MongoDB database, and the MongoDB4.0 version of

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Introduction to MongoDB

MongDB is a cross-platform, document-oriented database that can achieve high performance, high availability, and can be easily extended. It is an open source database system based on distributed file storage. In the case of high load, adding more nodes can ensure server performance.

MongoDB is also a product between relational database and non-relational database, which is the most functional and most like relational database in non-relational database. The main purpose of not adopting the relational model is to achieve better extensibility. MongoDB no longer has the concept of "row", and its operation mode is mainly based on two concepts: collection and document.

The characteristics of MongoDB the characteristics of MongoDB include set-oriented storage, free mode, rich query statements and multi-level index, replication set mechanism, easy horizontal expansion, pluggable storage engine, cross-platform multi-language support and so on. MongoDB is easy to install, provides document-oriented storage function, and is easy to use. MongoDB provides replication, high availability, and automatic sharding. If the load increases (more storage space and more processing power are needed), it can be distributed among other nodes in the computer network, which is called sharding. Mongo supports a rich set of query expressions. Query instructions use tags in the form of JSON to easily query objects and arrays embedded in a document. MongoDB supports a variety of programming languages: Ruby, Python, Java, C++, PHP, C # and other languages. Application area of MongoDB

MongoDB can provide scalable high-performance data storage solutions for Web applications. MongoDB is mainly applicable to website data, distributed scenarios, data caching and JSON document format storage. It is suitable for Internet applications with large amount of data, high concurrency and weak transactions. its built-in horizontal expansion mechanism provides data processing capacity from millions to billions, which can well meet the data storage requirements of Web2.0 and mobile Internet applications.

Installation of MongoDB 4. 0

MongoDB provides an installation package on the Linux platform, which can be downloaded from the official website http://www.mongodb.org/downloads. This time we chose to use the latest version of MongoDB4.0 to install and experiment.

Download the MongoDB4.0 package

Wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.0.tgztar zxvf mongodb-linux-x86_64-4.0.0.tgz-C / optmv / opt/mongodb-linux-x86_64-4.0.0 / / usr/local/mongodb creates the MongoDB data storage directory and log storage directory Configuration file # mkdir / data/mongodb1# mkdir / data/logs/mongodb# touch / data/logs/mongodb/mongodb1.log# chmod-R 777 / data/logs/mongodb/mongodb1.log# vim / usr/local/mongodb/mongodb1.conf / / add the following line dbpath=/data/mongodb1 # data storage directory logpath=/data/logs/mongodb/mongodb1.log # log file port=27017 # default server port logappend=true # write with append Log fork=true # maximum number of simultaneous connections running maxConns=5000 # in the background The default 2000storageEngine=mmapv1 # specifies that the storage engine is a memory-mapped file

Set kernel parameters

Echo 0 > / proc/sys/vm/zone_reclaim_modesysctl-w vm.zone_reclaim_mode=0 # permanently set echo never > / sys/kernel/mm/transparent_hugepage/enabledecho never > / sys/kernel/mm/transparent_hugepage/defrag

Set system environment variables for ease of use

Echo 'export MONGODB_HOME=/usr/local/mongodb' > > / etc/profileecho' export PATH=$PATH:$MONGODB_HOME/bin' > > / etc/profilesource / etc/profile

Start the mongodb service process and view the port (default 27017)

Mongod-- config / usr/local/mongodb/mongodb1.conf # enable MongoDBmongod-- config / usrlocal/mongodb/mongodb1.conf-- shutdown # stop MongoDBnetstat-ntap | grep mongodmongo-- port 270entries enter the mongo database. If the port is not specified, port 27017 is entered by default to create multiple instances.

When a single server has sufficient resources, multiple instances can be used. In order to make full use of server resources (only need to modify the data storage directory, Log file and port number and create the corresponding directory) cd / usr/local/mongodb/cp mongodb1.conf mongodb2.confvim mongodb2.conf # modify as follows # dbpath=/data/mongodb2 # data storage directory # logpath=/data/logs/mongodb/mongodb2.log # log file # port=27018 # default server port # logappend=true # write log # fork=true # background run # maxConns=5000 # maximum number of simultaneous connections The default 200 storageEngine=mmapv1 # specifies that the storage engine enables the multi-instance MongoDB logical storage structure for the memory-mapped file mkdir / data/mongodb2touch / data/logs/mongodb/mongodb2.logchmod-R 777 / data/logs/mongodb/mongodb2.logmongod-- config / usr/local/mongodb/mongodb2.conf #

The logical structure of MongoDB is mainly composed of document (document), collection (collection) and database (database). Its Chinese document is the core concept of MongoDB, it is the smallest unit of MongoDB logical storage, which is equivalent to a row of records in a relational database, a collection of documents, a set equivalent to the concept of tables in a relational database, and several constituent databases.

SQL terminology / concept MongoDB terminology / concept explanation / description databasedatabase database tablecollection database table / collection rowdocument data record row / document columnfield data field / domain indexindex index table joins table join, MongoDB does not support primary keyprimary key primary key, MongoDB automatically sets the _ id field as the primary key MongoDB basic operation

Log in, log out

# Local login (default instance port number:-- port=27017, may not be written) > mongo# login remote host instance > mongo-- host 192.168.1.2-- port=27017 # exit MongoDB > exit

Database operation

# create a database (if the database does not exist, otherwise switch to the specified database) > use school# view all databases > show dbs# delete school database > use school > db.dropDatabase () # Show database operation command > db.help ()

Set

The data of MongoDB is saved in the collection, and all the data stored in the collection is in Binary JSON format, referred to as BSON. BSON is a binary storage format similar to JSON.

# create info collection > db.createcollection ('info') # View collection method 1: > show tabels method 2: > show colletctions# display info collection operation command > db.info.help () document (add, delete, modify, check)

Insert # insert a record > db.info.insert ({"id": 1, "name": "jack", "hobby": ["game", "talk", "sport"]}) # insert a piece of document data into the specified collection > db.collection.insertOne () # insert multiple pieces of document data into the specified collection > db.collection.insertMany () # insert data in batches through a loop > for (var item1transferi db.info.remove ({"id": "1"})

Modify

# modify the name value of the info collection id=1 to "zhangsan" document db.info.update ({"id": "1"}, {$set: {"name": "wzn"}})

Query

# query info collection all documents > db.info.find () # query info collection documents with id 1 > db.info.findOne ({id:1}) # Statistical records > db.info.count () backup, restore database import export export: mongoexport import: mongoimport option:-d specify database name;-c specify collection name;-f specify which columns to export;-o specify file name to export -Q: specifies the filtering criteria for exported data. Specific commands are viewed through-- help. Backup and restore backup: mongodump restore: mongorestore

Options:

1:-h specifies the address of the server where the Mongodb is located or you can specify the port. (example:-h 127.0.0.1 purl 27017)

2:-d: database instance that needs to be backed up.

3:-o: back up the directory where the data is stored. This directory needs to be created in advance.

Copy database

> db.copyDatabase ("db1", "db2") / / copy the database db1 to the db2 clone collection. Clone the info collection of the database db1 to the instance port: 270 mongo-- port 270 security db.runCommand ({"cloneCollection": "db1.info", "from": "localhost:27017"}) MongoDB security management

MongoDB security management mainly includes security access control and user rights distribution of MongoDB.

Limit listening to specific IP and port

# vim / usr/local/mongodb/mongodb1.conf# only binds the internal network card address bind_ip=localhost (ip) # only listens on the specified port port=27017 authorization to start the built-in database users: read, readWrite database management roles: dbAdmin, dbOwner, userAdmin

Superuser role: root

# create superuser root in db1 database with password: 123123 > use db1 > db.createUser ({"user": "root", "pwd": "123123", "roles": ["root"]}) > exit## closes mongodb service mongod-f / usr/local/mongodb/mongodb1.conf-- shutdown## starts mongodb service mongod-f / usr/local/mongodb/mongodb1.conf with authentication parameters-- auth## queries data but does not display content at this time Authorization authentication is required > use db1 > db.auth ("root": "123123") # # of course, in practice, we can modify the configuration file so that when others visit our MongoDB We can specify a user with appropriate permissions to log in to him and operate vim / usr/local/mongodb/mongodb1.confauth=true / / add process management to view the currently running process (process opid for obtaining high resource consumption) > db.currentOp () to terminate the running process with high resource consumption (in parentheses followed by the opid value obtained above) > db.killOp (opid) MongoDB monitoring to view the database Instance status information > db.serverStatus () View current database statistics > db.status () View collection statistics > db.users.status () View collection size > db.users.dataSize ()

In addition, we can view the system monitoring information through the Web interface, only need to modify the configuration file

# vim / usr/local/mongodb/mongodb1.confhttpinterface=true / / add

From the Web page, you can see:

1) all connections to the current MongoDB

2) access statistics of various databases and collections, including: Reads, Writes, Queries, GetMores, Inserts, Updates, Removes

3) the state of the write lock

4) the last few hundred lines of the log file

5) all MongoDB commands

Third-party monitoring tools

We can use the MongoDB plug-in to monitor the MongoDB database through the configuration in Nagios.

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