mongodb installation and Application

1. Install the database

If necessary, update the yum package management first. Take the CentOS system as an example below:

$ yum -y update
1. Install Mongodb

View the current system version

$ cat /etc/redhat-release

open https://repo.mongodb.org/yum/... , select the mongo version that suits your system, and then edit the Mongodb installation source. Here's a 3.6 example:

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

Edit content (replace the following 3.6 words with your own mongo version):

[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

Installation:

$ yum install mongodb-org
2. Modify Mongo configuration

The mongo configuration file path: /etc/mongod.conf, but if run directly with the mongod command (run without service), if the configuration file is not specified with -f, mongo uses the internal configuration, and dbpath is stored under / data/db by default

$ vi /etc/mongod.conf

#------------------------------------------------------------------------

# Modifiable database storage location
storage:
  dbPath: /var/lib/mongodb

# To support remote connections, change the default 127.0.0.1 to:
net:
  bindIp: 0.0.0.0

# If database privilege authentication is required, turn on the following configuration:
security:
    authorization: enabled

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

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

2. Running databases

1. Startup
$ service mongod start  # Automatically use configuration/etc/mongod.conf

# Or manually specify profile startup:
$ sudo mongod -f /etc/mongod.conf --fork # --fork Background Run

# If run with privilege authentication
$ sudo mongod -f /etc/mongod.conf --auth --fork

If a timeout error occurs during startup, check to see if the pid in the service is the same as the pid in the config, and if it is inconsistent, modify it:

$ sudo vi /usr/lib/systemd/system/mongod.service # Modify pid
$ systemctl daemon-reload # Reload service service service
$ service mongod start
2. Restart
$ service mongod restart
3. Close
$ service mongod stop

Or close manually

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

Start randomly after default installation, no setup required

$ chkconfig mongod on

3. Operating databases

1. Data Management

1.) Database operations

# Enter mongo
$ mongo

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

# Show all databases
> show dbs;

# Switch/Create Database
> use xxx;

# Clone database from specified host
> db.cloneDatabase('Host');

# Copy library A data from specified host to library B
> db.copyDatabase('A library', 'B library', 'Host');  

# 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) / Record Operations

# Show all collections
> db.getCollectionNames();

# Create a collection
> db.createCollection('users', {size: 1024, max: 1000}); # Maximum 1M, 1000

# Show the first record
> db.users.findOne();

# Show all records under the collection
> db.users.find();

# adding record
> db.users.save({name: 'wang', age: 8});

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

# Modify the record (the last two parameters: the first indicates that one is not found, the new one is created, and the second indicates that more than one is updated)
db.users.update({name: 'wang'}, {$set: {sex: 'male'}}, false, true)

# Delete Record
db.users.remove({age: 8}); 
2. User Management

1.) User type

Common user types are:

read/readWrite: Read and write to the specified database
userAdminAnyDatabase: User management permissions for all databases, permissions to assign roles and users, but no read and write permissions, authorized in admin Library
readWriteAnyDatabase: Read and write permissions for all databases, authorized in admin Library
root: super account, super privileges, authorized in admin Library

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 library, it can be abbreviated:
> db.createUser({user:'User name',pwd:'Password',roles:['role']});

# Append User Rights
> db.grantRolesToUser('User name',[{role:'role',db:'library'}]);

# Modify user permissions
> db.updateUser('User name',{roles:['Role 1','Role 2']});

# Update Password
> db.changeUserPassword('User name','Password');
# or
> db.updateUser('User name',{pwd:'Password'});

# delete user
> db.dropUser('User name');

3.) Manage user creation

Users such as root, *AnyDatabase role user, cluster cluster cluster, etc. can only be created under admin Library

> 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 and write accounts in any library
> db.createUser({user:'User name',pwd:'Password',roles:['readWriteAnyDatabase']};

# Normal Read-Write Users (can be created under the admin library, but must also be authenticated in the admin library)
> db.createUser({user:'User name',pwd:'Password',roles:[{role:'readWrite',db:'Business Library'}]});

4.) General User Creation

mongoDB's permissions follow the libraries, under which libraries users create them, which libraries need to auth. If the authentication libraries and read-write target libraries are identical, the authSource parameter may be omitted when connecting (see Section 4: Connecting databases)

> use xxx;
> db.createUser({user:'User name',pwd:'Password',roles:['readWrite']);

If SCRAM-SHA-256 requires undigested passwords are prompted for errors, mechanisms need to be added as follows:

> db.createUser({user:'User name',pwd:'Password',roles:['readWrite'],mechanisms: ['SCRAM-SHA-1']});

4. Connecting databases

1. shell connection:

1.) Normal login

If mongoDB does not turn on authentication mode, all users will have the same privileges as root and do anything.

$ mongo

# Customize host and port
$ mongo --host Host --port port

2.) Authenticated login

mongoDB's permissions follow the libraries, and auth validation is required on which libraries ordinary users create new authorizations for

$ mongo --host Host --port port -u 'User name' -p 'Password' --authenticationDatabase 'Library for Identity Authentication'

Permissions for the root and *AnyDatabase roles can only be authenticated in admin databases, such as:

$ mongo -u root -p 123456 --authenticationDatabase admin

You can also enter the database and authorize:

$ mongo
> use Authentication database;
> db.auth('User name', 'Password');
2. URI connection:

1.) Uncertified database

mongo://Host: port/database name

2.) Authentication database

a.) Ordinary data users

If the current user-authenticated database is the same as the database to be operated on, connect as follows:

mongo://User name: Password@Host: Port/Database name

If different, add authSource, for example: authenticated in admin library, role db is xxx

mongo://User name: Password@Host: Port/xxx?authSource=admin

b.) root and all database users

If the connecting user is root, *AnyDatabase role, cluster cluster cluster's authentication library are admin

mongo://User name: Password@Host: Port/Database name? authSource=admin

c.) Authentication mechanism

mongoDB supports a variety of authentication mechanisms, commonly used are'MONGODB-CR'and'SCRAM-SHA-1'. The official recommendation is'SCRAM-SHA-1', which does not need to be included in the URI at this time, but if it is a'MONGODB-CR' type, it needs to be added to the URI.

mongo://Username: password@host: port/database? authMechanism=authentication mechanism&authSource=library used for authentication

Keywords: Database MongoDB yum sudo

Added by Rianna on Wed, 15 May 2019 00:05:10 +0300