当前位置:网站首页>Introduction to mongodb framework zero Foundation (unfinished)

Introduction to mongodb framework zero Foundation (unfinished)

2022-06-09 06:43:00 Menon research monk

Preface

Let's talk about science first NoSQL(not only sql)
In itself NoSQL Non relational databases have ACID( Atomicity 、 Uniformity 、 persistence 、 Isolation, )

Data persistence generally uses a relational database , In memory database use retrieval

MongoDB yes C++ To write , An open source database system based on distributed file storage . Store its data as a document , The key value of the data structure is determined by key、value form . The document format is similar json object

Its main features are :

  • Database for document storage 、json Format
  • Support rich query expressions
  • update() Command to replace document data or specify data fields 、Map/reduce Batch or aggregate data (Map Function call emit(key,value) Traverses all records in the collection , take key And value Pass to Reduce Function to process )
  • Simple installation , Support multi language programming

Most of the explanations in this article are in windows in , Other platforms are more or less applicable

1. Installation configuration

adopt Official website Install and download
Installation is zip Format package , After decompression ( If you install msi Format , Double click to install )
Create a... In its directory data Catalog ,data Create under directory db Catalog : The format is as follows :E:\mongodb-win32-x86_64-windows-5.0.9\data\db

Starting mode
There are two ways to start itself , A pre start and post start ( No matter which startup mode , To perform all bin In the directory mongod command )
By default, the database path will be found during startup /data/db, If the data changes , Need to pass in the command –dbpath Parameter to specify db Path to directory ( It can be an absolute path or a relative path )

Then run this command :E:\mongodb-win32-x86_64-windows-5.0.9\bin\mongod --dbpath E:\mongodb-win32-x86_64-windows-5.0.9\data\db
Replace the path above with your own

If you want to configure global variables , Set this in the environment variable :
Added to the system variable path that will do (E:\mongodb-win32-x86_64-windows-5.0.9\bin
After the cmd Command input :mongo.exe perhaps mongo The terminal appears
 Insert picture description here
The listening port for startup information is mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

2. Off / on configuration

Start the process

Pre start will occupy the entire command port
Post start mode : Start in guard mode , In the execution of an order , add to –fork Parameters ( This parameter needs to be matched –logpath Or is it –sylog Parameters ), The parameters inside the brackets specify the log file , Log files can be anywhere in the system

In itself windows It is not necessary to add fork Parameters , The parameter used is install
But look at my explanation of this article :
MongoDB appear Error parsing command line: unrecognised option ‘–fork‘ Solutions for
The specific command is as follows :

E:\mongodb-win32-x86_64-windows-5.0.9\bin\mongod   --dbpath  E:\mongodb-win32-x86_64-windows-5.0.9\data\db  --logpath E:\mongodb-win32-x86_64-windows-5.0.9\data\log\mongodb.log --install

After post start , Access link :http://127.0.0.1:27017/
 Insert picture description here

Common startup parameters are as follows :

Parameters describe
–quiet Quiet output
–port Specify port number , The default port number is 27017
–bind binding IP, If bound 127.0.0.1, Can only be accessed locally
–logpath Appoint MongoDB Log files , Note that the log file is not the directory
–logappend Add form to add
–fork Daemons start
–auth Enables validation
–config Specify profile , Not a directory
–journal Enable logging options , Data operations are written to

How to start through the configuration file , Some parameters need to be added :
Write... In the configuration file :

depath=
logpath=
port=
bind_ip=
fork=true

After filling in the above parameters , Start through the parameters of the configuration file
The specific command is as follows :

E:\>E:\mongodb-win32-x86_64-windows-5.0.9\bin\mongod --config  The path to the configuration file 

Close phase

You can add... When closing --shutdown Parameters of ( Be limited to linux in )
But in window Adding this parameter does not , You need to give him a service alias , Then close the service
Specific aliases can be added at startup servicename A name is enough
The complete command is as follows :

E:\>E:\mongodb-win32-x86_64-windows-5.0.9\bin\mongod --dbpath  E:\mongodb-win32-x86_64-windows-5.0.9\data\db  --logpath E:\mongodb-win32-x86_64-windows-5.0.9\data\log\mongodb.log --install --serviceName "mongo"

When the service is turned off or on
adopt net stop MongoDB close , adopt net start MongoDB Turn it on

It can also be used when closing kill function ( But it will damage the data )
See my article for details :( reference java The closing thought )

java How to properly close threads and thread pools ( Code practice includes source code analysis )

It can be used even when it is closed MongoDB Function to close :

db.shutdownServer()

db.runCommand("shutdown")

Both of the above functions should be in admin Conduct , And they are all relatively safe closing methods

3. Basic concepts

Concept describe
database database
collection Database collection ( Database like tables )
document file ( A row similar to a database )
field Domain ( Database like fields )
index Indexes
joinsMongoDB I won't support it
primary key Primary key ,MongoDB Automatically put _id Field sets the primary key

The server of the database is mongod, The client is mongo

One mongodb You can build multiple databases ( The default database is in db in , In this database, it is stored in data The catalog of )
The data itself is stored in db Under the table of contents , And this directory will not be created automatically during installation , So you need to create it manually data Directory and in its data Create... In the directory db Catalog .(data Directories can be created anywhere )
MongoDB Can hold multiple independent databases , Each has its own set and permissions , Different databases are also placed in different files .

  • show dbs: To display a list of all data ( Databases with special functions )
    admin:root database , Host all database permissions , You can also execute special server commands ( List all databases or shut down databases )
    local: Store any collection of local single servers
    config: Used for slice setting , Save relevant information about fragmentation
     Insert picture description here
  • use: Switch link to the specified database
  • db: Displays the current database object or collection
     Insert picture description here

Permissions and user mechanisms

Parameters describe
Read Allow users to read
readWrite Allow users to write
dbAdmin Allows users to execute administrative functions in a specified database ( Such as index creation and deletion )
userAdmin Allow the user to system,users A collection of written , Execute the management function in the specified database
clusterAdmin Only in admin Available in the database , Give the user management permission for all partition and replica set related functions
readAnyDatabase Only in admin Available in the database , Allow users to read
readWriteAnyDatabase Only in admin Available in the database , Allow users to write
userAdminAnyDatabase Only in admin Available in the database , That gives the user all the databases useradmin jurisdiction
dbAdminAnyDatabase Only in admin Available in the database , That gives the user all the databases dbadmin jurisdiction
root Only in admin Available in the database

Log in to user After user , Use db.system.users.find() View the information of all users

use admin
db.system.users.find()

By creating json Format creation user

db.createUSer({
	user: "manong",
	pwd: "yanjiuseng",
	roles:[{ role : "  The permissions of the above users " ,db:"admin"}]
})

After the administrator user is created , Need to restart MongoDB, And turn on verification
Execute this command :db.shutdownServer()

Now that the user is enabled , How to set up Authentication
Verification is not enabled by default , Turn on user authentication after adding users , The configuration file needs to be modified , Add... To the configuration file auth=true

After configuration , Need to pass through db.auth("user","pwd") , If returned to 1, Then the verification is successful , Return to 0, The verification fails

The above user has created a super user , If you create a normal user ( You still need to create ordinary users in the super user )

Ordinary users , First create a database , adopt use xx( If not, it will be created automatically )

db.createUSer({
	user: "manong",
	pwd: "yanjiuseng",
	roles:[{ role : " readWrite" ,db:"xx"}]
})

If you insert data into a document
Through db.xx.insert({key : 'value'})

If you want to change the user's role , Need to use db.updateUser() function , This function requires the current user to have userAdminAnyDatabase Or higher

By the following order :
You need to log in to this user ( That is to say admin On )

db.updateUSer(
	"manong",
	{roles:[
	{ role : "  The permissions of the above users " ,db:"admin"},
	{ role : "  The permissions of the above users " ,db:"admin"}]
)

Update user password
There are generally two forms :

db.updateUser()

db.changeUserPassword()

The syntax is as follows :

  • db.updateUser("yonghu" ,{"pwd" : "mima"} )
db.changeUserPassword("yonghu","pwd")

Delete user

Users can be deleted through db.dropUser()

原网站

版权声明
本文为[Menon research monk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090640073818.html