首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用mongo shell远程连接数据库

使用mongo shell远程连接数据库

作者头像
code4it
发布2018-09-17 15:42:46
3.5K0
发布2018-09-17 15:42:46
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要介绍一下如何使用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,<component>)
    db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
    db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
    db.unsetWriteConcern( <write concern doc> ) - 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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • install
  • shell
  • 查看空间占用
  • 查看集合
  • 查询
  • help
  • doc
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档