mongodb安装、远程访问设置、管理命令、增删改查操作以及GUI

  一般来说,mongodb不应该用于复杂的多表查询、统计报表,主要用于简单的增删改查,例如各种日志和结构易变的管理信息,虽然3.x新版本支持$lookup关联,但毕竟不擅长。

发行版

  https://www.mongodb.com/download-center?jmp=nav下载对应OS的版本,tar -xzvf解压。

  对于最新版本比如3.4,windows 7下可能回报api-ms-win-crt-runtimel1-1-0.dll。

  和其他采用选举的架构一样,生产应该使用奇数个节点,例如3、5个(采用类paxos算法的都是如此),mongodb支持仲裁者角色。

  注:推荐percona for mongodb版本。

其核心程序包括:

  • mongod:核心服务器程序
  • mongos:用于分片集群的控制器和查询路由器
  • mongo:客户端shell

主要的配套工具:

  • mongodump
  • mongorestore
  • bsondump
  • mongoimport
  • mongoexport
  • mongostat  性能监控
  • mongotop   性能监控
  • mongosniff
  • mongoperf 类似于bonnie++

mongodb 指定配置文件

mongod --config /etc/mongo.conf

或:nohup mongod -f mongodb.cnf &


典型配置(单机)

[root@dev logs]# cat /etc/mongo.conf port = 27017

dbpath = /usr/local/mongodb-linux-x86_64-rhel70-3.4.3/db
logpath = /usr/local/mongodb-linux-x86_64-rhel70-3.4.3/logs/mongo.log
smallfiles = true
directoryperdb = true # 初始化后不能修改
storageEngine = wiredTiger # 3.0后增加,默认为mmap
fork = true
logappend = true
journal = true

#auth=true 启用认证机制,匿名用户无法访问mongodb,建议启用,否则是否传输认证信息都可以登录,存在安全风险。

如果客户端设置了用户名密码,但是服务器没有对应的用户,则会认证失败。如下:

Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }

此时要创建用户,并用用户名/密码访问。

bind_ip=0.0.0.0 # 否则远程无法访问。

wiredTigerCacheSizeGB=4 #限制缓存使用内存大小为4GB,这样MongoDB使用的内存就是(4GB-1GB)* 50%=1.5GB,否则Mongodb占用内存会持续上升。

 官方对WiredTiger缓存的解释如下:

  启动完成后,日志中可以看到概述信息以及monogdb自身对服务器配置的一些检测。如下:

2020-04-09T11:02:38.603+0800 I CONTROL  [main] ***** SERVER RESTARTED *****
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] MongoDB starting : pid=2779 port=27017 dbpath=/home/hsetf/common/mongodb/data 64-bit host=hs-10-20-37-72
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] db version v3.4.5
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] git version: 520b8f3092c48d934f0cd78ab5f40fe594f96863
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] modules: none
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] build environment:
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten]     distmod: rhel62
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten]     distarch: x86_64
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2020-04-09T11:02:38.629+0800 I CONTROL  [initandlisten] options: { net: { port: 27017 }, processManagement: { fork: true }, storage: { dbPath: "/home/hsetf/common/mongodb/data" }, systemLog: 
{ destination: "file", logAppend: true, path: "/home/hsetf/common/mongodb/logs" } }
2020-04-09T11:02:38.658+0800 I -        [initandlisten] Detected data files in /home/hsetf/common/mongodb/data created by the 'wiredTiger' storage engine, so setting the active storage engine
 to 'wiredTiger'.
2020-04-09T11:02:38.658+0800 I STORAGE  [initandlisten] 
2020-04-09T11:02:38.658+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-04-09T11:02:38.658+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-04-09T11:02:38.658+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7528M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(
fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2020-04-09T11:02:39.627+0800 I CONTROL  [initandlisten] 
2020-04-09T11:02:39.627+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-04-09T11:02:39.627+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] 
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] 
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] 
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-04-09T11:02:39.628+0800 I CONTROL  [initandlisten] 
2020-04-09T11:02:39.686+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/home/hsetf/common/mongodb/data/diagnostic.data'
2020-04-09T11:02:39.686+0800 I NETWORK  [thread1] waiting for connections on port 27017
2020-04-09T11:05:59.167+0800 I NETWORK  [thread1] connection accepted from 10.20.25.225:32842 #1 (1 connection now open)
2020-04-09T11:05:59.179+0800 I NETWORK  [conn1] received client metadata from 10.20.25.225:32842 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.1" }, os: { type: "Linux",
 name: "Linux", architecture: "amd64", version: "2.6.32-431.el6.x86_64" }, platform: "Java/Oracle Corporation/1.8.0_191-b12" }

 

mongodb停止

> use admin;
switched to db admin
> db.shutdownServer()
server should be down...

导入csv数据

mongoimport --type csv -d test -c postalCodes --headerline --drop pincodes.csv

类SQL分页查询(重点参考https://docs.spring.io/spring-data/mongodb/docs/2.0.14.RELEASE/reference/html/#mongo.query)

增删改查

  mongodb增删改查以及java for mongodb driver、spring mongodb data完全指南参考:

  • https://docs.spring.io/spring-data/mongodb/docs/2.0.14.RELEASE/reference/html/
  • http://mongodb.github.io/mongo-java-driver-reactivestreams/1.11/
  • http://mongodb.github.io/mongo-java-driver/3.10/driver/tutorials/bulk-writes/
  • http://mongodb.github.io/mongo-java-driver/3.10/javadoc/overview-summary.html

use spider;

-- find中第一部分查询条件,第二部分映射

db.stat.find({state:'Gujarat'}/*条件*/,{_id:0, city:1, state:1,pincode:1}/*查询字段*/).sort({city:1}).skip(10).limit(10)

-- 查询name="xxxx" or id=1的记录,返回name字段,以_id降序,返回第11-20行
db.userx.find({$or:[{name:"xxxxx"},{"id":1}]},{"name":1}).sort({"_id":-1}).limit(10).skip(10)

-- 更新列值

db.userx.update({“id”:123},{$set:{"name":"name123"}})

--删除模型上的列

db.userx.update({},{$unset:{"desc":""},"multi":true})  -- 对于multi(默认为false): 如果multi=true,则修改所有符合条件的行,否则只修改第一条符合条件的行。

条件操作符

  支持等于$eq、大于$gt、大于等于$gte、小于$lt、小于等于$lte、in $in、not in $nin、正则表达式(既可以实现或者,也可以满足like),不同于rdbms的操作符,mongodb的操作符使用json模型的key实现,如下:

db.userx.find({"_id":{"$gt":90,"$lt":95}}) -- 操作符写在条件里面

db.userx.find({"name":/(zjh|yidu)*/}) -- 操作符写在条件里面,查询name以zjh或yidu开头的记录

mongod --shutdown

集合重命名

db.log1.renameCollection('log')

查看集合的统计信息

db.log.stats()

查看db的统计信息

db.stats() 

TTL自动删除文档

  自动实现文档的过期,作为缓存使用。

认证与权限管理

  https://www.cnblogs.com/whiteBear/p/12722151.html

查看执行计划(这执行计划输出够繁琐的,跟mysql explain format=json有的一拼)

  db.userx.find({$or:[{name:"xxxxx"},{"id":1}]},{"name":1}).sort({"_id":-1}).limit(10).skip(10).explain("allPlansExecution")

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "tabase.userx",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$or" : [
                {
                    "id" : {
                        "$eq" : 1
                    }
                },
                {
                    "name" : {
                        "$eq" : "xxxxx"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "SUBPLAN",
            "inputStage" : {
                "stage" : "LIMIT",
                "limitAmount" : 10,
                "inputStage" : {
                    "stage" : "PROJECTION",
                    "transformBy" : {
                        "name" : 1
                    },
                    "inputStage" : {
                        "stage" : "SKIP",
                        "skipAmount" : 0,
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$or" : [
                                    {
                                        "id" : {
                                            "$eq" : 1
                                        }
                                    },
                                    {
                                        "name" : {
                                            "$eq" : "xxxxx"
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "_id" : 1
                                },
                                "indexName" : "_id_",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "_id" : [ ]
                                },
                                "isUnique" : true,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "backward",
                                "indexBounds" : {
                                    "_id" : [
                                        "[MaxKey, MinKey]"
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 10,
        "executionTimeMillis" : 35,
        "totalKeysExamined" : 20,
        "totalDocsExamined" : 20,
        "executionStages" : {
            "stage" : "SUBPLAN",
            "nReturned" : 10,
            "executionTimeMillisEstimate" : 0,
            "works" : 21,
            "advanced" : 10,
            "needTime" : 10,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "inputStage" : {
                "stage" : "LIMIT",
                "nReturned" : 10,
                "executionTimeMillisEstimate" : 0,
                "works" : 20,
                "advanced" : 10,
                "needTime" : 10,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "limitAmount" : 10,
                "inputStage" : {
                    "stage" : "PROJECTION",
                    "nReturned" : 10,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 20,
                    "advanced" : 10,
                    "needTime" : 10,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 0,
                    "invalidates" : 0,
                    "transformBy" : {
                        "name" : 1
                    },
                    "inputStage" : {
                        "stage" : "SKIP",
                        "nReturned" : 10,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 20,
                        "advanced" : 10,
                        "needTime" : 10,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 0,
                        "invalidates" : 0,
                        "skipAmount" : 0,
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$or" : [
                                    {
                                        "id" : {
                                            "$eq" : 1
                                        }
                                    },
                                    {
                                        "name" : {
                                            "$eq" : "xxxxx"
                                        }
                                    }
                                ]
                            },
                            "nReturned" : 20,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 20,
                            "advanced" : 20,
                            "needTime" : 0,
                            "needYield" : 0,
                            "saveState" : 0,
                            "restoreState" : 0,
                            "isEOF" : 0,
                            "invalidates" : 0,
                            "docsExamined" : 20,
                            "alreadyHasObj" : 0,
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 20,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 20,
                                "advanced" : 20,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 0,
                                "invalidates" : 0,
                                "keyPattern" : {
                                    "_id" : 1
                                },
                                "indexName" : "_id_",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "_id" : [ ]
                                },
                                "isUnique" : true,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "backward",
                                "indexBounds" : {
                                    "_id" : [
                                        "[MaxKey, MinKey]"
                                    ]
                                },
                                "keysExamined" : 20,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0,
                                "indexDef" : {
                                    "indexName" : "_id_",
                                    "isMultiKey" : false,
                                    "multiKeyPaths" : {
                                        "_id" : [ ]
                                    },
                                    "keyPattern" : {
                                        "_id" : 1
                                    },
                                    "isUnique" : true,
                                    "isSparse" : false,
                                    "isPartial" : false,
                                    "direction" : "backward"
                                }
                            }
                        }
                    }
                }
            }
        },
        "allPlansExecution" : [ ]
    },
    "serverInfo" : {
        "host" : "localhost.localdomain",
        "port" : 27017,
        "version" : "4.0.6-3",
        "gitVersion" : "a64d7bd556ea66a10d1574885deefea32f71f842"
    },
    "ok" : 1
}

 

Hint

  虽然MongoDB查询优化器一般工作的很不错,但是也可以使用hint()来强迫MongoDB使用一个特定的索引。在这种方法下某些情形下会提升性能。一个有索引的collection并且执行一个多字段的查询。传入一个制定的索引,强迫查询使用该索引。

db.userx.find({"name":"user1000", "age":30}).hint({"name":1})

  注意:请确定你已经创建了相应的索引。

  假设在users上有个{"a": 1, "b": 1}的索引,名称是"a_1_b_1",则如下两种方式等价:

db.userx.find({"a": 4, "b": 5, "c": 6}).hint({"a": 1, "b": 1})
db.userx.find({"a": 4, "b": 5, "c": 6}).hint("a_1_b_1")

  也可以强迫查询不适用索引,做表扫描:

db.users.find().hint({"$natural":1})

 

性能分析工具

  • mongotop
  • mongostat
  • 只可惜不像mysql/redis,没有自带的mongobenchmark命令行,推荐使用YCSB。

当前正在执行的语句

  mongotop和mongostat只能看到概况信息,无法看到如show processlist;的sql列表。此时可以使用下列命令:

  db.currentOp().inprog

 

 

kill某操作

db.killOp(<operation id>)

capped collection

  capped collections是性能出色的有着固定大小的集合(定容集合),以LRU(Least Recently Used最近最少使用)规则和插入顺序进行 age-out(老化移出)处理,自动维护集合中对象的插入顺序,在创 建时要预先指定大小。如果空间用完,新添加的对象将会取代集合中最旧的对象。
可以插入及更新,但更新不能超出 collection 的大小,否则更新失败。不允许删除,但是可 以调用 drop() 删除集合中的所有行,但是 drop 后需要显式地重建集合。在 32 位机上,一 个capped collection的最大值约为482.5M,64 位上只受系统文件大小的限制。

mongodb管理客户端

  推荐使用mongobooster,比官方的好用很多,正式版还有语法提示。

索引

  默认情况下,当添加文档到集合中时,默认情况下,mongodb会为_id字段创建索引,这有点像mysql的存储方式。在具有大量文档的集合中,如果没有恰当的索引,其性能是非常低下的,所以通常需要和在数据库中一样创建索引。

  db.userx.ensureIndex({"name":1}) -- 在name字段上创建一个索引

  除了单个字段上的索引,也支持复合索引,如下:

  db.userx.ensureIndex({"name":1,"age":1}) -- 在name和age字段上创建一个复合索引

  索引除了用于搜索外,还可以支持排序操作,和mysql/oracle一样。

  除了普通索引外,还支持唯一索引,只要增加unique:true属性即可

  db.userx.ensureIndex({"name":1},{"unique":true})

删除索引

   db.userx.dropIndex({"name":1})

重构索引

   db.userx.reIndex({"name":1})

 

  从mongodb 2.6开始,mongodb还支持一个查询中使用多个索引,也就是索引连接操作。

  MongoDB的一些限制:参考《MongoDB实战 架构、开发、管理》第11章。

相关异常

     https://blog.csdn.net/anzhen0429/article/details/87716171(oplog损坏)

Mongodb作为文件存储服务器

  前置nginx+mongodb,直接基于nginx模块访问不用经过java,https://www.cnblogs.com/wintersun/p/4622205.html。

参考资料:

  • MongoDB实战 架构、开发、管理
  • MongoDB权威指南
  • MongoDB实战 
  • https://docs.spring.io/spring-data/mongodb/docs/2.0.14.RELEASE/reference/html/
posted @ 2017-04-12 13:54  zhjh256  阅读(510)  评论(0编辑  收藏  举报