Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb of NoSQL

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

NoSQL (NoSQL = Not Only SQL), which means "not just SQL".

Refers to the non-relational database, is the general name of the database management system which is different from the traditional relational database.

NoSQL is used to store very large-scale data. These types of data stores do not require fixed schemas and can be scaled out without redundant operations.

Recall that relational databases follow 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 the transaction are either completed or not done. The condition for the success of a transaction is that all operations in the transaction are successful. As long as one operation fails, the whole transaction fails and needs to be rolled back.

For example, a bank transfer of 100 yuan from An account to B account is divided into two steps: 1) withdraw 100 yuan from An account; 2) deposit 100 yuan into B account. These two steps are either completed together or not together. If only the first step is completed and the second step fails, the money will be inexplicably less than 100 yuan.

2. C (Consistency) consistency

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

For example, the existing integrity constraint a+b=10, if a transaction changes a, then b must be changed so that the transaction still satisfies a+b=10 after the transaction ends, otherwise the transaction fails.

3. I (Isolation) independence

The so-called independence means that concurrent transactions will not affect each other. If the data accessed by one transaction is being modified by another transaction, as long as another transaction is not committed, the data accessed by it will not be affected by the uncommitted transaction.

For example, there is a transaction that transfers 100 yuan from An account to B account. In the case that the transaction has not been completed, if B inquires about his account at this time, he will not see the new 100 yuan.

4. D (Durability) persistence

Persistence means that once a transaction is committed, its changes will be permanently stored on the database and will not be lost even if there is an outage.

The characteristics of both:

RDBMS

-highly organized structured data

-structured query language (SQL)

-data and relationships are stored in separate tables

-data manipulation language, data definition language

-strict consistency

-basic transaction

NoSQL

-not just SQL.

-No declarative query language

-there are no predefined patterns

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

-final consistency, not ACID attribute

-unstructured and unpredictable data

-CAP theorem

-High performance, high availability and scalability

Getting started with mongodb

1. Installation configuration:

1. Download the installation package and extract 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/

two。 Add environment variables:

The executable file for MongoDB is located in the bin directory, so you can add it to the PATH path:

Echo "export PATH=/opt/mongodb/bin:$PATH" > > / etc/profile & & source / etc/profile

3. Create conf folders to store configuration files, create data folders to store data, and create logs files to store files. These directories are not created automatically during the installation process, but 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 is set to system service and boot is set.

Vi / etc/init.d/mongod

#! / bin/sh # description: mongodb start () {/ opt/mongodb/bin/mongod-f / opt/mongodb/conf/mongodb.conf} stop () {/ opt/mongodb/bin/mongod-f / opt/mongodb/conf/mongodb.conf-- shutdown} case "$1" in start) start; stop) stop;; restart) stop start;; *) echo $"Usage: $0 {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 does not need a password to enter for the first time after installation, and there are no users. You can enter directly through the shell command, cd to the bin folder under the mongodb directory, and 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 administrative users (mongoDB does not have invincible user root, but only user userAdminAnyDatabase for managing users)

> 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 administrative users, close MongoDB and use permission to enable MongoDB again. Here, be careful not to use kill to kill the mongodb process directly. (if you do, delete the mongo.lock file in the data/db directory). You can use db.shutdownServer () to close it.

4. Modify the configuration file and enable password authentication login

Vi / opt/mongodb/conf/mongodb.conf

Auth = true # enable service mongod start

5. Enter mongo shell, use admin database and verify it. Without verification, nothing can be done.

> use admin > db.auth ("admin", "123456") 1 # authentication returns 1 indicates success >

6. After verification, the operation still cannot be done, because admin only has user management rights. Create users below, and users will follow the library.

> use mytestswitched to db mytest > db.createUser ({user: "root", pwd: "123456", roles: [{role: "readWrite", db: "mytest"}]})

Successfully added user: {

"user": "root"

"roles": [

{

"role": "readWrite"

"db": "mytest"

}

]

}

7. Use the created user root login to operate the database

[root@bj-web-001] # 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 dbmytest > show collections >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report