资讯专栏INFORMATION COLUMN

使用mongo shell远程连接数据库

roundstones / 2885人阅读

摘要:序本文主要介绍一下如何使用连接远程数据库这个里头的就包含了后面不传参数则会在命令行提示输入查看空间占用所有数据的总大小所有数据占的磁盘大小以为单位以为单位查看集合查询

本文主要介绍一下如何使用mongo shell连接远程数据库

install
curl -o mongodb-linux-x86_64-3.2.4.tgz https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.4.tgz
tar -xvf mongodb-linux-x86_64-3.2.4.tgz
这个里头的bin就包含了mongo
shell
mongo mydb --username user1  --host 192.168.99.100 --port 27017 --password
--password后面不传参数,则会在命令行提示输入
查看空间占用
mydb:PRIMARY> db.stats()
{
    "db" : "mydb",
    "collections" : 7,
    "objects" : 5762090,
    "avgObjSize" : 437.28749533589377,
    "dataSize" : 2519689904,  //所有数据的总大小
    "storageSize" : 1816985600,  //所有数据占的磁盘大小
    "numExtents" : 0,
    "indexes" : 10,
    "indexSize" : 84381696,
    "ok" : 1
}
db.stats(1073741824);以G为单位
db.stats(1024)以kb为单位
查看集合
db.getCollectionNames()
查询
mydb:PRIMARY> db.mylog.find({"user_id":"12345"}).count()
help
mydb:PRIMARY> db.help()
DB methods:
    db.adminCommand(nameOrDocument) - switches to "admin" db, and runs command [ just calls db.runCommand(...) ]
    db.auth(username, password)
    db.cloneDatabase(fromhost)
    db.commandHelp(name) returns the help for the command
    db.copyDatabase(fromdb, todb, fromhost)
    db.createCollection(name, { size : ..., capped : ..., max : ... } )
    db.createUser(userDocument)
    db.currentOp() displays currently executing operations in the db
    db.dropDatabase()
    db.eval() - deprecated
    db.fsyncLock() flush data to disk and lock server for backups
    db.fsyncUnlock() unlocks server following a db.fsyncLock()
    db.getCollection(cname) same as db["cname"] or db.cname
    db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db"s collections
    db.getCollectionNames()
    db.getLastError() - just returns the err msg string
    db.getLastErrorObj() - return full status object
    db.getLogComponents()
    db.getMongo() get the server connection object
    db.getMongo().setSlaveOk() allow queries on a replication slave server
    db.getName()
    db.getPrevError()
    db.getProfilingLevel() - deprecated
    db.getProfilingStatus() - returns if profiling is on and slow threshold
    db.getReplicationInfo()
    db.getSiblingDB(name) get the db at the same server as this one
    db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
    db.hostInfo() get details about the server"s host
    db.isMaster() check replica primary status
    db.killOp(opid) kills the current operation in the db
    db.listCommands() lists all the db commands
    db.loadServerScripts() loads all the scripts in db.system.js
    db.logout()
    db.printCollectionStats()
    db.printReplicationInfo()
    db.printShardingStatus()
    db.printSlaveReplicationInfo()
    db.dropUser(username)
    db.repairDatabase()
    db.resetError()
    db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
    db.serverStatus()
    db.setLogLevel(level,)
    db.setProfilingLevel(level,) 0=off 1=slow 2=all
    db.setWriteConcern(  ) - sets the write concern for writes to the db
    db.unsetWriteConcern(  ) - unsets the write concern for writes to the db
    db.setVerboseShell(flag) display extra information in shell output
    db.shutdownServer()
    db.stats()
    db.version() current version of the server
doc

mongo Shell Quick Reference

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19155.html

相关文章

  • ubuntu16.04 安装mongodb并远程连接

    摘要:重新启动具有访问控制的实例连接并验证或者先连接后验证开启远程连接修改以下代码节选阿里云创建安全组,开启端口 环境声明ubuntu 16.04mongodb 3.6.4 1、mongodb安装--官网教程官网链接 https://docs.mongodb.com/mast... 1、导入公钥 sudo apt-key adv --keyserver hkp://keyserver.ubu...

    xiaodao 评论0 收藏0
  • mongoDB安装与操作

    摘要:一下安装数据库进入官网,选择相应的版本下载,安装时默认勾选安装服务和修改一些数据库配置,如已有数据库连接工具,可取消默认勾选的安装。 一、Win下安装数据库 进入mongo官网,选择相应的msi版本下载,安装时默认勾选安装MongoD服务和修改一些数据库配置,如已有数据库连接工具Robo 3T,可取消默认勾选的compass安装。 若启动服务报mp错误,可修改binmongod.cfg...

    zorpan 评论0 收藏0
  • 【工具】MongoDB 与可视化工具 adminMongo 的安装、启动与连接

    MongoDB 下载、安装与启动 下载网址:https://www.mongodb.com/downl... showImg(https://segmentfault.com/img/bVbef93?w=2290&h=1328); 下载完成后解压,把解压后的文件夹重命名一个短些的名字,比如 mongodb启动终端,进入 MongoDB 目录下(我的文件路径是 /Users/mobike/Docum...

    Chiclaim 评论0 收藏0
  • MongoDB 资源、库、工具、应用程序精选列表中文版

    摘要:推荐阅读资源库工具应用程序精选列表中文版有哪些鲜为人知,但是很有意思的网站一份攻城狮笔记每天搜集上优秀的项目一些有趣的民间故事超好用的谷歌浏览器油猴插件合集目录资源文档文章图书会谈教程更多库工具管理数据部署桌面发展监控应用资源文档介绍文档教 推荐阅读 MongoDB 资源、库、工具、应用程序精选列表中文版 有哪些鲜为人知,但是很有意思的网站? 一份攻城狮笔记 每天搜集 Github ...

    e10101 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<