主頁 > 資料庫 > 分布式檔案存盤資料庫之MongoDB基礎入門

分布式檔案存盤資料庫之MongoDB基礎入門

2020-11-08 15:33:23 資料庫

  一、MongoDB簡介

  MongoDB是用c++語言開發的一款易擴展,易伸縮,高性能,開源的,schema free 的基于檔案的nosql資料庫;所謂nosql是指不僅僅是sql的意思,它擁有部分sql的特性,同時又比sql的性能和擴展要好;所謂schema free指沒有太多的約束,我們知道在關系型資料庫中有很多約束和范式,在MongoDB中就沒有,所以這里的schema free是指這個;mongodb支持海量資料,支持osx,linux,windows,solaris的32位和64位系統,基于GUN VGPL v3.0協議開源;

  以上這張圖反映了MongoDB和其他資料庫的伸縮性和性能以及功能的一些比較;橫軸代表資料庫的功能,縱軸代表可伸縮性和性能;從上圖坐標系可以看到關系型資料sql server/mysql的功能是最強大,但是對于伸縮性和性能來講也是最低的;對于memcached來講它的的伸縮性和性能是最好的,因為它不需與持久化存盤資料,所有資料都在記憶體,同時它的功能也非常簡陋,所以memcached的性能是最好的,但是功能是最簡陋的;對于k/v鍵值資料庫來講,它的性能和伸縮性要略小于memcached,功能要比memcached要略多一點;對于MongoDB來講,它要比memcached的性能和伸縮性要略小,但功能比它強很多,性能同k/v資料庫不相上下,比關系型資料庫的可伸縮性和性能都要高很多,同時對于關系型資料庫的功能也相差不多;從上面的圖可以看到mongodb是性能和擴展性要優于關系型資料,同時功能上也不輸關系型資料庫多少;正是因為mongodb的這些特性,相比其他nosql,MongoDB的使用量是排在第一的;

  MongoDB和關系型資料

  以上這張圖主要對比了mongodb和關系型資料庫中的資料庫、表、行相關概念;在mongdb中的資料庫和關系型資料庫中的資料庫類似,都是用來存放多張表格,但在mongodb中,沒有表的概念,只有collection(對檔案的集合);對于關系型資料庫中的行,就相當于mongodb中的document(檔案);在關系型資料庫中多行資料組成一張表,多張表組成一個庫;而在mongodb中是多個檔案組成一個collection,多個collection組成一個庫;如下圖所示

  MongoDB資料特點

  1、mongodb是基于檔案的關系型資料庫,使用JSON的方式來存盤資料;

  2、schema-free

  3、用c++語言撰寫,支持全面的索引,不支持事務(支持原子事務,或者說當行檔案支持事務),基于記憶體映射檔案,所以支持延遲寫入,正因為這些特點,使得mongodb的性能優越;

  4、支持副本機制,自動分片機制;所謂副本機制指當一個集群中的主節點掛掉以后,它能自動發現,并將其他一個從節點自動選舉提升為主節點;

  5、查詢介面不支持sql,而是用JSON/Javascript表示的靈活的檔案查詢;

  6、支持map/Reduce機制,它能夠將資料map以后再做reduce,支持并行處理;

  7、支持根據位置查找物件;

  二、MongoDB安裝

  1、yum安裝

  配置yum源

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

  查看mogodb相關簡介

[root@node01 ~]# yum info mongodb-org
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Available Packages
Name        : mongodb-org
Arch        : x86_64
Version     : 4.4.1
Release     : 1.el7
Size        : 5.9 k
Repo        : mongodb-org
Summary     : MongoDB open source document-oriented database system (metapackage)
URL         : http://www.mongodb.org
License     : SSPL
Description : 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 ~]# 

  安裝mongodb-org

[root@node01 ~]# yum install mongodb-org -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving 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
……省略部分內容……
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 ~]# 

  編輯組態檔/etc/mongod.conf將其配置為監聽在非127.0.0.1地址

  啟動mongodb

[root@node01 ~]# systemctl start mongod.service 
[root@node01 ~]# ss -tnl
State      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 ~]# 

  提示:啟動后請確保27017埠處于監聽狀態即可;

  2、下載官方二進制包,解壓安裝

  官方下載地址:https://www.mongodb.com/try/download/community;

  提示:選擇要下載的mongodb的版本和運行的系統平臺以及對應包的格式,然后點擊copy link復制下載鏈接地址;

[root@node02 ~]# cd /usr/local/src/
[root@node02 src]# ll
total 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.tgz
Resolving 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 OK
Length: 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]# ll
total 69584
-rw-r--r-- 1 root root 71253687 Sep  8 22:55 mongodb-linux-x86_64-rhel70-4.4.1.tgz
[root@node02 src]# 

  解壓包,并創建軟連接至/usr/local/mongodb

[root@node02 src]# ll
total 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 0
drwxr-xr-x. 2 root root  6 Nov  5  2016 bin
drwxr-xr-x. 2 root root  6 Nov  5  2016 etc
drwxr-xr-x. 2 root root  6 Nov  5  2016 games
drwxr-xr-x. 2 root root  6 Nov  5  2016 include
drwxr-xr-x. 2 root root  6 Nov  5  2016 lib
drwxr-xr-x. 2 root root  6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root  6 Nov  5  2016 libexec
lrwxrwxrwx  1 root root 48 Nov  7 20:03 mongodb -> /usr/local/src/mongodb-linux-x86_64-rhel70-4.4.1
drwxr-xr-x. 2 root root  6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root 49 Sep 15 20:33 share
drwxr-xr-x. 3 root root 92 Nov  7 20:03 src
[root@node02 src]# 

  將/usr/local/mongodb/bin/*軟連接至/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]# mongo
mongo   mongod  mongos  
[root@node02 src]# mongo

  啟動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: 1594
child process started successfully, parent exiting
[root@node02 src]# ss -tnl
State      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 mongod
root       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 --fork
root       1634  0.0  0.0 112812   972 pts/0    R+   20:34   0:00 grep --color=auto mongod
[root@node02 src]# 

  提示:這種方式是用root用戶啟動;

  用mongod用戶啟動

[root@node02 src]# mongod --shutdown --dbpath=/var/lib/mongo/  --logpath=/var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --port=27017 --logappend --fork
killing process with pid: 1594
[root@node02 src]# ss -tnl
State      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 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1697
child process started successfully, parent exiting
[mongod@node02 ~]$ ss -tnl
State      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 mongod
root       1671  0.0  0.1 191836  2340 pts/0    S    20:37   0:00 su - mongod
mongod     1672  0.1  0.1 115940  2652 pts/0    S    20:37   0:00 -bash
mongod     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 --fork
mongod     1738  0.0  0.1 155436  1868 pts/0    R+   20:37   0:00 ps aux
mongod     1739  0.0  0.0 112812   972 pts/0    S+   20:37   0:00 grep --color=auto mongod
[mongod@node02 ~]$ 

  提示:關閉mongodb 使用--shutdown選項即可;

  mongod 相關引數說明

  --dbpath:用于指定存放資料的目錄;

  --logpath:用于指定日志檔案;

  --bind_ip:指定監聽的ip地址,0.0.0.0表示監聽本機所有可用地址;

  --port:用于指定監聽的埠,mongodb默認是監聽27017;

  --logappend:指定日志以追加的方式寫入日志檔案

  --fork:指定運行為后臺;

  --shutdown:指定關閉mongodb;

  提供組態檔方式啟動

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

  使用組態檔啟動mongodb

[mongod@node02 ~]$ mongod -f /etc/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 2050
child process started successfully, parent exiting
[mongod@node02 ~]$ ss -tnl
State      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 ~]$ 

  使用組態檔關閉mongodb

[mongod@node02 ~]$ mongod -f /etc/mongod.conf  --shutdown
killing process with pid: 2050
[mongod@node02 ~]$ ss -tnl
State      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 ~]$ 

  以yaml格式提供組態檔,然后使用其組態檔啟動mongodb

[mongod@node02 ~]$ cat /etc/mongod.conf
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true

processManagement:
  fork: true

net:
  port: 27017
  bindIp: 0.0.0.0

[mongod@node02 ~]$ ss -tnl
State      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: 2166
child process started successfully, parent exiting
[mongod@node02 ~]$ ss -tnl
State      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 ~]$ 

  到此,mongodb的安裝配置啟動就完成了;

  三、MongoDB基礎使用

  使用mongo工具連接mongodb

[root@node02 ~]# mongo --host node01
MongoDB shell version v4.4.1
connecting to: mongodb://node01:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit 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()
---
> 

  查看幫助

> 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 items to display on shell
        exit                         quit the mongo shell
> 

  提示:db.help()是查看有關db相關操作的命令,db.mycoll.help()是查看collection相關操作的命令;

  查看資料庫串列

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
> 

  查看當前庫下的collections

> show collections
> show tables
> 

  提示:這里沒有顯示表示當前庫沒有collection;collection是多個檔案的集合,相當于mysql中的表的概念;

  切換庫

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use local
switched to db local
> show collections
startup_log
> 

  提示:切換庫,再次查看collection,就能看到local庫下有一個startup_log的collection;這里還需要說明一點,在mongodb中資料庫是不需要創建的,直接ues即可,它是采用的延時創建資料庫的方式,只要我們切換到一個資料庫時,在往collection插入資料時,它自動會創建我們use的資料庫和我們指定的collection;如下所示,我們要創建一個student資料庫,直接使用use student命令即可;

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use student
switched to db student
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> 

  提示:我們這里use student成功,但是直接查看資料庫是看不到的,因為student中沒有collection;只有在student庫中有collection時,這個student庫才會創建,我們再使用show dbs命令就能看到student庫;

  查看當前所在資料庫

> db
student
> db.getName()
student
> 

  查看當前資料庫狀態資訊

> db.stats()
{
        "db" : "student",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "totalSize" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "scaleFactor" : 1,
        "fileSize" : 0,
        "fsUsedSize" : 0,
        "fsTotalSize" : 0,
        "ok" : 1
}
> 

  提示:使用db.stats()命令能夠清楚的看到,當前資料庫的名稱,collections的數量,資料庫大小,存盤大小,索引等等資訊;

  查看mongodb的版本號

> db.version()
4.4.1
> 

  查看mongodb服務器狀態

> db.serverStatus()

  查看當前資料庫的連接機器地址

> db.getMongo()
connection to node01:27017
> 

  查看當前資料庫下的所有collection名稱串列

> db.getCollectionNames()
[ ]
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use config
switched to db config
> db.getCollectionNames()
[ "system.sessions" ]
> 

  創建collection

> use student
switched to db student
> db.stats()
{
        "db" : "student",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "totalSize" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "scaleFactor" : 1,
        "fileSize" : 0,
        "fsUsedSize" : 0,
        "fsTotalSize" : 0,
        "ok" : 1
}
> db.student_info.insert({name: "tom",age: "22",gender: "nan"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
student  0.000GB
> show collections
student_info
> db.stats()
{
        "db" : "student",
        "collections" : 1,
        "views" : 0,
        "objects" : 1,
        "avgObjSize" : 64,
        "dataSize" : 64,
        "storageSize" : 20480,
        "indexes" : 1,
        "indexSize" : 20480,
        "totalSize" : 40960,
        "scaleFactor" : 1,
        "fsUsedSize" : 1977794560,
        "fsTotalSize" : 52046860288,
        "ok" : 1
}
> 

  提示:創建collection和創建資料庫類似,它都是采用延時創建的方法,我們只需要往對應的collection中插入檔案資料,它自動會給我們生成對應的collection;這里還需要說明一點在mongodb中,向collection中插入檔案資料時,是任意插入的,這就是mongodb和mysql關系型資料不一樣的點,因為mongodb有schema free的特點,它不受欄位的多少約束;如下

> db.student_info.insert({name: "jerry",age: "26"})
WriteResult({ "nInserted" : 1 })
> db.student_info.insert({name: "xiaoming",age: "24",gender: "M",student_id: "003",score: "84"})
WriteResult({ "nInserted" : 1 })
> db.student_info.insert({name: "xiaohong",age: "34",gender: "nv",student_id: "004",score: "44"})
WriteResult({ "nInserted" : 1 })
> db.student_info.insert({name: "zhangsan",age: "90",gender: "M",student_id: "005",score: "94"})
WriteResult({ "nInserted" : 1 })
> 

  查看collection的狀態資訊

> db.student_info.stats() 

  提示:這里顯示的內容過多,就省略了輸出的資訊;

  查看collection中的檔案資訊

> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : "22", "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
>

  以美觀方式顯示查詢的檔案內容

> db.student_info.find({name:"tom"}).pretty()
{
        "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"),
        "name" : "tom",
        "age" : "22",
        "gender" : "nan"
}
> 

  統計對應collection中有多少條檔案

> db.student_info.find().count()
5
> 

  查詢collection中資料的原始大小

> db.student_info.dataSize()
418
>

  查詢collection中索引資料的原始大小

> db.student_info.totalIndexSize()
36864
>

  查詢collection中索引加資料壓縮存盤之后的大小

> db.student_info.totalSize()
73728
>

  查詢collection中資料壓縮存盤大小

> db.student_info.storageSize()
36864
> 

  find()高級用法

  比較操作:

    $gt,大于;語法格式:find({field: {$gt: value}})

    $gte,大于等于;

    $lt,小于;

    $lte,小于等于;

    $ne,不等于;如果要查詢某個欄位等于某個值,其語法格式為find({field: value})即可;

> db.student_info.find({age: {$gt: "30"}})
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> 

  提示:以上查詢是以age欄位大于30為查詢條件;

    $in,語法格式:find({field:{$in: [value,value,...]}}),表示查詢某個欄位的值在指定串列范圍的檔案

    $nin,查詢指定欄位不在指定串列范圍的檔案,語法同$in相同

> db.student_info.find({age: {$in: ['22','24','26']}})
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : "22", "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
> db.student_info.find({age: {$nin: ['22','24','26']}})
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> 

  提示:我這里之所以數字要加引號,是因為我在創建檔案時加了引號,mongodb把它識別成字串了;正常情況數字不用加引號,是可以直接查詢的;

  組合條件:

    邏輯運算:

    $or:或運算,語法格式:find({$or:[{<expression>},...]})

    $and:與運算

    $not:非運算

    $nor:反運算:回傳不符合指定條件的所有檔案

> db.student_info.find({$or: [{age:{$gt:'22'}},{age:{$lt:'90'}}]})
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : "22", "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> db.student_info.find({$and: [{age:{$gt:'22'}},{age:{$lt:'90'}}]})
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
> 

  元素查詢:根據指定檔案的欄位是否存在進行查詢

    $exists:語法格式find({field:{$exists:<boolean>}})

> db.student_info.find({score:{$exists:true}})
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> db.student_info.find({score:{$exists:0}})
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : "22", "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
> 

  提示:boolean運算式可以是true或者false;也可以是1或者0;

    $type:回傳指定欄位的值的型別為指定型別的檔案,語法格式:find({field:<BSON type>})

> db.student_info.find({score:{$type:1}})
> db.student_info.find({score:{$type:2}})
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> 

  提示:BSON type是對應型別的數字標識,比如double對應1,string對應2;如下圖

  更新操作:語法格式db.mycoll.update(query)

    $set:修改指定欄位的值為新指定的值:語法格式update({field: value},{$set:{field:new_value}})

> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : "22", "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> db.student_info.update({name:"tom"},{$set:{age:25}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "gender" : "M", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> 

    $unset:洗掉指定欄位:語法格式:update({field:value},{$unset:{field1:value,field2:value,...}})

> db.student_info.update({name:"xiaoming"},{$unset:{gender:"nan"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "student_id" : "003", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> db.student_info.update({name:"xiaoming"},{$unset:{gender:"nan",student_id:"003"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> 

    $rename:更改欄位名,語法格式update({field:value},{$rename:{oldname1:newname,oldname2:newname,...}})

> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "gender" : "nv", "student_id" : "004", "score" : "44" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> db.student_info.update({name:"xiaohong"},{$rename:{gender:"xingbie",student_id:"xuehao"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> 

  提示:當前面的條件為{},表示更新檔案全部欄位名稱;

  洗掉操作:語法格式:db.mycoll.remove(<query>,<justone>)

> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
{ "_id" : ObjectId("5fa6c89c391624ebd5fc6bc0"), "name" : "zhangsan", "age" : "90", "gender" : "M", "student_id" : "005", "score" : "94" }
> db.student_info.remove({name:"zhangsan"})
WriteResult({ "nRemoved" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : "26" }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
> db.student_info.update({name:"jerry"},{$set:{age:25}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c308391624ebd5fc6bbc"), "name" : "tom", "age" : 25, "gender" : "nan" }
{ "_id" : ObjectId("5fa6c597391624ebd5fc6bbd"), "name" : "jerry", "age" : 25 }
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
> db.student_info.remove({age:25})
WriteResult({ "nRemoved" : 2 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
> 

  提示:默是洗掉符合指定條件的所有檔案,我們可以在后面寫一個數字,來指定洗掉匹配條件的檔案數量

> db.student_info.find()
{ "_id" : ObjectId("5fa6c84b391624ebd5fc6bbe"), "name" : "xiaoming", "age" : "24", "score" : "84" }
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
> db.student_info.remove({age:{$type:2}},1)
WriteResult({ "nRemoved" : 1 })
> db.student_info.find()
{ "_id" : ObjectId("5fa6c877391624ebd5fc6bbf"), "name" : "xiaohong", "age" : "34", "score" : "44", "xingbie" : "nv", "xuehao" : "004" }
> 

  洗掉collection

> db.student_info.drop()
true
> show collections
> 

  洗掉資料庫

> db.getName()
student
> db.dropDatabase()
{ "dropped" : "student", "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> 

  提示:一般洗掉資料庫要切換到對應的資料庫下執行db.dropDatabase();

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/206674.html

標籤:其他

上一篇:Oracle常用函式及工具

下一篇:mysql 用戶權限命令

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:33:24 more
  • MySQL中binlog備份腳本分享

    關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......

    uj5u.com 2023-04-20 08:28:06 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:27:27 more
  • 快取與資料庫雙寫一致性幾種策略分析

    本文將對幾種快取與資料庫保證資料一致性的使用方式進行分析。為保證高并發性能,以下分析場景不考慮執行的原子性及加鎖等強一致性要求的場景,僅追求最終一致性。 ......

    uj5u.com 2023-04-20 08:26:48 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:26:35 more
  • 云時代,MySQL到ClickHouse資料同步產品對比推薦

    ClickHouse 在執行分析查詢時的速度優勢很好的彌補了MySQL的不足,但是對于很多開發者和DBA來說,如何將MySQL穩定、高效、簡單的同步到 ClickHouse 卻很困難。本文對比了 NineData、MaterializeMySQL(ClickHouse自帶)、Bifrost 三款產品... ......

    uj5u.com 2023-04-20 08:26:29 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:25:13 more
  • Redis 報”OutOfDirectMemoryError“(堆外記憶體溢位)

    Redis 報錯“OutOfDirectMemoryError(堆外記憶體溢位) ”問題如下: 一、報錯資訊: 使用 Redis 的業務介面 ,產生 OutOfDirectMemoryError(堆外記憶體溢位),如圖: 格式化后的報錯資訊: { "timestamp": "2023-04-17 22: ......

    uj5u.com 2023-04-20 08:24:54 more
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:24:03 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:23:11 more