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

Yum installation MongoDB and database management

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Introduction to MongoDB

MongoDB is written in C++ language and 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 aims to provide scalable, high-performance data storage solutions for WEB applications. MongoDB stores the data as a document, and the data structure consists of key-value (key= > value) pairs. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

The main feature of MongoDB MongoDB is a document storage-oriented database, which is relatively simple to operate and easy to set any attribute index in MongoDB records to achieve faster sorting and create data mirrors locally or over the network, making MongoDB more scalable if the load increases (more storage space and stronger processing power are required). It can be distributed on other nodes in the computer network. This is the so-called fragmented Mongo that supports rich query expressions. Query instructions in the form of JSON tags, you can easily query the object embedded in the document and array MongoDb use the update () command to replace the completed document (data) or some specified data fields Mongodb Map/reduce is mainly used for batch processing and aggregation of data operations Map and Reduce. The Map function calls emit (key,value) to traverse all the records in the collection, passes key and value to the Reduce function for processing, the Map function and the Reduce function are written using Javascript, and can perform MapReduce operations through db.runCommand or mapreduce commands GridFS is a built-in function in MongoDB that can be used to store a large number of small files MongoDB allows scripts to be executed on the server side, you can write a function in Javascript and execute it directly on the server side The definition of the function can also be stored in the server, and MongoDB can be called directly next time to support a variety of programming languages: RUBY,PYTHON,JAVA,C++,PHP,C# and other languages MongoDB install simple MongoDB installation configuration YUM source warehouse cd / etc/yum.repos.d/vim mongodb-org.repo// create yum repository Write the following content [mongodb-org] name=MongoDB Repositorybaseurl= https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.ascyum installation yum install-y mongodb-org configuration file modification

The modification of the configuration file includes path, dbpath, port and bindIp. If it is only used on this machine, it can be used without change, or its listening address (bindIp) can be simply modified to facilitate the use of other addresses.

Turn off firewall and selinuxsystemctl disable firewalld.servicesystemctl stop firewalld.servicesetenforce 0 to open database systemctl start mongod.servicenetstat-anpt | grep mongod

Enter the database mongo / / enter the database db.version () / / check the database version show dbs / / view all libraries db.getMongo () / / view the connection address of the current database machine

Open the multi-instance creation path,dbpath directory mkdir-p / data/mongodb/ * keep the attributes of the original file to copy mkdir / data/mongodb/mongotouch / data/mongodb/mongod2.logchmod 777 / data/mongodb/mongod2.log copy and modify the configuration file configuration file path: / data/mongodb/mongod.logdbPath: / data/mongodb/mongoport: 27018bindIp: 0.0.0.0

Launch a new instance mongod-f / etc/mongod2.conf

Enter the new instance database mongo-- port 27018netstat-anpt | grep mongod

Basic operation of database management database

The creation of a library in mongodb is closely related to tables. To create a library, you only need to use use. When you do not create a collection (table) in the library, the library will not exist. When you create a collection, the library also exists at the same time.

> use python / / if it does not exist, the python library is automatically created. If you do not create a collection, you will delete > db.createCollection ('a') / / create a collection with the name a > db.a.insert ({"name": "zhangsan", "score": 90, "hobby": ["game", "talk", "sport"]}) / / insert data into the collection; use double quotation marks for strings; values are not enclosed in double quotes. String arrays use [] enclosing > db.users.update () / / change data > show collections / / View collections > db.a.drop () / / delete collections > db.dropDatabase () / / delete databases

Example demo > use stady / / use library switched to db stady > db.createCollection ('school') / / create school collection {"ok": 1} > db.school.insert ({"id": 1, "name": "lisi") "score": 90}) / / insert data WriteResult ({"nInserted": 1}) > db.school.find () / / View the data in the collection {"_ id": ObjectId ("5b4843900edf47ef3aa006f3"), "id": 1, "name": "lisi", "score": 90} > for (var item2) I db.school.find () / View collection data {"_ id": ObjectId ("5b4843900edf47ef3aa006f3"), "id": 1, "name": "lisi", "score": 90} {"_ id": ObjectId ("5b4844e70edf47ef3aa006f4"), "id": 2, "name": "tom2"} {"_ id": ObjectId ("5b4844e70edf47ef3aa006f5"), "id": 3 "name": "tom3"} {"_ id": ObjectId ("5b4844e70edf47ef3aa006f6"), "id": 4, "name": "tom4"} {"_ id": ObjectId ("5b4844e70edf47ef3aa006f7"), "id": 5, "name": "tom5"} > db.school.findOne ({"id": 3}) / / View the third data in the collection {"_ id": ObjectId ("5b4844e70edf47ef3aa006f5") "id": 3, "name": "tom3"} > a=db.school.findOne ({"id": 3}) / / View the third record And give it the alias a {"_ id": ObjectId ("5b4844e70edf47ef3aa006f5"), "id": 3 "name": "tom3"} > typeof (a.id) / / View attribute type number / / attribute is numeric > typeof (a.name) / / View attribute type string / / attribute is string > db.school.update ({"id": 3} {$set: {"name": "jack"}) / / modify data WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.school.findOne ({"id": 3}) {"_ id": ObjectId ("5b4844e70edf47ef3aa006f5"), "id": 3 "name": "jack"} > db.school.count () / / Statistics the number of records in the collection 5 > db.createCollection ('tea') / / create a new collection {"ok": 1} > show tables / / View the collection in the library schooltea > db.tea.drop () / / delete the collection True > show tables school / / Collection deleted > show dbs / / View Library admin 0.000GBconfig 0.000GBlocal 0.000GBpython 0.000GBstady 0.000GB > use pythonswitched to db python > db.dropDatabase () / / Delete python database First of all, you must advance the database. If you choose to delete {"dropped": "python", "ok": 1} > show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBstady 0.000GB > the data in the import and export library [root@Nginx mongodb] # mongoexport-d stady-c school-o / opt/school.json / / export the school in the stady library under / opt, and name it school.json Note: the file name must end with .json and operate in linux mode.

Import data into the library [root@Nginx mongodb] # mongoimport-d stady-c school2-- file / opt/school.json

Conditional filter export [root@Nginx mongodb] # mongoexport-d stady-c school-Q'{"id": {"$eq": 3}}'- o / opt/top3.json

Backup database mkdir / backup/ / create backup folder mongodump-d stady-o / backup/

Restore database mongorestore-d chen-- dir=/backup/stady

Copy database db.copyDatabase () / / copy database

# example demonstration

Db.copyDatabase ("stady", "stady2") / / make a copy of the stady library stady2show dbs / / View all libraries show tables / / View all collections in the library

Copy the collection to the new instance demo mongo-port 27018 / / enter the new instance db.runCommand ({"cloneCollection": "stady.school", "from": "172.16.10.27 db.runCommand"})

Process management example demonstrates db.currentOp () db.killOp (1689)

When you think a process takes up a lot of resources, you can end it, and when you finish it, it will reload, which is equivalent to process initialization.

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