linux mongo 远程连接命令,使用mongo shell远程连接数据库

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

installcurl -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

shellmongo 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()

helpmydb: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,你可以使用MongoDB的Java驱动程序来连接和操作MongoDB数据库。以下是一个简单的示例代码,展示了如何连接MongoDB数据库: 首先,你需要在你的Java项目中添加MongoDB的Java驱动程序依赖。你可以在你的构建工具(如Maven或Gradle)的配置文件中添加以下依赖: Maven: ```xml <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.7</version> </dependency> ``` Gradle: ```groovy implementation 'org.mongodb:mongo-java-driver:3.12.7' ``` 接下来,你可以使用以下代码来连接MongoDB数据库: ```java import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.client.MongoDatabase; public class MongoDBConnector { public static void main(String[] args) { // 设置MongoDB连接信息 String host = "localhost"; // MongoDB主机名或IP地址 int port = 27017; // MongoDB端口 String database = "mydb"; // 数据库名称 String username = "myuser"; // 用户名 String password = "mypassword"; // 密码 // 创建MongoCredential实例 MongoCredential credential = MongoCredential.createCredential(username, database, password.toCharArray()); // 创建MongoClient实例 MongoClient mongoClient = new MongoClient(host, port); // 获取MongoDatabase实例 MongoDatabase mongoDatabase = mongoClient.getDatabase(database); // 打印数据库连接成功信息 System.out.println("成功连接MongoDB数据库"); // 关闭MongoClient连接 mongoClient.close(); } } ``` 在这个示例中,我们使用MongoClient类来连接MongoDB数据库。我们提供了MongoDB的主机名和端口号,以及数据库的名称、用户名和密码。然后,我们使用这些信息创建一个MongoCredential实例,并使用它来验证连接。接下来,我们使用MongoClient实例来获取MongoDatabase实例,可以使用该实例来进行数据库操作。最后,我们打印出连接成功的信息,并关闭MongoClient连接。 请注意,这只是一个简单的示例,实际情况可能会更复杂。你可能需要处理异常、使用连接池等。另外,MongoDB还提供了更高级的功能和API,如集合操作、查询和更新等。你可以查阅MongoDB的官方文档以了解更多详细信息:https://docs.mongodb.com/drivers/java/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值