Directing a profile

(Reprinted from the article)

Mongo is not Mango, but comes from Humongous, huge; Enormous).

MongoDB is a NoSQL database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications. For more information on what NoSQL is, read “What is NoSQL?”

MongoDB is a product between relational database and non-relational database, non-relational database has the most rich function, the most like relational database.

MongoDB uses Binary JSON (BSON) objects for storage. Similar to key/value pairs in JSON format, field values can contain other documents, arrays, and document arrays. The supported query language is powerful in that its syntax is somewhat similar to that of an object-oriented query language, and it can perform almost all of the functions of a single table query in a relational database, and it also supports indexing of data.

Directing a history

When 10Gen Software was founded in 2007 by Dwight Merriman, Eliot Horowitz and Kevin Ryan, the company’s goal was to enter the cloud computing industry and provide cloud computing services to enterprises. When developing a cloud computing product, they plan to develop a database-like component that provides storage services for the cloud computing product. It was the era of relational databases, and they felt that traditional relational databases could not meet their requirements. They wanted a data storage product that programmers could use without knowing SQL.

After searching the Internet, they couldn’t find anything that satisfied them, whether it was open source or closed source products. Since they couldn’t find anything, they could develop their own products. Anyway, they have the technical strength, and 10Gen’s founders are all from Google. This is their second venture.

There’s a reason 10Gen doesn’t use relational databases. They got burned when they were at DoubleClick. DoubleClick is an online advertising company, the service of many well-known companies in the United States, the company provides about 400000 AD, but in terms of scalability and agility often meet with difficulties, so they had to often develop and use many custom data storage to solve the shortage of the existing relational database, it makes them very distressed.

So they decided to develop a database product to solve their Problems with DoubleClick and provide storage for their cloud computing products.

  • MongoDB was first developed in 2007 by a new York-based company called MongoDB10genOrganization development, now known asMongoDB Inc.
  • In 2009, after nearly two years of development, 10Gen developed the prototype of MongoDB, opened source and officially named it MongoDB, and established an open source community to operate MongoDB through the community.
  • MongoDB 1.0 was released in February 2009, providing most of the basic query functionality.
  • In December 2009, MongoDB 1.2 was released, introducing map-Reduce and supporting large-scale data processing.
  • MongoDB’s first real product was MongoDB 1.4, released in March 2010.
  • MongoDB 1.6 was released in August 2010, introducing major features such as sharding for horizontal scaling, replica sets with automatic failover capability, and IPv6 support.
  • On May 23, 2012, MongoDB 2.1 was released. The release features a new architecture and a number of enhancements.
  • On June 06, 2012, MongoDB 2.0.6 was released, a distributed document database.
  • MongoDB 2.2 was released in August 2012, introducing an aggregation pipeline that combines multiple data processing steps into a chain of operations.
  • In 2013, MongoDB launched its first commercial version, MongoDB Enterprise Advanced.
  • MongoDB 2.4.3, released on April 23, 2013, includes some performance optimizations, feature enhancements, and bug fixes.
  • On August 20, 2013, MongoDB 2.4.6 was released, again featuring performance optimizations, feature enhancements and bug fixes.
  • MongoDB 3.0 was released in March 2015, including a new WiredTiger storage engine, pluggable storage engine API, 50 replica set restrictions, and security improvements. Version 3.2 was released later that year, supporting document validation, partial indexing, and some major aggregation enhancements.
  • In 2016, MongoDB launched Atlas service, MongoDB Atlas, and cooperated with public cloud service providers (Google, Microsoft Azure). This year, MongoDB was exposed to a very serious security gate incident. Hackers deleted data through the default listening address 0.0.0.0 of MongoDB, and extorted through this vulnerability. Data can be restored by paying 0.2 to 0.5 bitcoin.
  • In October 2017, MongoDB was successfully listed in New York Stock Exchange through IPO on the occasion of its 10th anniversary. At $24, the company was valued at $1.6 billion and raised $1.92.
  • MongoDB 3.6 was released in November 2017, providing better support for multi-set join queries, change flows, and document validation using JSON schemas.
  • MongoDB 4.0 was released in June 2018, which received a lot of attention and provided cross-document transaction capabilities. This is an important milestone and MongoDB is ready for high data integrity requirements.
  • On March 18, 2019, Forrester named MongoDB NoSQL Leader.
  • MongoDB 4.2 was released in October 2019 and began supporting distributed transactions.
  • As of October 2020, MongoDB’s community edition is 4.4.1, with scalability and performance enhancements, reduced replication latency, improved availability and fault tolerance, improved query capability and ease of use, and functional updates to the MongoDB cloud platform. MongoDB has gradually changed from a vendor focusing on database services to a vendor providing data platform services.

By 2020, MongoDB had been downloaded 110 million times worldwide. MongoDB currently has more than 2,000 employees and over 18,000 paying customers, many of whom use both MongoDB Atlas and MongoDB Enterprise Edition. Most large companies still use community edition for some of their internal scenarios. The MongoDB Community edition is still open source and is similar to the enterprise MongoDB edition except for some key features.

MongoDB support language

MongoDB vs. relational database terminology

SQL Terminology Concepts Terms and Concepts of MongoDB
Database (database) Database (database)
The table (table). A collection
The row (line) Document or BSON Document
The column (column) Field = field
Index = index Index = index
Table joins Embedded Documents and linking
Specify any unique column or column combination as primary key. primary keyIn MongoDB, the primary key isautomatically set to the _idField. (In MongoDB, the primary key is automatically set to_idField)
aggregation (e.g. group by) MongoDB provides three ways to perform aggregation: The aggregation pipeline, the map-reduce function, and single purpose aggregation methods.

MongoDB data type

The data type describe
String A string. Data types commonly used to store data. In MongoDB, utF-8 encoded strings are valid.
Integer Integer value. Used to store values. Depending on the server you use, it can be 32-bit or 64-bit.
Boolean Boolean value. Used to store Boolean values (true/false).
Double A double precision floating point value. Used to store floating point values.
Min/Max keys Compare a value to the lowest and highest values of a BSON (binary JSON) element.
Arrays Use to store an array or list or multiple values as a key.
Timestamp The time stamp. Record the time when the document was modified or added.
Object For inline documents.
Null Used to create null values.
Symbol Symbols. This data type is basically the same as a string type, except that it is generally used in languages that use special symbolic types.
Date Date time. Store the current date or time in UNIX time format. You can specify your own Date and time by creating a Date object and passing in the year, month and day information.
Object ID Object ID. The ID used to create the document.
Binary Data Binary data. Used to store binary data.
Code Code type. Used to store JavaScript code in documents.
Regular expression Regular expression type. Used to store regular expressions.

MongoDB download and installation

download

Select MongoDB Community Server Community Edition and select the corresponding version according to your system. I use the CentOS version. MongoDB is available only in RedHat.

CentOS is short for Community ENTerprise Operating System, also known as Community ENTerprise Operating System. It is a distribution of Linux Operating System.

CentOS is not a new Linux distribution. It is a clone of Red Hat Enterprise Linux (hereafter referred to as RHEL), a product distributed by the Red Hat family. RHEL is a Linux distribution used by many enterprises that requires payment from Red Hat for services, technical support, and version upgrades. CentOS can build a Linux operating system like RHEL, but it does not pay any product or service fees to Red Hat, nor does it receive paid technical support or upgrade services.

Verify that this version of the software supports your operating system.

The installation

Upload the resources to /usr/local/src, decompress the resources to /usr/local, and name the resources mongodb.

#Create the mongodb directory
mkdir -p /usr/local/mongodb
#Decompress mongodb to the specified directoryTar -zxvf /usr/local/src-mongodb-linux-x86_64-rhel70-4.4.1. TGZ -c /usr/local/#Rename the decompressed directory to mongodbMv/usr/local/mongo - Linux - x86_64 - rhel70-4.4.1 / / usr/local/mongoCopy the code

Create a data/log directory

Create a folder for storing data and logs and modify its permissions to add read and write permissions.

#Create a directory for storing data
mkdir -p /usr/local/mongodb/data/db
#Create a directory for storing logs
mkdir -p /usr/local/mongodb/logs
#Create a logging file
touch /usr/local/mongodb/logs/mongodb.log
Copy the code

Start the mongo

The front desk to start the

The default startup mode of MongoDB is foreground. Foreground startup is when MongoDB starts the process and occupies the current terminal window.

#Switch to the specified directory
cd /usr/local/mongodb/
#The front desk to start the
bin/mongod --dbpath /usr/local/mongodb/data/db/ --logpath /usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --bind_ip 0.0.0.0
Copy the code
  • --dbpath: Specifies the directory for storing data files
  • --logpath: Specifies a log file, not a directory
  • --logappend: Logs are appended
  • --port: Specifies the port number. The default value is 27017
  • --bind_ipIf you are bound to 127.0.0.1, you can access the IP address only from the local host. The default IP address is the local host

The background to start

The so-called background startup is to start MongoDB as a daemon. Add –fork to the command.

#The background to start
bin/mongod --dbpath /usr/local/mongodb/data/db/ --logpath /usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --bind_ip 0.0.0.0 --fork
Copy the code

The command startup mode is not suitable for management. After all, parameters need to be configured each time you enter a command. We can configure the startup parameters through the configuration file, and then start the service by specifying the configuration file, so that it is more convenient to manage MongoDB.

The configuration file

Add a mongodb. Conf configuration file to the bin directory.

# data file storage directory dbpath = / usr/local/mongo/data/db # log file directory contains the logpath = / usr/local/mongo/logs/mongo. # log records in the form of additional log Bind_ip = 0.0.0.0 # Daemon enabled, i.e. run fork = true in the backgroundCopy the code

Start the

#Switch to the specified directory
cd /usr/local/mongodb/
#Start the service by specifying a configuration file
bin/mongod -f bin/mongodb.conf
Copy the code

Client access

You can access the MongoDB server through mongo in the bin directory.

The command is: bin/mongo –host host address (default 127.0.0.1) –port Port (default 27017)

[root @ localhost mongo] # bin/mongo mongo shell version v4.4.1 connecting to: mongo: / / 127.0.0.1:27017 /? compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("2bf54fad-83bc-444c-8bee-166a224445b8") } MongoDB server version: 4.4.1 -- The server generated these startup warnings when booting: 2020-10-21T10:47:44.855+08:00: SERVER RESTARTED * * * * * * * * * * the T10:2020-10-21 47:47. 024 + 08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted The T10:2020-10-21 47:47. 024 + 08:00: You are running this process as the root user, which is not recommended 2020-10-21T10:47:47.024+08:00: / sys/kernel/mm/transparent_hugepage/enabled is' always'. We suggest setting it to 2020-10-21 T10: 'never' 47:47. 024 + 08:00: / sys/kernel/mm/transparent_hugepage/defrag is' always'. We suggest setting it to 2020-10-21 T10: 'never' 47:47. 024 + 08:00: Soft Rlimits too Low 2020-10-21T10:47:47.024+08:00: currentValue: 1024 2020-10-21T10:47:47.024+08:00: recommendedMinimum: 64000 --- --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() ---> 
Copy the code

Help Command.

> helpdb.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show  users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.mycoll.find() list objects in collection mycoll db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shellCopy the code

Db.version () Displays the version information.

> db.version()
4.4.1
Copy the code

Show DBS Displays all databases.

> show dbsAdmin 0.000GB config 0.000GB local 0.000GBCopy the code

Here is a simple access test through the client first, then will list in detail the client operation MongoDB database, collection, documents, indexes, built-in functions and other related operations.

Close the mongo

Foreground start and close

Press Ctrl + C to close it.

Background startup and shutdown

Use the –shutdown parameter to turn it off.

#Disable the command startup mode
bin/mongod --dbpath /usr/local/mongodb/data/db/ --logpath /usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --bind_ip 0.0.0.0 --fork --shutdown
#Disable the configuration file startup mode
bin/mongod -f bin/mongodb.conf --shutdown
Copy the code

Disable the kill command.

Using the kill -9 command to forcibly stop a process is not recommended.

#View information about mongodb processes
ps -ef | grep mongodb
# kill-9 Forcibly shut down
kill -9 pid
Copy the code

MongoDB shutdown

After connecting to the MongoDB service, switch to the admin database and use the related functions to shut down the service.

#Connect the mongo
bin/mongo
#Switching the Admin Database
use admin
#Execute the following functions (choose 1 from 2) to shut down the serviceThe shutdownServer (db). RunCommand (" shutdown ")Copy the code

The environment variable

Every time you operate MongoDB, you need to enter a specific directory, for example, start the service, connect the client, etc. Can you operate in any directory? The answer is yes, simply add the MongoDB related directory to the system environment variable.

Edit the system environment variable file using vim /etc/profile and add the following content.

#Adding environment variables
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
Copy the code

Then reload the system environment variables through source /etc/profile. This allows you to operate MongoDB directly in any directory on the system.

This article covered some of the basics of MongoDB and taught you how to download and install MongoDB in a Linux environment. We’ll start with security issues, take a look at some of MongoDB’s worst experiences and lessons from not encrypting, and teach you a wave of practical solutions.

Make it. The answer is yes, simply add the MongoDB related directory to the system environment variable.

Edit the system environment variable file using vim /etc/profile and add the following content.

#Adding environment variables
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
Copy the code

Then reload the system environment variables through source /etc/profile. This allows you to operate MongoDB directly in any directory on the system.

This article covered some of the basics of MongoDB and taught you how to download and install MongoDB in a Linux environment. We’ll start with security issues, take a look at some of MongoDB’s worst experiences and lessons from not encrypting, and teach you a wave of practical solutions. (Reprinted from the article)