mysqlflags

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2020 License: MIT Imports: 14 Imported by: 6

README

go-mysqlflags

Utility for mysql related flags and connect and exec show statement to mysql.

Usage

import "github.com/kazeburo/go-mysqlflags"
use with go-flags and Connect to DB

Create DSN for connecting database.

type opts struct {
	mysqlflags.MyOpts
	Timeout time.Duration `long:"timeout" default:"10s" description:"Timeout to connect mysql"`
}

psr := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
_, err := psr.Parse()

dsn, err := mysqlflags.CreateDSN(opts.MyOpts, opts.Timeout, false)

When my_print_defaults command is available, mysqlflags.CreateDSN reads mysql client parameter from that. The parameters given with go-flags overwrite them.

Open Database with mysqlflags.

db, err := mysqlflags.OpenDB(opts.MyOpts, opts.Timeout, false)
if err != nil {
	log.Printf("couldn't connect DB: %v", err)
	return 1
}
defer db.Close()
Exec show statement and get result as struct

show global status / show variables

type threads struct {
	Running   int64 `mysqlvar:"Threads_running"`
	Connected int64 `mysqlvar:"Threads_connected"`
	Cached    int64 `mysqlvar:"Threads_cached"`
}

type connections struct {
	Max       int64 `mysqlvar:"max_connections"`
	CacheSize int64 `mysqlvar:"thread_cache_size"`
}

var threads threads
err = mysqlflags.Query(db, "SHOW GLOBAL STATUS").Scan(&threads)
if err != nil {
	return err
}

var connections connections
err = mysqlflags.Query(db, "SHOW VARIABLES").Scan(&connections)
if err != nil {
	return err
}

show slave(replica) status (single source)

type replica struct {
	IORunning   mysqlflags.Bool `mysqlvar:"Slave_IO_Running"`
	SQLRunning  mysqlflags.Bool `mysqlvar:"Slave_SQL_Running"`
    LastSQLError string `mysqlvar:"Last_SQL_Error"`
}

var replica replica
err := mysqlflags.Query(db, "SHOW SLAVE STATUS").Scan(&replica)

f !replica.IORunning.Yes() || !replica.SQLRunning.Yes() {
    fmt.Errorf("something wrong is replication IO:%s SQL:%s Error:%s",
        replica.IORunning, replica.SQLRunning, replica.LastSQLError);
}

show slave(replica) status (multi source)

type replica struct {
	IORunning   mysqlflags.Bool `mysqlvar:"Slave_IO_Running"`
	SQLRunning  mysqlflags.Bool `mysqlvar:"Slave_SQL_Running"`
	ChannelName *string         `mysqlvar:"Channel_Name"` // use pointer for optinal field
	Behind      int64           `mysqlvar:"Seconds_Behind_Master"`
}


var replicas []replica
err := mysqlflags.Query(db, "SHOW SLAVE STATUS").Scan(&replicas)
for _, replica := range replicas {
    ..
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDSN

func CreateDSN(opts MyOpts, timeout time.Duration, debug bool) (string, error)

CreateDSN creates DSN from Opts. omit timeout parameter when timeout is 0

func OpenDB

func OpenDB(opts MyOpts, timeout time.Duration, debug bool) (*sql.DB, error)

OpenDB opens MySQL connections from Opts

Types

type Bool added in v0.0.8

type Bool struct {
	// contains filtered or unexported fields
}

Bool mysql variables boolean

func (Bool) String added in v0.0.8

func (b Bool) String() string

True true when the original string

func (Bool) Yes added in v0.0.9

func (b Bool) Yes() bool

Yes return true when the original is Yes or yes or YES

type MyOpts

type MyOpts struct {
	MySQLDefaultsExtraFile string  `long:"defaults-extra-file" description:"path to defaults-extra-file"`
	MySQLSocket            string  `long:"mysql-socket" description:"path to mysql listen sock"`
	MySQLHost              string  `short:"H" long:"host" default:"localhost" description:"Hostname"`
	MySQLPort              string  `short:"p" long:"port" default:"3306" description:"Port"`
	MySQLUser              string  `short:"u" long:"user" default:"root" description:"Username"`
	MySQLPass              *string `short:"P" long:"password" description:"Password"`
	MySQLDBName            string  `long:"database" default:"" description:"database name connect to"`
	MySQLDSNParams         map[string]string
}

MyOpts mysql connection related flags used with go-flags

type QueryMap added in v0.0.4

type QueryMap struct {
	// contains filtered or unexported fields
}

QueryMap has rows map and error

func Query added in v0.0.4

func Query(db *sql.DB, query string, args ...interface{}) *QueryMap

Query does exec show statement and return QueryMap for Scan

func (*QueryMap) Scan added in v0.0.4

func (qm *QueryMap) Scan(dest interface{}) error

Scan converts rows to Struct

Jump to

Keyboard shortcuts

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