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 installation and common operation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

A brief introduction to MongoDB

1. Introduction

1) MongoDB is written by C++ language, is an open source database system based on distributed file storage, and also belongs to NoSQL.

2) in the case of high load, more nodes can be added to ensure server performance.

3) MongoDB aims to provide scalable high-performance data storage solutions for WEB applications.

4) MongoDB stores the data as a document, and the data structure consists of key-value (key= > value) pairs; MongoDB documents are similar to JSON objects; field values can include other documents, arrays and document arrays.

2. Comparison between MongoDB and relational database.

SQL terminology / concept MongoDB terminology / concept explanation databasedatabase database tablecollection database table / collection rowdocument data record row / document columnfiled data field / domain indexindex index table joins

Table connection. MongoDB does not support primary keyprimary key primary key. MongoDB automatically sets the _ id field as the primary key.

II. MongoDB installation

If you install the epel extension source, you can install the MongoDB2.4 version; but if we want to install version 3.0, the official source only supports 64-bit systems.

1. Set up the yum source

[root@centos ~] # vim / etc/yum.repos.d/mongodb-org-3.0.repo

[mongodb-org-3.0]

Name=MongoDB Repository

Baseurl= http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64

Gpgcheck=0

Enabled=1

Note: if you want to use the yum tool later, change it to enabled=0, that is, restore the original yum source.

2. Installation

[root@centos ~] # yum install-y mongodb-org

Note: download time because it is a foreign source, there may be download interruption.

3. Modify the configuration file

[root@centos ~] # vim / etc/mongod.conf / / remove three comments

Fork: true

PidFilePath: / var/run/mongodb/mongod.pid

BindIp:

4. Modify the kernel and limit parameters

[root@centos ~] # echo never > / sys/kernel/mm/transparent_hugepage/enabled

[root@centos ~] # echo never > / sys/kernel/mm/transparent_hugepage/defrag

[root@centos ~] # vim / etc/security/limits.conf / / add at the end

Mongod soft nofile 64000

Mongod hard nofile 64000

Mongod soft nproc 32000

Mongod hard nproc 32000

5. Start MongoDB

[root@centos ~] # service mongod start

[root@centos ~] # ls / var/lib/mongo/ View the generated file

Journal local.0 local.ns mongod.lock storage.bson _ tmp

Note: if the startup fails, you can start it with the following command. There will be an error message.

[root@centos] # mongod-f / etc/mongod.conf

3. Connect MongoDB

1. Run the following command directly to enter the mongodb shell

[root@centos ~] # mongo

2. If the listening port is not the default 27017, you need to add the-- port option when connecting, for example, the port number is modified to 27018:

[root@centos] # mongo-- port 27018

3. To connect to MongoDB remotely, you need to add-- host option, for example

[root@centos] # mongo-- host 127.0.0.1

4. If authentication is set, you need to add a user name and password when connecting (similar to MySQL), for example

[root@centos ~] # mongo-utpp-p123456 / / username tpp, password 123456

IV. MongoDB user management

1. Mongodb users are for the library. When you set up a user, you need to enter the corresponding library first.

> use test

Switched to db test

2. Create users and roles

> db.createUser ({user: "admin", pwd: "123456", roles: [{role:'dbOwner',db:'userdb'}]})

Successfully added user: {

"user": "admin"

"roles": [

{

"role": "dbOwner"

"db": "userdb"

}

]

}

3. View all users

> use admin / / all users can be viewed only in the admin library.

Switched to db admin

> db.system.users.find () / / you can see the previously created admin user

{"_ id": "test.admin", "user": "admin", "db": "test", "credentials": {"SCRAM-SHA-1": {"iterationCount": 10000, "salt": "Ye/s***eMZ5IsVUaNRHN6A==", "storedKey": "yocgLHpm2MUlL/VmViLZVirJBAE=", "serverKey": "M6Rjhand hxMVbgvaF9TmG4yroomk7Dg3g ="} " "roles": [{"role": "dbOwner", "db": "userdb"}]}

4. View all users under the current library

> use test / / because the admin library has no users, while the test library has

Switched to db test

> show users / / View all users under the current library

{

"_ id": "test.admin"

"user": "admin"

"db": "test"

"roles": [

{

"role": "dbOwner"

"db": "userdb"

}

]

}

5. Delete a user

> db.dropUser ('admin') / / enter the library where the user resides first

True

5. MongoDB library management

1. View the version

> db.version ()

3.0.7

2. Create a library

> use userdb / / switch if the library exists

Switched to db userdb

> db.createCollection ('mycol') / / create the collection mycol. Only after the collection is created can we see the userdb library.

{"ok": 1}

3. Check the library

> show dbs

Admin 0.078GB

Local 0.078GB

Userdb 0.078GB

4. Delete the current library

> db.dropDatabase ()

{"dropped": "userdb", "ok": 1}

> show dbs

Admin 0.078GB

Local 0.078GB

5. View the current library status information

> use admin

Switched to db admin

> db.stats ()

{

"db": "admin"

"collections": 4

"objects": 10

"avgObjSize": 67.2

"dataSize": 672

"storageSize": 28672

"numExtents": 4

"indexes": 3

"indexSize": 24528

"fileSize": 67108864

"nsSizeMB": 16

"extentFreeList": {

"num": 0

"totalSize": 0

}

"dataFileVersion": {

"major": 4

"minor": 22

}

"ok": 1

}

6. View the status of the mongodb server

> db.serverStatus ()

6. MongoDB creates a collection

Syntax: db.cretateCollection (name,option)

> use mydb

Switched to db mydb

> db.createCollection ('myclo', {capped:true,autoIndexID:true,size:6142800,max:10000})

{"ok": 1}

Syntax description:

Name: the name of the collection

Option: parameters used to configure the collection (all optional)

1) capped true/false: if true, enable capping collection. Capping collection is a collection of fixed size. When it reaches its maximum value, it automatically overwrites the oldest entries; if false, you also need to specify size parameters.

2) autoIndexID true/false: if true, the default value for the automatically created index _ id field is false.

3) size: specify the maximum size of the byte cap set; if the cap is true, you also need to specify this field in B units.

4) max: specifies the maximum number of files allowed in the capped collection.

7. MongoDB data management

1. View the collection

> show collections / / you can also use show tables

Myclo

System.indexes

2. Insert document data

> db.Account.insert ({AccountID:1,UserName: "123", password:" 123456 "})

WriteResult ({"nInserted": 1})

> db.Account.insert ({AccountID:2,UserName:'aaa',password:'123456'})

WriteResult ({"nInserted": 1})

Note: if the Account collection does not exist, insert the data directly, and mongodb will automatically create the collection

3. Update document data

> db.Account.update ({AccountID:1}, {"$set": {"Age": 20}})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

4. View all documents

> db.Account.find ()

{"_ id": ObjectId ("5666000a6ee7f810bc23f731"), "AccountID": 1, "UserName": "123"," password ":" 123456 "," Age ": 20}

{"_ id": ObjectId ("566601e16ee7f810bc23f732"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

5. Query documents according to conditions

> db.Account.find ({AccountID:2})

{"_ id": ObjectId ("566601e16ee7f810bc23f732"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

> db.Account.find ({"UserName": "aaa"})

{"_ id": ObjectId ("566601e16ee7f810bc23f732"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

6. Delete documents according to conditions

> db.Account.remove ({AccountID:1})

WriteResult ({"nRemoved": 1})

> db.Account.find ()

{"_ id": ObjectId ("566601e16ee7f810bc23f732"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

7. Delete the collection

> db.Account.drop ()

True

> show collections

Myclo

System.indexes

8. View the collection status

> use mydb / / enter the library first

Switched to db mydb

> db.printCollectionStats ()

Myclo

{

"ns": "mydb.myclo"

"count": 0

"size": 0

"numExtents": 1

"storageSize": 6144000

"lastExtentSize": 6144000

"paddingFactor": 1

"paddingFactorNote": "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only."

"userFlags": 1

"capped": true

"max": 10000

"maxSize": 6144000

"nindexes": 1

"totalIndexSize": 8176

"indexSizes": {

"_ id_": 8176

}

"ok": 1

}

-

System.indexes

{

"ns": "mydb.system.indexes"

"count": 1

"size": 112

"avgObjSize": 112

"numExtents": 1

"storageSize": 8192

"lastExtentSize": 8192

"paddingFactor": 1

"paddingFactorNote": "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only."

"userFlags": 0

"capped": false

"nindexes": 0

"totalIndexSize": 0

"indexSizes": {

}

"ok": 1

}

-

Reference article

Introduction to MongoDB user and role Management: http://bbs.51cto.com/thread-1146654-1.html

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