MongoDB installation and configuration

Keywords: Database MongoDB

MongoDB database is used in the company's project. Here we introduce the relevant knowledge, installation and configuration of MongoDB.

Introduction to MongoDB

MongoDB is written in C + + language. It is an open source database system based on distributed file storage.

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

MongoDB aims to provide scalable high-performance data storage solutions for WEB applications.

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

Common predicates in MongoDB database:

1. In MongoDB, the database is stored as a file, and the corresponding database is stored in the database directory!

2. In MongoDB, the "table" in the traditional database is called "Collections"!

3. In MongoDB, when storing data to a collection, you can access it directly in JSON format!

4. In MongoDB, the data in the collection is called Documents "Documents"!

MongoDB Download

Download the installation package. There is an executable file of the installation version. You can install it until Next. The installation free version should be extracted to the local computer. Create a new data and logs folder under the extracted directory, a new mongo.config file, and a new db folder under the data file,

Add configuration information to the mongo.conf file as follows:

#Database path
dbpath=D:\worksoftware\mongodb-win32-x86_64-2012plus-4.2.16\data\db 
logpath=D:\worksoftware\mongodb-win32-x86_64-2012plus-4.2.16\logs\mongo.log #Log output file path
#logappend=true #The error log is in append mode
#journal=true #Enable log file, enabled by default
#quiet=true #This option can filter out some useless log information. If you need to debug, please set it to false
port=27017 #The default port number is 27017

Start MongoDB

Open cmd.exe with administrator privileges, enter the bin directory under the MongoDB installation directory, and enter mongod --dbpath D:\worksoftware\mongodb-win32-x86_64-2012plus-4.2.16\data\db

Open the browser and enter localhost:27017 to see the following figure

Using MongoDB as a Windows Service

Similarly, enter the installation directory bin directory, open cmd.exe with administrator privileges, and enter mongod --config D:\worksoftware\mongodb-win32-x86_64-2012plus-4.2.16\mongo.config --install --serviceName "mongodb". Press enter to open the Windows service as follows:

Enter net start mongodb to start the service, mongo to open the MongoDB client, and show dbs to view the database.

Now that MongoDB has been installed, you can also configure the installation directory of MongoDB and add the environment variable path for convenience. You can execute commands anywhere on the computer.

Create administrator account

After the MongoDB database is installed, the default mode is [unauthorized mode] (that is, without any permission verification, directly enter mongo enter in the command window to connect). The default IP and port of MongoDB are: mongodb://127.0.0.1:27017 Or mongodb://localhost:27017 . For the sake of data security, we should all configure the access rights of the database and modify the default( mongodb://127.0.0.1:27017 )Connection binding IP and port number

Enter the mongo command to enter the mongo syntax environment and create an administrator account

> use admin
switched to db admin
> db.createUser({
... user:"admin",
... pwd:'admin',
... roles:["root"]
... })
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
>

After creating the administrator account, close the mongodb service and modify the configuration file

#Database path
dbpath=D:\worksoftware\mongodb-win32-x86_64-2012plus-4.2.16\data\db 
logpath=D:\worksoftware\mongodb-win32-x86_64-2012plus-4.2.16\logs\mongo.log #Log output file path
#logappend=true #The error log is in append mode
#journal=true #Enable log file, enabled by default
#quiet=true #This option can filter out some useless log information. If you need to debug, please set it to false
port=27017 #The default port number is 27017
bind_ip=127.0.0.1
auth=true#Turn on user authentication

After modifying the configuration file, restart the mongodb service and use the command mongodb://admin:admin @Connect to the database using localhost / Admin.

MongoDB database common commands

> use admin//Switch the database and create the database if it does not exist
switched to db admin
> db.createUser({//Create account
... user:"admin",
... pwd:'admin',
... roles:["root"]
... })
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
> show users//View account
{
        "_id" : "admin.admin",
        "userId" : UUID("9417a275-4977-4565-9a17-2470cd3427fd"),
        "user" : "admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
> db.getUsers()//View account
[
        {
                "_id" : "admin.admin",
                "userId" : UUID("9417a275-4977-4565-9a17-2470cd3427fd"),
                "user" : "admin",
                "db" : "admin",
                "roles" : [
                        {
                                "role" : "root",
                                "db" : "admin"
                        }
                ],
                "mechanisms" : [
                        "SCRAM-SHA-1",
                        "SCRAM-SHA-256"
                ]
        }
]
> db.auth('admin',"admin")//Login authentication
1
> db.updateUser("admin",{pwd:"123456"})//Modify account password
> db.dropUser("admin")//Delete account
true
> db//View the current database
admin
> show collections//View all sets (tables) under the current database
system.users
system.version
> db.system.users.find()
> db.system.version.find()//View all json data (documents) in the current data collection
{ "_id" : "featureCompatibilityVersion", "version" : "4.2" }
{ "_id" : "authSchema", "currentVersion" : 5 }
> db.stats()//View information about the current database
{
        "db" : "admin",
        "collections" : 2,
        "views" : 0,
        "objects" : 2,
        "avgObjSize" : 52,
        "dataSize" : 104,
        "storageSize" : 61440,
        "numExtents" : 0,
        "indexes" : 3,
        "indexSize" : 86016,
        "scaleFactor" : 1,
        "fsUsedSize" : 374723637248,
        "fsTotalSize" : 403391377408,
        "ok" : 1
}
> show dbs//View all databases
admin   0.000GB
config  0.000GB
local   0.000GB
>

Posted by mickd on Tue, 19 Oct 2021 11:11:04 -0700