The NoSQL MongoDB

Source: Internet
Author: User
Tags mkdir mongodb server mongo shell

NoSQL (NoSQL = not-only sql), meaning "not just sql."

Refers to the non-relational database, is different from the traditional relational database database management system collectively.

NoSQL is used for storage of hyper-scale data. These types of data stores do not require a fixed pattern and can be scaled horizontally without extra action.


Review the relational database following the acid rules:

Transaction transaction, similar to real-world transactions , has the following four features:

1, A (atomicity) atomicity

Atomicity is easy to understand, that is, all operations in a transaction are either done or not , and the transaction succeeds because all operations in the transaction are successful, and as long as one operation fails, the entire transaction fails and needs to be rolled back.

For example , bank transfer, transfer from a account 100 to B account, divided into two steps: 1) from a account 100 yuan, 2) deposited into the account of 100 to B. The two steps are either completed together, or not completed together, if only the first step, the second step fails, the money will be inexplicably less than 100 yuan.

2, C (consistency) consistency

Consistency is also relatively easy to understand, that is, the database should always be in a consistent state, the operation of the transaction will not change the original consistency of the database constraints.

For example , if an existing integrity constraint a+b=10, if a transaction changes A, then the B must be changed so that the transaction will still satisfy a+b=10, otherwise the transaction fails.

3, I (isolation) Independence

The so-called independence refers to the concurrent transactions do not affect each other, if one transaction to access the data is being modified by another transaction, as long as another transaction is not committed, the data it accesses is not affected by uncommitted transactions.

For example , there is a transaction from a to 100 to a B account, in the case of the transaction is not completed, if at this time B query their own account, is not see the new increase of 100 yuan.

4, D (durability) Persistence

Persistence means that once a transaction commits, its modifications are persisted to the database, even if the outage occurs.


The characteristics of both:

Rdbms

-Highly organized structured data

-Structured Query Language (SQL)

-Data and relationships are stored in a separate table

-Data manipulation language, data definition language

-Strict consistency

-Basic services

Nosql

-More than just SQL

-No declarative query language

-No pre-defined pattern

-key-value pair storage, column storage, document storage, graphics database

-final consistency, not ACID properties

-Unstructured and unpredictable data

-Cap theorem

-High performance, high availability and scalability


Getting Started with MongoDB

First, the installation configuration:

1. Download the installation package and unzip it to the specified installation directory

mkdir/opt/soft/&& Cd/opt/softcurl-o https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.9.tgz mkdir /opt/mongodbtar zxvf MONGODB-LINUX-X86_64-3.4.10.TGZMV mongodb-linux-x86_64-3.4.10/*/opt/mongodb/

2. Add Environment variables:

MongoDB's executable file is located in the bin directory, so you can add it to your path path:

echo "Export path=/opt/mongodb/bin: $PATH" >>/etc/profile && source/etc/profile

3. Create the Conf folder to hold the configuration file, create the Data folder to hold it, create the logs file to hold the files, these directories will not be created automatically during the installation process and will be created manually.

Mkdir/opt/mongodb/confmkdir/opt/mongodb/datamkdir/opt/mongodb/logstouch/opt/mongodb/conf/mongodb.conftouch/opt /mongodb/logs/mongod.log

4. Edit the configuration file

Vi/opt/mongodb/conf/mongodb.conf

DBPath =/opt/mongodb/datalogpath =/opt/mongodb/logs/mongod.logpidfilepath =/opt/mongodb/mongo.pidport = 27017fork = True

5. MongoDB set to system service and set boot boot

Vi/etc/init.d/mongod

#!/bin/sh #description: MongoDB start () {/opt/mongodb/bin/mongod-f/opt/mongodb/conf/mongodb.conf}stop () {/opt/mongo  Db/bin/mongod-f/opt/mongodb/conf/mongodb.conf--shutdown}case "$" in start) start;  stop) stop;;  restart) stop start;; *) echo $ "Usage: $ Start|stop|restart}" Exit 1esac

chmod +x/etc/rc.d/init.d/mongod

6. Start

Service Mongod Start

II. User Authorization and management

1, MongoDB installed after the first entry is not required password, and no user, through the shell command can directly enter, CD to the MongoDB Directory Bin folder, execute the command./mongo

./mongo MongoDB Shell version v3.4.9 connecting to:mongodb://127.0.0.1:27017 MongoDB server version:3.4.9 > Show DBS Admin 0.000GB local 0.000GB > Use test switched to DB Test >

2, add the administrative user (MongoDB no invincible user root, only the user who manages the user useradminanydatabase)

> Use admin switched to DB admin> Db.createuser ({User: "admin", pwd: "123456", Roles:[{role: "Useradminanydatabase", DB: "Admin"}]})

Successfully added User: {

"User": "admin",

"Roles": [

{

"Role": "Useradminanydatabase",

"DB": "admin"

}

]

}


3, after adding Admin user, close MongoDB, and use permission mode to open MongoDB again, here Note do not use kill directly to kill the MongoDB process, (if so, please go to data/ DB directory to delete the Mongo.lock file), you can use Db.shutdownserver () to close it.


4, modify the configuration file, turn on password Authentication login

Vi/opt/mongodb/conf/mongodb.conf

Auth = True #开启
Service Mongod Start

5, enter the MONGO shell, use the admin database and verify, if not verified, can not do any operation

> Use admin> db.auth ("admin", "123456") 1 #认证返回1表示成功 >

6, after verification or do not operate, because the admin only user management rights, the following create users, users follow the library go

> Use mytestswitched to DB mytest> db.createuser ({User: "root", pwd: "123456", roles: [{role: "ReadWrite", DB: "Mytes T "}]})

Successfully added User: {

"User": "Root",

"Roles": [

{

"Role": "ReadWrite",

"DB": "MyTest"

}

]

}


7. database operation with user root login created

[Email protected] ~]# MONGO 127.0.0.1/mytest-uroot-pmongodb shell version V3.4.10enter password:connecting To:mongodb ://127.0.0.1/mytestmongodb server version:3.4.10> dbmytest> use mytestswitched to DB mytest> show COLLECTIONS&G T

The NoSQL MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.