Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.
Open
31 changes: 29 additions & 2 deletions commands/management/add_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type addMySQLCommand struct {
ServiceName string
Username string
Password string
DefaultsFile string
AgentPassword string
Environment string
Cluster string
Expand Down Expand Up @@ -125,9 +126,17 @@ func (cmd *addMySQLCommand) GetAddress() string {
}

func (cmd *addMySQLCommand) GetDefaultAddress() string {
if cmd.DefaultsFile != "" {
// address might be specified in defaults file
return ""
}
return "127.0.0.1:3306"
Comment thread
BupycHuk marked this conversation as resolved.
}

func (cmd *addMySQLCommand) GetDefaultUsername() string {
return "root"
}

func (cmd *addMySQLCommand) GetSocket() string {
return cmd.Socket
}
Expand Down Expand Up @@ -179,6 +188,8 @@ func (cmd *addMySQLCommand) Run() (commands.Result, error) {
return nil, err
}

username := defaultsFileUsernameCheck(cmd)

tablestatsGroupTableLimit := int32(cmd.DisableTablestatsLimit)
if cmd.DisableTablestats {
if tablestatsGroupTableLimit != 0 {
Expand All @@ -199,8 +210,9 @@ func (cmd *addMySQLCommand) Run() (commands.Result, error) {
Environment: cmd.Environment,
Cluster: cmd.Cluster,
ReplicationSet: cmd.ReplicationSet,
Username: cmd.Username,
Username: username,
Password: cmd.Password,
DefaultsFile: cmd.DefaultsFile,
AgentPassword: cmd.AgentPassword,
CustomLabels: customLabels,

Expand Down Expand Up @@ -251,8 +263,9 @@ func init() {
AddMySQLC.Flag("node-id", "Node ID (default is autodetected)").StringVar(&AddMySQL.NodeID)
AddMySQLC.Flag("pmm-agent-id", "The pmm-agent identifier which runs this instance (default is autodetected)").StringVar(&AddMySQL.PMMAgentID)

AddMySQLC.Flag("username", "MySQL username").Default("root").StringVar(&AddMySQL.Username)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for username let's use the same logic as for default address.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

AddMySQLC.Flag("username", "MySQL username").StringVar(&AddMySQL.Username)
AddMySQLC.Flag("password", "MySQL password").StringVar(&AddMySQL.Password)
AddMySQLC.Flag("defaults-file", "Path to defaults file").StringVar(&AddMySQL.DefaultsFile)
AddMySQLC.Flag("agent-password", "Custom password for /metrics endpoint").StringVar(&AddMySQL.AgentPassword)

querySources := []string{mysqlQuerySourceSlowLog, mysqlQuerySourcePerfSchema, mysqlQuerySourceNone} // TODO add "auto", make it default
Expand Down Expand Up @@ -284,3 +297,17 @@ func init() {
AddMySQLC.Flag("disable-collectors", "Comma-separated list of collector names to exclude from exporter").StringVar(&AddMySQL.DisableCollectors)
addGlobalFlags(AddMySQLC)
}

func defaultsFileUsernameCheck(cmd *addMySQLCommand) string {
// defaults file specified, but passed username has higher priority
if cmd.Username != "" && cmd.DefaultsFile != "" {
return cmd.Username
}

// username not specified, but can be in defaults files
if cmd.Username == "" && cmd.DefaultsFile != "" {
return ""
}

return cmd.GetDefaultUsername()
}