当前位置:网站首页>Introduction to mongodb foundation of distributed document storage database

Introduction to mongodb foundation of distributed document storage database

2020-11-08 11:26:28 Irving the procedural ape

stay mongdb A database in is similar to a database in a relational database , They are used to store multiple forms , But in mongodb in , There is no concept of table , Only collection( A collection of documents ); For rows in a relational database , Equivalent to mongodb Medium document( file ); In a relational database, multiple rows of data form a table , Multiple tables form a library ; And in the mongodb It's multiple documents that make up a single collection, Multiple collection Form a library ;

   One 、MongoDB brief introduction

  MongoDB Yes, it is c++ An easy extension of language development , Flexible , High performance , Open source ,schema free Document based nosql database ; So-called nosql It's not just sql It means , It has parts sql Characteristics of , At the same time, it's more than sql Better performance and scalability ; So-called schema free Not too many constraints , We know that there are many constraints and paradigms in relational databases , stay MongoDB There is no , So here schema free It means this ;mongodb Support massive data , Support osx,linux,windows,solaris Of 32 Bit and 64 Bit system , be based on GUN VGPL v3.0 Open source agreement ;

   The picture above shows MongoDB Comparison with other databases in terms of scalability, performance and functionality ; The horizontal axis represents the function of the database , The vertical axis represents scalability and performance ; You can see relational data from the above coordinate system sql server/mysql Is the most powerful , But it's also the lowest in terms of scalability and performance ; about memcached In terms of its scalability and performance is the best , Because it doesn't need to store data persistently , All the data is in memory , At the same time, its function is very simple , therefore memcached The performance of is the best , But the function is the most rudimentary ; about k/v Key value database , Its performance and scalability are slightly less than memcached, It's more functional than memcached A little bit more ; about MongoDB Speaking of , It's better than memcached Performance and scalability are slightly smaller , But it's a lot more powerful , The performance is the same as k/v Databases are on a par , It's much more scalable and performance than relational databases , At the same time, the function of relational database is not much different ; You can see from the picture above mongodb It's better performance and scalability than relational data , At the same time, the function of relational database is not input ; Precisely because mongodb These characteristics of , Compared to other nosql,MongoDB The number one usage is ;

  MongoDB And relational data

   The above picture mainly compares mongodb And databases in relational databases 、 surface 、 Line related concepts ; stay mongdb A database in is similar to a database in a relational database , They are used to store multiple forms , But in mongodb in , There is no concept of table , Only collection( A collection of documents ); For rows in a relational database , Equivalent to mongodb Medium document( file ); In a relational database, multiple rows of data form a table , Multiple tables form a library ; And in the mongodb It's multiple documents that make up a single collection, Multiple collection Form a library ; As shown in the figure below

  MongoDB Data characteristics

  1、mongodb It's a document based relational database , Use JSON To store data ;

  2、schema-free

  3、 use c++ Language writing , Support comprehensive index , Unsupported transaction ( Support atomic transactions , In other words, when the line documents support transactions ), Based on memory mapped file , So delay write is supported , Because of these characteristics , bring mongodb Excellent performance ;

  4、 Support replica mechanism , Automatic slicing mechanism ; The so-called replica mechanism refers to when the primary node in a cluster fails , It automatically finds out , And the other slave node is automatically elected to the master node ;

  5、 Query interface does not support sql, It's about using JSON/Javascript Express flexible document query ;

  6、 Support map/Reduce Mechanism , It can take data map Do it later reduce, Support parallel processing ;

  7、 Support to find objects by location ;

   Two 、MongoDB install

  1、yum install

   To configure yum Source

[root@node01 ~]# cat /etc/yum.repos.d/mongodb.repo[mongodb-org]name = MongoDB Repositorybaseurl = https://mirrors.aliyun.com/mongodb/yum/redhat/7/mongodb-org/4.4/x86_64/gpgcheck = 1enabled = 1gpgkey = https://www.mongodb.org/static/pgp/server-4.4.asc[root@node01 ~]#

   see mogodb Related introduction

[root@node01 ~]# yum info mongodb-orgLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comAvailable PackagesName  : mongodb-orgArch  : x86_64Version  : 4.4.1Release  : 1.el7Size  : 5.9 kRepo  : mongodb-orgSummary  : MongoDB open source document-oriented database system (metapackage)URL   :  : SSPLDescription : MongoDB is built for scalability, performance and high availability, scaling from single server   : deployments to large, complex multi-site architectures. By leveraging in-memory computing, MongoDB   : provides high performance for both reads and writes. MongoDB’s native replication and automated   : failover enable enterprise-grade reliability and operational flexibility.   :    : MongoDB is an open-source database used by companies of all sizes, across all industries and for a   : wide variety of applications. It is an agile database that allows schemas to change quickly as   : applications evolve, while still providing the functionality developers expect from traditional   : databases, such as secondary indexes, a full query language and strict consistency.   :    : MongoDB has a rich client ecosystem including hadoop integration, officially supported drivers for   : 10 programming languages and environments, as well as 40 drivers supported by the user community.   :    : MongoDB features:   : * JSON Data Model with Dynamic Schemas   : * Auto-Sharding for Horizontal Scalability   : * Built-In Replication for High Availability   : * Rich Secondary Indexes, including geospatial   : * TTL indexes   : * Text Search   : * Aggregation Framework & Native MapReduce   :    : This metapackage will install the mongo shell, import/export tools, other client utilities, server   : software, default configuration, and systemd service files.[root@node01 ~]#

   install mongodb-org

[root@node01 ~]# yum install mongodb-org -yLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comResolving Dependencies--> Running transaction check---> Package mongodb-org.x86_64 0:4.4.1-1.el7 will be installed--> Processing Dependency: mongodb-org-tools = 4.4.1 for package: mongodb-org-4.4.1-1.el7.x86_64--> Processing Dependency: mongodb-org-mongos = 4.4.1 for package: mongodb-org-4.4.1-1.el7.x86_64…… Omit part ……Installed: mongodb-org.x86_64 0:4.4.1-1.el7                    Dependency Installed: cyrus-sasl.x86_64 0:2.1.26-23.el7       cyrus-sasl-gssapi.x86_64 0:2.1.26-23.el7   cyrus-sasl-plain.x86_64 0:2.1.26-23.el7      mongodb-database-tools.x86_64 0:100.2.0-1   mongodb-org-database-tools-extra.x86_64 0:4.4.1-1.el7  mongodb-org-mongos.x86_64 0:4.4.1-1.el7   mongodb-org-server.x86_64 0:4.4.1-1.el7      mongodb-org-shell.x86_64 0:4.4.1-1.el7   mongodb-org-tools.x86_64 0:4.4.1-1.el7      Dependency Updated: cyrus-sasl-lib.x86_64 0:2.1.26-23.el7                   Complete![root@node01 ~]#

   Edit profile /etc/mongod.conf Configure it to listen on non 127.0.0.1 Address

   start-up mongodb

[root@node01 ~]# systemctl start mongod.service [root@node01 ~]# ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       *:27017         *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [root@node01 ~]#

   Tips : Please make sure that 27017 The port is listening ;

  2、 Download the official binary package , Unpack the installation

   Official download address :https://www.mongodb.com/try/download/community;

   Tips : Choose what you want to download mongodb Version and running system platform and the corresponding package format , And then click copy link Copy download link address ;

[root@node02 ~]# cd /usr/local/src/[root@node02 src]# lltotal 0[root@node02 src]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz--2020-11-07 19:59:32-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgzResolving fastdl.mongodb.org (fastdl.mongodb.org)... 13.35.121.42, 13.35.121.49, 13.35.121.114, ...Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|13.35.121.42|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 71253687 (68M) [application/gzip]Saving to: ‘mongodb-linux-x86_64-rhel70-4.4.1.tgz’100%[======================================================================>] 71,253,687 2.00MB/s in 43s 2020-11-07 20:00:19 (1.57 MB/s) - ‘mongodb-linux-x86_64-rhel70-4.4.1.tgz’ saved [71253687/71253687][root@node02 src]# lltotal 69584-rw-r--r-- 1 root root 71253687 Sep 8 22:55 mongodb-linux-x86_64-rhel70-4.4.1.tgz[root@node02 src]#

   Unzip the package , And create a soft link to /usr/local/mongodb

[root@node02 src]# lltotal 69584-rw-r--r-- 1 root root 71253687 Sep 8 22:55 mongodb-linux-x86_64-rhel70-4.4.1.tgz[root@node02 src]# tar xf mongodb-linux-x86_64-rhel70-4.4.1.tgz [root@node02 src]# ln -sv /usr/local/src/mongodb-linux-x86_64-rhel70-4.4.1 /usr/local/mongodb‘/usr/local/mongodb’ -> ‘/usr/local/src/mongodb-linux-x86_64-rhel70-4.4.1’[root@node02 src]# ll /usr/local/total 0drwxr-xr-x. 2 root root 6 Nov 5 2016 bindrwxr-xr-x. 2 root root 6 Nov 5 2016 etcdrwxr-xr-x. 2 root root 6 Nov 5 2016 gamesdrwxr-xr-x. 2 root root 6 Nov 5 2016 includedrwxr-xr-x. 2 root root 6 Nov 5 2016 libdrwxr-xr-x. 2 root root 6 Nov 5 2016 lib64drwxr-xr-x. 2 root root 6 Nov 5 2016 libexeclrwxrwxrwx 1 root root 48 Nov 7 20:03 mongodb -> /usr/local/src/mongodb-linux-x86_64-rhel70-4.4.1drwxr-xr-x. 2 root root 6 Nov 5 2016 sbindrwxr-xr-x. 5 root root 49 Sep 15 20:33 sharedrwxr-xr-x. 3 root root 92 Nov 7 20:03 src[root@node02 src]#

   take /usr/local/mongodb/bin/* Soft connect to /usr/bin/

[root@node02 src]# ln -sv /usr/local/mongodb/bin/* /usr/bin/‘/usr/bin/install_compass’ -> ‘/usr/local/mongodb/bin/install_compass’‘/usr/bin/mongo’ -> ‘/usr/local/mongodb/bin/mongo’‘/usr/bin/mongod’ -> ‘/usr/local/mongodb/bin/mongod’‘/usr/bin/mongos’ -> ‘/usr/local/mongodb/bin/mongos’[root@node02 src]# mongomongo mongod mongos [root@node02 src]# mongo

   start-up mongodb

[root@node02 src]# mkdir -p /var/lib/mongo[root@node02 src]# mkdir -p /var/log/mongodb/[root@node02 src]# groupadd -g 995 mongod[root@node02 src]# useradd -u 996 -g mongod mongod[root@node02 src]# mongod --dbpath=/var/lib/mongo/ --logpath=/var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --port=27017 --logappend --fork about to fork child process, waiting until server is ready for connections.forked process: 1594child process started successfully, parent exiting[root@node02 src]# ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       *:27017         *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [root@node02 src]# ps axu |grep mongodroot  1594 4.8 3.8 1555604 72740 ?  Sl 20:33 0:01 mongod --dbpath=/var/lib/mongo/ --logpath=/var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --port=27017 --logappend --forkroot  1634 0.0 0.0 112812 972 pts/0 R+ 20:34 0:00 grep --color=auto mongod[root@node02 src]#

   Tips : This way is to use root User start ;

   use mongod User start

[root@node02 src]# mongod --shutdown --dbpath=/var/lib/mongo/ --logpath=/var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --port=27017 --logappend --forkkilling process with pid: 1594[root@node02 src]# ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [root@node02 src]# chown -R mongod.mongod /var/log/mongodb/              [root@node02 src]# chown -R mongod.mongod /var/lib/mongo/              [root@node02 src]# su - mongod[mongod@node02 ~]$ mongod --dbpath=/var/lib/mongo/ --logpath=/var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --port=27017 --logappend --forkabout to fork child process, waiting until server is ready for connections.forked process: 1697child process started successfully, parent exiting[mongod@node02 ~]$ ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       *:27017         *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [mongod@node02 ~]$ ps aux |grep mongodroot  1671 0.0 0.1 191836 2340 pts/0 S 20:37 0:00 su - mongodmongod  1672 0.1 0.1 115940 2652 pts/0 S 20:37 0:00 -bashmongod  1697 6.3 3.7 1555916 69368 ?  Sl 20:37 0:00 mongod --dbpath=/var/lib/mongo/ --logpath=/var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --port=27017 --logappend --forkmongod  1738 0.0 0.1 155436 1868 pts/0 R+ 20:37 0:00 ps auxmongod  1739 0.0 0.0 112812 972 pts/0 S+ 20:37 0:00 grep --color=auto mongod[mongod@node02 ~]$

   Tips : close mongodb Use --shutdown Options can be ;

  mongod Description of relevant parameters

  --dbpath: Used to specify the directory where the data is stored ;

  --logpath: Used to specify the log file ;

  --bind_ip: Specify the ip Address ,0.0.0.0 Means to listen to all available addresses of the local computer ;

  --port: Used to specify the port to listen to ,mongodb The default is monitor 27017;

  --logappend: Specifies that the log is appended to the log file

  --fork: Specify the run as background ;

  --shutdown: Specify to close mongodb;

   Provide configuration file to start

[mongod@node02 ~]$ cat /etc/mongod.confdbpath=/var/lib/mongo/logpath=/var/log/mongodb/mongod.loglogappend=1port=27017bind_ip=0.0.0.0fork=1

   Use the configuration file to start mongodb

[mongod@node02 ~]$ mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections.forked process: 2050child process started successfully, parent exiting[mongod@node02 ~]$ ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       *:27017         *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [mongod@node02 ~]$

   Use the configuration file to close mongodb

[mongod@node02 ~]$ mongod -f /etc/mongod.conf --shutdownkilling process with pid: 2050[mongod@node02 ~]$ ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [mongod@node02 ~]$

   With yaml Format provides configuration files , Then use its configuration file to start mongodb

[mongod@node02 ~]$ cat /etc/mongod.confsystemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.logstorage: dbPath: /var/lib/mongo journal: enabled: trueprocessManagement: fork: truenet: port: 27017 bindIp: 0.0.0.0[mongod@node02 ~]$ ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [mongod@node02 ~]$ mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections.forked process: 2166child process started successfully, parent exiting[mongod@node02 ~]$ ss -tnlState  Recv-Q Send-Q   Local Address:Port       Peer Address:Port    LISTEN  0  128       *:22          *:*     LISTEN  0  100     127.0.0.1:25          *:*     LISTEN  0  128       *:27017         *:*     LISTEN  0  128       :::22          :::*     LISTEN  0  100      ::1:25          :::*     [mongod@node02 ~]$

   Here we are ,mongodb The installation configuration of is started ;

   3、 ... and 、MongoDB Based on using

   Use mongo Tool connection mongodb

[root@node02 ~]# mongo --host node01MongoDB shell version v4.4.1connecting to: mongodb://node01:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit session: session { "id" : UUID("ecf71c30-5113-4492-bcae-46f2d781ae59") }MongoDB server version: 4.4.1---The server generated these startup warnings when booting:   2020-11-07T20:18:51.467+08:00: ***** SERVER RESTARTED *****  2020-11-07T20:18:52.481+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted  2020-11-07T20:18:52.481+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'  2020-11-07T20:18:52.481+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'------  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()--->

   view help

> help  db.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 i.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢