Mongodb installation and operation


I. database installation under win

Enter the Mongo official website and select the corresponding MSI version to download. During installation, the mongod service installation and some database configurations are checked by default. For example, the existing database connection tool Robo 3T can cancel the compass installation checked by default.

If the startup service reports MP error, modify binmongod.cfg and comment the last line MP.

If you need to modify the configuration / service or run it manually after installation, you can refer to the following:

1. Command line operation

You can run it from the command line or by specifying the configuration. For example, install it in D: mongodb.

#Single line configuration
> D:\MongoDB\bin\mongod.exe --dbpath D:\MongoDB\data --logappend

#Specify profile
> D:\MongoDB\bin\mongod.exe --config "D:\MongoDB\bin\mongod.cfg"
2. Service mode operation

By installing and running services, mongodb can run silently in the background.

Install / remove services:

#Add to Windows Service
> D:\MongoDB\bin\mongod.exe --config "D:\MongoDB\bin\mongod.cfg" --install

#Remove service (run CMD as administrator)
> D:\MongoDB\bin\mongod.exe --remove

Start / stop service:

> net start MongoDB
> net stop MongoDB

II. Linux installation database

If necessary, update Yum package management first. Take CentOS system as an example:

$ yum -y update
1. Install mongodb

View current system version

$ cat /etc/redhat-release

Open…, select the Mongo version suitable for your system, and then edit the mongodb installation source. Take 3.6 as an example:

$ sudo vi /etc/yum.repos.d/mongodb-org-3.6.repo

Edit the content (replace the following 3.6 words according to your Mongo version):

name=MongoDB Repository


$ yum install mongodb-org
2. Modify Mongo configuration

Mongo configuration file path / etc / mongod.conf, but if mongod command is used to run directly (not service run), if – F is not used to specify the configuration file, Mongo will use the internal configuration, and dbpath is also stored in / data / DB by default.

$ vi /etc/mongod.conf


#Database storage location can be modified
  dbPath: /var/lib/mongodb

#To support remote connections, change the default to:

#For database permission authentication, open the following configuration:
    authorization: enabled

If the remote connection tool Robo 3T is still unable to connect, add ports in the firewall:

$ sudo /sbin/iptables -I INPUT -p tcp --dport 27017 -j ACCEPT

III. Linux operation database

1. Start up
$service mongod start ා auto use configuration / etc / mongod.conf

#Or manually specify profile startup:
$sudo mongod - F / etc / mongod.conf -- fork -- fork background run

#If running with authority authentication
$ sudo mongod -f /etc/mongod.conf --auth --fork

If the timeout error occurs during startup, you can check whether the PID in service is the same as that in config. If not, modify it:

$sudo VI / usr / lib / SYSTEMd / system / mongod.service - modify PID
$systemctl daemon reload reloads the service
$ service mongod start
2, restart
$ service mongod restart
3, shut down
$ service mongod stop

Or close manually

$ sudo mongod -f /etc/mongod.conf --shutdown
4. Start with the system

After default installation, it will start randomly without setting

$ chkconfig mongod on

IV. operation database

1. Data management

1.) database operation

#Enter Mongo
$ mongo

#Display the current database and locate the test database by default
> db;

#Show all databases
> show dbs;

#Switch / create database
> use xxx;

#Delete database
> db.dropDatabase();

#Repair database
> db.repairDatabase();

#View the linked machine address of the current DB
> db.getMongo(); 

#Exit Mongo
> exit;

2.) set (table) operation

#Show all collection names
> db.getCollectionNames();

#Create collection
>DB. Createcollection ('users', {size: 1024, Max: 1000}); ා maximum 1m, 1000

#Delete collection
> db.users.drop();

3.) document (record) operation

After version 3.2, a single instruction and a batch instruction are added to CRUD operations respectively.

#Query record
>DB. Users. Findone(); (query the first item in the collection)
>DB. Users. Find(); (query all in the collection)

#Query by criteria
> db.users.findOne({age: 8});
> db.users.find({age: 8}); 

#New record
>DB. Users. Insertone ({Name: 'Wang', age: 8}); ා the primary key already has a potential error
>DB. Users. Insertmany ([{}, {}]); 񖓿 add multiple
>DB. Users. Save ({Name: 'Wang', age: 8}); ා if the primary key already exists, it will be replaced.

#Modification record
> db.users.updateOne({name: 'wang'}, {$set: {sex: 'male'}}); 
> db.users.updateMany({name: 'wang'}, {$set: {sex: 'male'}});
#General writing method (the next two parameters: the first indicates that if no one can be found, a new one will be created, and the second indicates that more than one will be updated)
> db.users.update({name: 'wang'}, {$set: {sex: 'male'}}, false, true);

#Delete record
> db.users.deleteOne({age: 8}); 
> db.users.deleteMany({age: 8}); 
#General writing method
>DB. Users. Remove ({age: 8}); ා default multiple
>DB. Users. Remove ({age: 8}, true); ා single
2. User management

1.) user type

For web applications, the most commonly used user types are:

customer type role Managing users Read and write data Identity authorization Library
Database users read/readWrite nothing Designated Library Specify library / Admin
All database read and write permissions readWriteAnyDatabase nothing Arbitrary library admin
User administrator userAdmin Designated Library nothing Specify library / Admin
All database administrators userAdminAnyDatabase Arbitrary library nothing admin
Super account root Arbitrary library Arbitrary library admin

2.) common user instructions

#Show all users
> show users;

#New user
>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: [{role: 'role', DB: 'Library'}]});

#If the current database is the same as the target database, it can be abbreviated as follows:
>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: ['role']});

#Append user rights
>DB. Grantrolestouser ('user name ', [{role:' role ', DB:' Library '}]);

#Modify user rights
>DB. Updateuser ('user name ', {roles: [' role 1 ',' role 2 ']});

#Update password
>DB. Changeuserpassword ('user name ',' password ');
A kind of
>DB. Updateuser ('user name ', {PWD:' password '});

#Delete user
>DB. Dropuser ('user name ');

3.) manage user creation

Root, * anydatabase role user, cluster cluster and other users can only be created under the admin database.

> use admin;

#Super administrator
>DB. CREATEUSER ({user: 'root', PWD: 'password', roles: ['root ']});

#User administrator account
>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: ['useradminanydatabase ']});

#Read write account of any library
>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: ['readwriteanydatabase ']};

#Normal read-write user (can be created under the admin library, but also in the admin library when authenticating)
>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: [{role: 'readwrite', DB: 'Business Library'}]});

4.) common user creation

The permissions of mongodb follow the database. If the database is created under which database, the auth authentication must be performed in which database. If the authentication database is consistent with the read-write target database, the authsource parameter can be omitted when connecting (see section 4: connecting the database).

> use xxx;
>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: ['readwrite ']);

If scram-sha-256 requires unknown passwords error is prompted, you need to add mechanisms as follows:

>DB. CREATEUSER ({user: 'user name', PWD: 'password', roles: ['readwrite ']]]});

V. connecting to the database

1. Shell connection:

1.) general login

If mongodb does not turn on the authentication mode, all users have the same permissions as root and operate arbitrarily.

$ mongo

#Custom host and port
$Mongo -- host host -- port port

2.) authentication login

The permissions of mongodb follow the library. When an ordinary user creates a new authorization in a library, he / she needs to perform auth authentication in that library.

$Mongo -- host host -- port port
>Use authentication database;
>DB. Auth ('username ',' password ');

You can also authorize authentication when you log in:

$Mongo -- host host -- Port Port - U 'user name' - p 'password' -- authenticationdatabase 'database used for authentication'

The permissions of root and * anydatabase roles can only be authenticated in the admin database, such as:

$Mongo -- host host -- Port Port - U root - P 123456 -- authenticationdatabase admin
2. URI connection:

1.) uncertified database

Mongo: // host: port / database name

2.) certification database

A.) general data users

If the database authenticated by the current user is the same as the database to be operated, the connection mode is as follows:

Mongo: // user name: password @ host: port / database name

If it is different, you need to add authsource. For example, if you have authenticated in the admin library, the role DB is XXX.

Mongo: // username: password @ host: port / XXX? Authsource = admin

B.) root and all database users

If the connected user is root, * anydatabase role, and the cluster’s authentication database is admin

Mongo: // username: password @ host: port / database name? Authsource = admin

C.) certification mechanism

Mongodb supports a variety of authentication mechanisms, including ‘mongodb-cr’ and ‘scrum-sha-1’. It is officially recommended that ‘scrum-sha-1’ should not be included in the URI at this time, but ‘mongodb-cr’ should be added to the URI.

Mongo: // user name: password @ host: port / database? Authmechanism = authentication mechanism & authsource = library used for authentication

Vi. backup / restore database

1. Backup database:

It also applies to copy remote database to local

$mongodump -- host host -- port port -- username user -- password "password" -- authenticationdatabase "authorization database" -- DB target database -- out storage directory
2. Restore database:

If the local database is running in the non authentication mode, the recovery to the local database can be simple as follows:

$mongorestore directory

Restore remote authentication database:

$mongorestore -- host host host -- port port -- username user -- password "password" -- authenticationdatabase "authorization library" storage directory

Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]