linux-MongoDB service

Table of contents

1. Overview of MongoDB

Related concepts

characteristic

Application scenarios

2. Directory structure

3. Default database

admin

local

config

4. Database operations

library operations

Document operations

 5. MongoDB database backup

1. Backup

mongo dump

 mongoexport

 2. Recovery

mongorestore

 mongoimport

Edit


1. Overview of MongoDB

        MongoDB is a nosql database. It has the characteristics of high performance and schema-less document type. It has the richest functions among nosql databases and is most similar to a relational database. The database format is BSON.

Related concepts

        Example: The MongoDB process running on the system is similar to the mysql instance

        Library: Each database is independent, has its own users, permissions, and independent storage collections, similar to the mysql library

        Collection: consists of a set of documents, similar to a mysql table

        Document: The minimum data set of the mongodb database is a data unit composed of multiple key-value pairs in an orderly manner, similar to the data records of mysql

        Primary key: uniquely identifies a row of data

characteristic

        Facial collection document storage, suitable for storing data in json form

        The format is free, the data format is not fixed, and changes in the data structure will not affect program operation.

        Object-oriented SQL query statements basically cover all query statements in relational databases

        With index support, query efficiency is faster

        Supports replication and failover

        Sharded clusters can be used to improve query performance

Application scenarios

        game

        logistics

        social contact

        Internet of things

        Live video

        Big Data

2. Installation

1. Compile and install

2. rpm installation

        Configure mongo source vim /etc/yum.repo.d/mongo.repo

        [mongodb-org-4.2]
        name=MongoDb Repository
        baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
        gpgcheck=1
        enabled=1
        gpgkey=https://www.monyum install -y mongodb-orggodb.org/static/gpg/server-4.2.asc

        Install yum install -y mongodb-org

        Start the database systemctl start mongod.service

2. Directory structure

        rpm -ql mongodb-org-server

                /etc/mongod.conf main configuration file
                                                        port: 27017 listening port number
                                                        bindIp: 127.0.0.1 listening address

                /run/mongodb PID file

                /usr/bin/mongod startup command

                /var/lib/mongo stores data files

                /var/log/mongodb log

        rpm -ql mongodb-org-shell

                /usr/bin/mongo client commands

        rpm -ql mongodb-org-tools

                /usr/bin/mongodump backup database

                /usr/bin/mongoexport backup document

                /usr/bin/mongoimport restore documentation

                /usr/bin/mongorestore restore database

3. Default database

admin

        From a permissions perspective, this is the "root" database. If a user is added to this database, the user automatically inherits all database permissions.
        Certain server-side commands can also only be run from this database, such as listing all databases or shutting down the server.

local

        This database is never replicated and can be used to store any collection limited to a single local server.

config

        When Mongo is used for sharding setup, the config database is used internally to save information about shards.

4. Database operations

library operations

        show database show database

                                     show dbs

        Display the current database db

        Switch database use dbName

        Database creation: Implicit creation
                                                    does not require active creation. Use use newDB to automatically create a non-existing database. It
                                                    will only be saved after the collection is created in the library and viewed using show dbs.

        To delete data, use dbName db.dropdatabase() (it needs to be in the library to delete it)

Document operations

        View the collection show tables

        Create a collection db.createCollection("test ") (collection test)

        Delete collection db. The name of the collection to be deleted.drop()

         Insert data (document) db.collection name.insert({key:value}) insert data in a single row

                                       db.setname.insertmany ( [ {key:value},{key:value},{key:value} ] )

                                                                                                        Insert data into multiple rows

                               The default data type of mongodb numbers is float. To change it to integer type: NumberInt (number)

        Query data db.set name.find({}) full set query

                                db.collection name.find() full collection query

        Conditional query db.info.find({query condition 1key:value,query condition 2,...},{key1:1|0,key2:1|0,...})

                                key:1 is displayed, key:0 is not displayed

                                When there is only one key displayed, key:1 only displays the key and the corresponding value, and key:0 displays all other keyvalues ​​except this key.

        Query how many documents there are in the collection db.collection name.count()

         Delete data db.Collection name.remove({})​​​​​​​​​​​​​​

                                 db.Collection name.remove({key:value})

         Update data db.info.update({_id:"3"}, {$set:{name:"jack"}})

        All documents that meet the conditions are updated db.info.update({'name':'wangwu'},{$set:{password:"tom"}},{multi:true})

        Update the document to increment the value db.info.update({userid:""},{$inc:{likenum:NumberInt()}})

 5. MongoDB database backup

1. Backup

mongo dump

        -h=hostname:port
        -u=<username>
        -p=<password>
        --authenticationDatabase=<dbname>
        -d=<database>
        -o=<path>

         语法        mongodump -d dbName -h hostName:port -u userName -p Password -o backupDirectory

 mongoexport

        -h=hostname:port
        -u=<username>
        -p=<password>
        --authenticationDatabase=<dbname>
        -d=<database>
        -o=<path>

        语法        mongoexport -d dbName -c tableName -h hostName:port -u userName -p Password -o backupDirectory/jsonFile.json

 2. Recovery

mongorestore

        -h=hostname:port
        -u=<username>
        -p=<password>
        --authenticationDatabase=<dbname>
        -d=<database>
        --drop When a collection with the same name exists in the target database, delete and restore it

        语法         mongorestore -h hostName -u username -p password -d dbName -c tableName bakcupDirector/bsonFile

 mongoimport

        ·-h=hostname:port
        -u=<username>
        -p=<password>
        --authenticationDatabase=<dbname>
        -d=<database>
        --drop When a collection with the same name exists in the target database, delete and restore it

        语法        mongoimport -d dbName -c tableName -h hostName:port -u userName -p Password  backupDirectory/jsonFile.json

Guess you like

Origin blog.csdn.net/a872182042/article/details/132281751