MongoDB Foundation Installation (Actual Deployment)

MongoDB Basic Installation

Overview of MongoDB

(1) MongoDB is a database based on distributed file storage.Written in C++.Designed to provide scalable high performance data storage solutions for WEB applications.

(2) MongoDB is a product between relational and non-relational databases, which is the most rich and similar to relational databases.The data structure it supports is very loose and is a json-like bson format, so it can store more complex data types.Mongo's strongest feature is that it supports a very powerful query language whose syntax is somewhat similar to object-oriented query language, which can perform almost all functions like relational database form queries, and also supports indexing data.

(3) Features:

Collection-oriented storage, easy to store object-type data;

Free mode, support query, support dynamic query;

Supports full indexing, including internal objects;

Supports replication and failure recovery;

Use efficient binary data storage, including large objects such as videos;

Automatically process fragments to support scalability at the cloud computing level;

Support RUBY, PYTHON, JAVA, C++, PHP, C#and other languages;

File storage format is BSON (a JSON extension);

It can be accessed over the network.

MongoDB installation process

1. Preparing for the experiment

Name role address
Centos7-1 Service carrier machine 192.168.142.212

2. Specific Processes

(1) Configure the local YUM source (path location: /etc/yum.repos.d/)

[root@promote yum.repos.d]# vim MongoDB.repo
[mongodb-org]
name=mongodb
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

(2) Install and configure the service ontology

//Install MongoDB
[root@promote yum.repos.d]# yum install mongodb-org -y

//Modify Profile
[root@promote yum.repos.d]# vim /etc/mongod.conf
net:
  port: 27017
//Enable services to be used by any network
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all 

//Open Service
[root@promote yum.repos.d]# systemctl start mongod.service
[root@promote yum.repos.d]# netstat -atnp | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      39695/mongod

//Enter the MongoDB database
[root@promote mongodb]# mongo --port 27017

At this point, the MongoDB service has been installed, but this is not the end of the experiment. The next step is to extend the installation process.

(3) Establishing multiple instances of MongoDB

//On the basis of MongoDB installation
[root@promote etc]# cp -p mongod.conf mongod2.conf
//Modify the configuration file for the second instance
[root@promote yum.repos.d]# vim /etc/mongod2.conf
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/mongod2.log            //Log file path (need to be different from first)
storage:
  dbPath: /data/mongodb/mongo                //Data file path (again)
  journal:
    enabled: true
net:
  port: 27018                     //Set a different port number than before
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all int

//Set up Instance 2 site
[root@promote etc]# Mkdir-p/data/mongodb //Create Instance 2 data storage location
[root@promote etc]# cd /data/mongodb/
[root@promote mongodb]# mkdir mongo
[root@promote mongodb]# touch mongod2.log //Create Instance 2 log file storage location
[root@promote mongodb]# chmod 777 mongod2.log

//Start Instance 2
[root@promote mongodb]# mongod -f /etc//mongod2.conf
[root@promote mongodb]# Mongo--port 27018 //in different instances with different port numbers

MongoDB Basic Operations

In MonoDB

> show dbs/databases #View all databases
 > show collections/tables #View all collections in the database
 > db.info.find #View the contents of the collection
    Example: > db.info.find ({"id": 20}) #View information with ID 20
 > use school; #databases that do not exist are created and entered without a collection being created and not displayed
 > db.createCollection ('info') #Build Collection (Datasheet) info
 > db.info.insert ({"id": 10, "name": zhangsan, "key name": value}) #Insert a value into the collection
 > db.info.insert ({"hobby": ["game", "read", "value"]}) #Add an array of strings to the collection           
> a=db.info.find() #Define alias for search results
 > for (var i=11; i<=100; i++) db.info.insert ({"id": i, "name": "liuliu"+i}) #Loop insert key-value pairs
 > db.info.update ({"id": 20}, {$set:{"name": "kaili"}) #Change data
 ##Change the name in id=20 to kaili
 > db.info.count() #How much data is in the statistics collection
 > db.test.drop() #Delete a collection
 > db.test.remove ({"id": 8}) #Delete a data
 > db.dropDatabase () #Delete the entire database (before entering it)
> db.copyDatabase ("school", "shell") #Copy the school database as a shell database (renamed copy)

MongoDB Import, Export

export

[root@promote mongodb]# mongoexport -d school -c info -o /opt/school.json

'-d'specifies the database
'-c'specified set
'-o'specifies the export path

Import (Import database may not exist)

[root@promote mongodb]# mongoimport -d school -c info --file=/opt/school.json

"--file" specifies the imported json file

Conditional Export

[root@promote mongodb]# mongoexport -d school -c info -q '{"id":{"$eq":20}}' -o /opt/ttt.json

The'-q'condition judgement (greater than: gt; less than: lt; equal to: eq).No greater than or equal to, less than or equal to)

MongoDB backup, recovery

backups

[root@promote mongodb]# mongodump -d school -o /opt/

recovery

[root@promote mongodb]# mongorestore -d school --dir=/opt/school

'--dir'specifies backup directory path

Cloning a collection in an instance

Prerequisite: In the case of multiple instances of MongoDB

[root@promote mongodb]# mongo --port 27018             #Enter the second instance

#Clone the school.info collection from the local LongoDB database on port 27017 to this example
> db.runCommand({"cloneCollection":"school.info","from":"192.168.142.212:27017"})

Create an administrative user

> use admin               #The database exists by default
> db.createUser({"user":"root","pwd":"123123","roles":["root"]})     #Set up administrative users
"user"     #Manage User Names
"pwd"     #Password
"roles":["root"]      #Permission as Administrator
> db.auth("root","123123")                    #Verification

Keywords: Database MongoDB yum JSON

Added by ehhwan on Sun, 22 Dec 2019 22:15:13 +0200