mgo

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2023 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	DbAdmin    = "admin"
	DbLocal    = "local"
	MaxMembers = 50

	CmdOk = 1

	MongoRoot           = "root"           // 最高权限,暴露给dba
	MongoClusterAdmin   = "clusterAdmin"   // operator内部使用,只有管理权限没有读写权限
	MongoClusterMonitor = "clusterMonitor" // 监控使用

	MongoReadWrite = "readWrite" // 数据库读写权限

	MongoUser     = "MONGO_USER"
	MongoPassword = "MONGO_PASSWORD"
	MongoRole     = "MONGO_ROLE"
	MongoDB       = "MONGO_DB"
)
View Source
const (
	Primary   = "PRIMARY"
	Secondary = "SECONDARY"
	Arbiter   = "ARBITER"
)
View Source
const (
	// %s里不能使用单引号(')
	MongoShellEvalWithAuth = `mongo -u root -p '%s' --eval '%s'`
	MongoShellEvalNoAuth   = `mongo --eval '%s'`

	// success:
	// "ok" : 1
	// fail:
	// {"info":"try querying local.system.replset to see current configuration","ok":0,"errmsg":"already initialized","code":23,"codeName":"AlreadyInitialized"}
	// errmsg\" : \"Our config version of 1 is no larger than the version on 10.29.5.107:30247, which is 1\",\
	RSIntitate = `rs.initiate({_id: "%s", members: %s});`
	// 当多个节点共同初始化时,会生成不一样的replicaSetId
	RSReconfig = `rs.reconfig({_id: "%s", members: %s, force: true });`

	DBServerStatusReplMe = `db.serverStatus().repl.me;`
	// success:
	// "ok" : 1
	// fail:
	// "ok" : 0
	RSStatus             = `rs.status();`
	RSAlreadyInitialized = `AlreadyInitialized`
	RSConfigIncompatible = `NewReplicaSetConfigurationIncompatible`

	OK = `"ok" : 1`

	// success:
	// Successfully added user
	// fail:
	// Error: couldn't add user: there are no users authenticated
	CreateUser = `
db.getSiblingDB("admin").createUser({
    user: "%s",
    pwd: "%s",
    roles: [{role: "root", db: "admin"}]
});
`
	CreateUserSuccess = `Successfully added user`
	// 用户已经创建成功
	CreateUserUnauthorized = `no users authenticated`

	ReconfigUnauthorized = `not authorized on admin to execute command`
	// 在非master节点上执行了创建root用户
	CreateUserNotMaster = `not master`
)

mongo script

Variables

View Source
var (
	ErrCmdNotOk      = errors2.New("command exec not ok")
	ErrAlreadyExists = errors2.New("already exists")
)
View Source
var StaticMemberUtil = new(memberUtil)

Functions

This section is empty.

Types

type Client

type Client struct {
	mongo.Client
}

func Dial

func Dial(addrs []string, user, password string, direct bool) (*Client, error)

func (*Client) AddMembers

func (s *Client) AddMembers(members []Member) error

func (*Client) ChangeUserPassword

func (s *Client) ChangeUserPassword(name, pw string) error

func (*Client) CheckMemberStatus

func (s *Client) CheckMemberStatus() (error, []string, []string)

判断所有member status是否正常

func (*Client) CheckReplSetInit

func (s *Client) CheckReplSetInit() error

判断集群是否正常初始化

func (*Client) CreateUserBySecret

func (s *Client) CreateUserBySecret(usersSecret *corev1.Secret) error

func (*Client) CreateUserBySpec

func (s *Client) CreateUserBySpec(user, pw string, roles primitive.A) error

func (*Client) GetMgoNodeInfo

func (s *Client) GetMgoNodeInfo() (*ServerStatusRepl, error)

获取当前mongo的副本集信息

func (*Client) ReadConfig

func (s *Client) ReadConfig() (*RSConfig, error)

func (*Client) RemoveMembers

func (s *Client) RemoveMembers(members []Member) error

func (*Client) ReplMemberStatus

func (s *Client) ReplMemberStatus() ([]MemberStatus, error)

获取集群Member状态, 在status中显示

func (*Client) RunCommand

func (s *Client) RunCommand(cmd bson.D, pResult interface{}) error

command syntax ref: https://docs.mongodb.com/manual/reference/command/

func (*Client) StepDown

func (s *Client) StepDown() error

func (*Client) WriteConfig

func (s *Client) WriteConfig(cfg *RSConfig) error

func (*Client) WriteConfigWithForce

func (s *Client) WriteConfigWithForce(cfg *RSConfig) error

type DBServerStatusReplResponse

type DBServerStatusReplResponse struct {
	ServerStatusRepl ServerStatusRepl `bson:"repl" json:"repl"`
	OK               int              `bson:"ok" json:"ok"`
}

type Member

type Member struct {
	Tags         ReplsetTags `bson:"tags,omitempty" json:"tags,omitempty"`
	Host         string      `bson:"host" json:"host"`
	ID           int         `bson:"_id" json:"_id"`
	Priority     int         `bson:"priority" json:"priority"`
	SlaveDelay   int64       `bson:"slaveDelay" json:"slaveDelay"`
	Votes        int         `bson:"votes" json:"votes"`
	ArbiterOnly  bool        `bson:"arbiterOnly" json:"arbiterOnly"`
	BuildIndexes bool        `bson:"buildIndexes" json:"buildIndexes"`
	Hidden       bool        `bson:"hidden" json:"hidden"`
}

type MemberStatus

type MemberStatus struct {
	Host           string `bson:"name" json:"name"`
	StateStr       string `bson:"stateStr" json:"stateStr"`
	SyncingTo      string `bson:"syncingTo" json:"syncingTo"`
	SyncSourceHost string `bson:"syncSourceHost" json:"syncSourceHost"`
	ID             int    `bson:"_id" json:"_id"`
	Health         int    `bson:"health" json:"health"`
	State          int    `bson:"state" json:"state"`
}

type OKResponse

type OKResponse struct {
	Errmsg string `bson:"errmsg,omitempty" json:"errmsg,omitempty"`
	OK     int    `bson:"ok" json:"ok"`
}

OKResponse is a standard MongoDB response

type RSConfig

type RSConfig struct {
	ID                                 string   `bson:"_id" json:"_id"`
	Members                            []Member `bson:"members" json:"members"`
	Settings                           Settings `bson:"settings,omitempty" json:"settings,omitempty"`
	Version                            int      `bson:"version" json:"version"`
	ProtocolVersion                    int      `bson:"protocolVersion,omitempty" json:"protocolVersion,omitempty"`
	Configsvr                          bool     `bson:"configsvr,omitempty" json:"configsvr,omitempty"`
	WriteConcernMajorityJournalDefault bool     `bson:"writeConcernMajorityJournalDefault,omitempty" json:"writeConcernMajorityJournalDefault,omitempty"`
}

Member document from 'replSetGetConfig' ref: https://docs.mongodb.com/manual/reference/command/replSetGetConfig/#dbcmd.replSetGetConfig

type RSConfigWrap

type RSConfigWrap struct {
	Config *RSConfig `bson:"config" json:"config"`
	Errmsg string    `bson:"errmsg,omitempty" json:"errmsg,omitempty"`
	OK     int       `bson:"ok" json:"ok"`
}

runCommand resp外层有ok等状态码

type RSStatusResponse

type RSStatusResponse struct {
	Members []MemberStatus `bson:"members" json:"members"`
	OK      int            `bson:"ok" json:"ok"`
}

type ServerStatusRepl

type ServerStatusRepl struct {
	Primary     string `bson:"primary" json:"primary"`
	Me          string `bson:"me" json:"me"`
	IsMaster    bool   `bson:"ismaster" json:"ismaster"`
	Secondary   bool   `bson:"secondary" json:"secondary"`
	ArbiterOnly bool   `bson:"arbiterOnly" json:"arbiterOnly"`
}

type Settings

type Settings struct {
	GetLastErrorModes       map[string]ReplsetTags `bson:"getLastErrorModes,omitempty" json:"getLastErrorModes,omitempty"`
	GetLastErrorDefaults    WriteConcern           `bson:"getLastErrorDefaults,omitempty" json:"getLastErrorDefaults,omitempty"`
	HeartbeatIntervalMillis int64                  `bson:"heartbeatIntervalMillis,omitempty" json:"heartbeatIntervalMillis,omitempty"`
	HeartbeatTimeoutSecs    int                    `bson:"heartbeatTimeoutSecs,omitempty" json:"heartbeatTimeoutSecs,omitempty"`
	ElectionTimeoutMillis   int64                  `bson:"electionTimeoutMillis,omitempty" json:"electionTimeoutMillis,omitempty"`
	CatchUpTimeoutMillis    int64                  `bson:"catchUpTimeoutMillis,omitempty" json:"catchUpTimeoutMillis,omitempty"`
	ReplicaSetID            primitive.ObjectID     `bson:"replicaSetId,omitempty" json:"replicaSetId,omitempty"`
	ChainingAllowed         bool                   `bson:"chainingAllowed,omitempty" json:"chainingAllowed,omitempty"`
}

type WriteConcern

type WriteConcern struct {
	WriteConcern interface{} `bson:"w" json:"w"`
	WriteTimeout int         `bson:"wtimeout" json:"wtimeout"`
	Journal      bool        `bson:"j,omitempty" json:"j,omitempty"`
}

ref: https://docs.mongodb.com/manual/reference/write-concern/

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL