cmd

package
v0.0.0-...-f5295be Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 42 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// Default values
	DefaultInstallDir = "/usr/local"
	DefaultDataDir    = "/data/mysql"
	DefaultMySQLPort  = 3306
	DefaultSSHPort    = 22
	DefaultSSHUser    = "root"
	DefaultMGRPort    = 33061
	DownloadMysqlUrl  = "https://dev.mysql.com/get/Downloads/MySQL-8.0"

	// Timeouts
	SSHTimeout        = 5
	MySQLStartTimeout = 60
	ServiceTimeout    = 30

	// Architecture types
	ArchMasterSlave = "master-slave"
	ArchMGR         = "mgr"
	ArchPXC         = "pxc"

	// File patterns
	MySQLPackagePatternMinimal = "mysql-%s-linux-glibc2.28-x86_64-minimal.tar.xz"
	MySQLPackagePatternFull    = "mysql-%s-linux-glibc2.28-x86_64.tar.xz"
	LogFilePattern             = "mysql_deploy_%s.log"
	BackupDirPattern           = "%s_backup_%s"
)

Constants for configuration

View Source
const (
	ColorReset  = "\033[0m"
	ColorRed    = "\033[31m"
	ColorGreen  = "\033[32m"
	ColorYellow = "\033[33m"
	ColorBlue   = "\033[34m"
)

颜色常量

Variables

View Source
var CheckupCmd = &cobra.Command{
	Use:   "checkup",
	Short: "MySQL health checkup with HTML report output",
	Run: func(cmd *cobra.Command, args []string) {
		dsn := fmt.Sprintf("%s:%s@(%s:%d)/%s", dbUser, dbPassWd, dbHost, dbPort, database)

		db := mysqlConnect(dsn)
		defer db.Close()

		filename := "mysql_checkup_report.html"
		f, err := os.Create(filename)
		ifErrWithLog(err)
		defer f.Close()

		f.WriteString(htmlHeader)
		f.WriteString(renderDirectory())
		if showOSInfo {
			f.WriteString(renderSysInfo())
		}
		advices := analyzeAdvices(db)
		f.WriteString(renderAdvices(advices))
		f.WriteString(renderModule("指标与参数", paramItemsWithPerf, db))
		f.WriteString(renderModule("数据与索引", tableItems, db))
		f.WriteString(renderModule("SQL与性能", sqlItems, db))
		f.WriteString(renderModule("账号与权限", accountItems, db))
		f.WriteString(renderModule("主从状态", replicaItems, db))
		f.WriteString(htmlFooter)
		fmt.Printf("巡检报告已生成: %s\n", filename)
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "mysqldba",
	Short: "Welcome to the MySQL DBA Toolbox.",
	Long:  "Welcome to the MySQL DBA Toolbox. \nAuthor: HongBin <hongbin119@gmail.com> \nVersion: " + version,
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

func RecommendIndexes

func RecommendIndexes(db *sql.DB, sql string, dynamicSelectivity bool) (map[string][]IndexRecommendation, error)

Types

type AccountPrivilege

type AccountPrivilege struct {
	User           string
	Host           string
	Grants         []string
	HighRisk       bool
	IsWildcardHost bool
	LastUsed       string
}

账号权限结构体

type Advice

type Advice struct {
	Level      string // 高/中/低
	Content    string
	RelatedSQL string
}

巡检建议结构体

type BinlogStats

type BinlogStats struct {
	TotalCount      int
	InsertCount     int
	UpdateCount     int
	DeleteCount     int
	QueryType       string
	QueryCount      int
	TableName       string
	Timestamp       string
	TransactionSize int   // 事务大小(字节)
	StartPosition   int64 // 事务开始位置
}

BinlogStats 统计结构体

type CheckItem

type CheckItem struct {
	Title  string
	Anchor string
	SQL    string
}

CheckItem represents a single checkup item.

type Column

type Column struct {
	Name        string
	Selectivity float64
	CondType    string
	Source      string
}

Column represents a column with its selectivity and condition information

type CommandInfo

type CommandInfo struct {
	Name        string
	Required    bool
	Description string
	InstallCmd  map[string]string // OS -> install command
}

CommandInfo represents information about a command

type CommandResult

type CommandResult struct {
	Host   string
	Output string
	Error  error
}

CommandResult holds the result of a command execution

type DeployConfig

type DeployConfig struct {
	MysqlVersion    string
	InstallPrefix   string
	DataDir         string
	ReplicationUser string
	ReplicationPass string
	AdminUser       string
	AdminPass       string
	RootPass        string
	MyCnfTemplate   string
	ServiceTemplate string
	HostFile        string
	Architecture    string
	SSHUser         string
	SSHPort         int
	MySQLPort       int
	MGRGroupName    string
	MGRPort         int
	ForceDownload   bool
	ForceDeploy     bool
	SSHKeyPath      string
	MinimalPackage  bool   // 是否使用minimal安装包
	AppUser         string // 应用账号用户名
	AppPass         string // 应用账号密码
}

DeployConfig holds all deployment configuration

type DiagnosticInfo

type DiagnosticInfo struct {
	OutputDir   string
	MyCnfPath   string
	DataDir     string
	ErrorLog    string
	WarningsLog string
	StartTime   time.Time
}

type HostInfo

type HostInfo struct {
	IP       string
	Role     string
	ServerID int
}

HostInfo represents a single host configuration

type IndexRecommendation

type IndexRecommendation struct {
	IndexSQL        string
	SelectivityInfo string
}

IndexRecommendation holds the recommended index information

type SQLParseResult

type SQLParseResult struct {
	Tables       map[string]string           // alias -> real table name
	TableColumns map[string]*TableColumnInfo // table name -> column info
}

SQLParseResult contains the complete parsed SQL information

func ParseSQL

func ParseSQL(query string) (*SQLParseResult, error)

ParseSQL parses SQL statement and extracts table and column information

type SSHClient

type SSHClient struct {
	Addr    string
	User    string
	KeyPath string
	Client  *ssh.Client
}

SSH免密登录工具

func NewSSHClient

func NewSSHClient(addr, user, keyPath string) (*SSHClient, error)

func (*SSHClient) Close

func (c *SSHClient) Close() error

Close 关闭SSH客户端连接

func (*SSHClient) CopyDir

func (c *SSHClient) CopyDir(localDir, remoteDir string) error

CopyDir 复制整个目录到远程服务器

func (*SSHClient) CopyFile

func (c *SSHClient) CopyFile(localPath, remotePath string) error

func (*SSHClient) Run

func (c *SSHClient) Run(cmd string) (string, error)

func (*SSHClient) RunWithStream

func (c *SSHClient) RunWithStream(cmd string, outputWriter io.Writer) error

type SSHConfig

type SSHConfig struct {
	HostFile   string
	Command    string
	LocalFile  string
	RemotePath string
	SSHUser    string
	SSHPort    int
	SSHKeyPath string
	Timeout    int
	Parallel   int
}

SSHConfig holds SSH configuration

type TableColumnInfo

type TableColumnInfo struct {
	TableName      string
	WhereColumns   []string
	JoinColumns    []string
	GroupByColumns []string
	OrderByColumns []string
	SelectColumns  []string
}

TableColumnInfo holds column information for a specific table

type UserCreateRequest

type UserCreateRequest struct {
	Username    string
	Password    string
	Host        string
	Privileges  []string
	RequireSSL  bool
	Description string
}

UserCreateRequest represents a database user creation request

Jump to

Keyboard shortcuts

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