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

Deploy the MongoDB database on CentOS

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

Share

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

Introduction to MongoDB

MongoDB is a cross-platform, document-oriented database. It can achieve high performance, high availability, and can be easily scaled. Is an open source database system based on distributed file storage. Under high load, adding more nodes can ensure server performance.

The characteristics of MongoDB MongoDB include set-oriented storage, free mode, rich query statements and multi-level index, replication set mechanism, easy horizontal expansion, pluggable storage engine, cross-platform multi-language support and so on. MongoDB is easy to install, provides document-oriented storage function, and is easy to operate. MongoDB provides replication, high availability, and automatic sharding. If the load increases (more storage space and more processing power are needed), it can be distributed among other nodes in the computer network, which is called sharding. Mongo supports a rich set of query expressions. Query instructions use tags in the form of JSON to easily query objects and arrays embedded in a document. MongoDB supports a variety of programming languages: Ruby, Python, Java, C++, PHP, C # and other languages. Application area of MongoDB

MongoDB can provide scalable high-performance data storage solutions for Web applications. MongoDB is mainly suitable for website data, distributed scenarios, data caching and JSON document format storage. It is suitable for Internet applications with large amount of data, high concurrency and weak transactions. its built-in horizontal scaling mechanism provides data processing capacity from millions to billions, which can well meet the data storage requirements of Web2.0 and mobile Internet applications.

Storage structure of MongoDB

The storage structure of MongoDB consists of logical storage and physical storage.

The logical structure of MongoDB is mainly composed of document (document), collection (collection) and database (database). Among them, document is the core concept of MongoDB, it is the smallest unit of MongoDB logical storage, which is equivalent to a row of records in a relational database, multiple documents constitute a collection, a set is equivalent to the concept of tables in a relational database, and multiple collections constitute a database.

The physical storage structure of MongoDB mainly includes data storage and log storage.

Installation and operation control

(1) configure YUM source repository

[root@localhost ~] # vim / etc/yum.repos.d/mongodb.repo [mongodb-org] name=MongoDB Repositorybaseurl= https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc[root@localhost ~] # yum list

(2) install MongoDB

[root@localhost ~] # yum install mongodb-org-y [root@localhost ~] # vim / etc/mongod.conf / / modify the main configuration file / / net: port: 27017 / / listening port / / bindIp: 0.0.0.0 / / listening address / /

1) start the mongodb service and view the port information

[root@localhost ~] # systemctl start mongod.servicev [root@localhost ~] # netstat-anpt | grep 27017tcp 0 0 0.0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 015 * LISTEN 14604/mongod

2) connect and access the database

[root@localhost ~] # / usr/bin/mongo....// omit 2018-07-17T09:54:54.595+0800 I CONTROL [initandlisten] > > db.version () / / View version information / / 3.6.6 > show dbs; / / View Database / / admin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GB > db.getMongo () / / View the connection address of the current database machine / / connection to 127.0.0.1

(3) enable multiple instances

1) when a single server has sufficient resources, multiple real columns can be used to make full use of server resources. The specific steps are as follows:

Cp-p / etc/mongod.conf / etc/mongod2.conf / / copy the main configuration file / / vim / etc/mongod2.conf / / modify the main configuration file / / path: / data/mongodb/mongod2.log / / log location / / dbPath: / data/mongodb/mongo / / data location / / port: 27018 / / port number / / mkdir-p / data/mongodb/ create log storage directory / / Cd / data/mongodb/mkdir mongotouch mongod2.log / / create a log file / / chmod 777 mongod2.log / / elevate privileges / / mongod-f / etc/mongod2.conf / / start the service / / [root@localhost mongo] # netstat-ntap | grep mongod / / View port / / tcp 00 0.0.0.0 chmod 27017 0.0.0.0 * LISTEN 14604/mongod Tcp 0 0 0.0.0 0 27018 0.0.0 0 15 * LISTEN 15552/mongod

2) enter the database with port number 27018

[root@localhost mongo] # mongo-- port 27018 MongoDB shell version v3.6.6connecting to: mongodb://127.0.0.1:27018/MongoDB server version: 3.6.Plus / omit / / > basic operation of MongoDB

(1) add, delete, modify and check the mongoDB database.

> use school / / create if there is no collection, delete / / > db.createCollection ('info') / / create collection / / > show collections / / View collection (table) if you don't create a collection You can also use show tables to view / / info > db.info.insert ({"id": 1, "name": "jack"}) / / insert a data record / / WriteResult ({"nInserted": 1}) > db.info.find () / / view data information / / {"_ id": ObjectId ("5b4d59fb97ae83a938d0e8b3"), "id": 1, "name": "jack"} > db.info.update ({"id": 1}) {$set: {"name": "tom"}) / / change / / WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.info.find () {"_ id": ObjectId ("5b4d59fb97ae83a938d0e8b3"), "id": 1 "name": "tom"} > db.info.drop () / delete collection / / true > db.dropDatabase () / / delete database / / {"dropped": "school", "ok": 1} > routine maintenance of show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBMongoDB

It mainly includes: database backup and recovery, security management and database status monitoring.

1) backup and recovery management

Backup management in MongoDB includes import and export, backup and recovery, replication of databases and cloning of collections.

Export operation [root@bogon ~] # mongoexport-d kgc-c users-o / opt/users.json / / for only one instance / / mongoexport-h 127.0.0.1 kgc 27018-d school-c test-o / opt/test.json / / for two solid / / import operations [root@bogon opt] # mongoimport-d kgc-c user1-- file users.json / / for only one Example / / mongoimport-hinge 127.0.0.1 txt 27018-d school-c txt-- file test.json / / for two real / / conditional operations [root@bogon opt] # mongoexport-d kgc-c user1-Q'{"id": {"$eq": 10}}'- o / opt/top10.json

2) back up and restore and copy the database.

[root@bogon opt] # mkdir / backup/ / create backup directory / / [root@bogon opt] # mongodump-d kgc-o / backup/ # backup [root@bogon backup] # mongorestore-d kgc2-- dir=/backup/kgc # restore > db.copyDatabase ("kgc", "kgc2") / / copy database / /

3) Clone collection

Mongo-port 27018 db.runCommand ({"cloneCollection": "kgc.users", "from": "192.168.235.190 db.runCommand 27017")

4) create an administrative user

> use admin > db.createUser ({"user": "root", "pwd": "123"," roles ": [" root "]}) > db.auth (" root "," 123")

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