MongoDB管理命令与mgo驱动器

huangapple go评论77阅读模式
英文:

MongoDB admin commands with mgo driver

问题

如果有管理员凭据,是否可以通过官方的Go驱动程序(mgo)在Mongo shell之外运行mongo shell命令,例如db.stats()rs.status()db.serverStatus()

英文:

Is it possible, given admin creds, to run mongo shell commands such as db.stats(), rs.status() and db.serverStatus() external to the mongo shell via the official Go driver for MongoDB (mgo)?

答案1

得分: 15

这是完全可能的,但首先你需要记住,你列出的“命令”实际上是shell助手。你需要获取它们所代表的真正命令,然后通过mgoSession.Run方法来运行它们。

有几种方法可以做到这一点,第一种方法是在shell中运行db.listCommands(),找到相应的命令。第二种方法是在不使用括号的情况下运行你想要模拟的助手。例如:

> rs.status
function () { return db._adminCommand("replSetGetStatus"); }

如你所见,助手实际上运行了针对admin数据库的replSetGetStatus命令。类似地,你会发现db.stats()实际上运行了dbstats命令。而db.serverStatus()助手是你列出的三个助手中唯一可以直接运行的。

下面是一个非常简单的示例,展示了如何运行这三个命令。我展示了两种调用形式,一种只传递一个字符串,另一种是传递完整的命令文档。我在一个没有身份验证的测试mongod上运行了这个示例,所以如果你要在启用身份验证的实例上进行测试,你需要自己添加身份验证部分:

package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func main() {
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)
	result := bson.M{}
	if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}}, &result); err != nil {
		panic(err)
	} else {
		fmt.Println(result)
	}
	if err := session.DB("test").Run("dbstats", &result); err != nil {
		panic(err)
	} else {
		fmt.Println(result)
	}
	if err := session.DB("admin").Run("replSetGetStatus", &result); err != nil {
		panic(err)
	} else {
		fmt.Println(result)
	}
}

希望对你有所帮助!

英文:

This is certainly possible, but first you need to bear in mind that the "commands" you have listed are actually shell helpers. You will need to get the real commands that they represent to run them via mgo Session.Run.

There are a couple of ways to do that, the first is to just run db.listCommands() in the shell and find the appropriate one. The second way to do this is to run the helper you wish to emulate without parentheses. For example:

> rs.status
function () { return db._adminCommand("replSetGetStatus"); }

As you can see, what the helper actually does is run the replSetGetStatus command against the admin database. Similarly you will find that db.stats() actually runs the dbstats command. The db.serverStatus() helper is the only one of the three you listed that you can pretty much run as-is.

Here's a very simple example of running all three - I show two forms of the call, one that just passes a string and the more general option that passes in the full command document - I ran this on a test mongod without auth, so you would have to add that piece yourself to test on an auth-enabled instance:

package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func main() {
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)
	result := bson.M{}
	if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}}, &result); err != nil {
		panic(err)
	} else {
		fmt.Println(result)
	}
	if err := session.DB("test").Run("dbstats", &result); err != nil {
		panic(err)
	} else {
		fmt.Println(result)
	}
	if err := session.DB("admin").Run("replSetGetStatus", &result); err != nil {
		panic(err)
	} else {
		fmt.Println(result)
	}
}

答案2

得分: -3

获取统计信息

手动运行服务器状态命令

英文:

get stats

manually running server status commands

huangapple
  • 本文由 发表于 2015年4月10日 02:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/29545784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定