msops

package module
v0.0.0-...-e9d2142 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2016 License: MIT Imports: 9 Imported by: 1

README

msops

Build Status codecov.io MIT license

A Go library for MySQL Ops. Based on go-sql-driver.

Requirements

go-sql-driver should be pre-installed

go get github.com/go-sql-driver/mysql

Installation

go get github.com/ericpai/msops

Examples

package main

import (
	"fmt"

	"github.com/ericpai/msops"
)

func main() {
	dbaUsers := []string{"dba", "dba"}
	dbaPassword := []string{"password", "password"}
	replUsers := []string{"repl", "repl"}
	replPassword := []string{"password", "password"}
	dbEndpoints := []string{"127.0.0.1:3306", "127.0.0.1:3307"}
	params := map[string]string{
		"charset": "utf8",
		"timeout": "1s",
	}

	// Register DB instances
	for i := 0; i < 2; i++ {
		if err := msops.Register(dbEndpoints[i], dbaUsers[i], dbaPassword[i], replUsers[i], replPassword[i], params); err != nil {
			fmt.Printf("Register db[%d] error: %s\n", i, err.Error())
		} else {
			defer msops.Unregister(dbEndpoints[i])
		}
	}

	// Get master status of DB0
	if masterSt, err := msops.GetMasterStatus(dbEndpoints[0]); err != nil {
		fmt.Printf("Get master status of db[0] error: %s\n", err.Error())
	} else {
		fmt.Printf("Master log file: %s, log position: %d\n", masterSt.File, masterSt.Position)
	}

	// Change DB0 to be the master of DB1
	if err := msops.ChangeMasterTo(dbEndpoints[1], dbEndpoints[0], false); err != nil {
		fmt.Printf("Change master error: %s\n", err.Error())
	}

}

User Guide

See API documentations here.

Developer Guide

If you have found bugs or want to propose some new features, please submit an issue to this repo. Pull requests are welcome as well.

Licensing

Msops is released under MIT license.

Documentation

Overview

Package msops implements series of mysql ops methods.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeMasterTo

func ChangeMasterTo(slaveEndpoint, masterEndpoint string, useGTID bool) error

ChangeMasterTo makes slaveEndpoint as a slave of masterEndpoint from now on. Use MASTER_AUTO_POSITION=1 instead of specifying the binlog file and position if useGTID is true.

func GetGlobalStatus

func GetGlobalStatus(endpoint, pattern string) (map[string]string, error)

GetGlobalStatus executes "SHOW GLOBAL STATUS LIKE pattern" and returns the resultset.

func GetGlobalVariables

func GetGlobalVariables(endpoint, pattern string) (map[string]string, error)

GetGlobalVariables executes "SHOW GLOBAL VARIABLES LIKE pattern" and returns the resultset.

func KillProcesses

func KillProcesses(endpoint string, whiteUsers ...string) error

KillProcesses kills all the connection threads except the ones of whiteUsers.

func Register

func Register(endpoint, dbaUser, dbaPassword, replUser, replPassword string, params map[string]string) error

Register registers the instance of endpoint with opening the connection with user 'dbaUser', password 'dbaPassword'.

'replUser' and 'replPassword' are used to establish replication by other endpoints.

'params' are the k-v params appending to go-mysql-driver connection string.

'dbaUser' should have the following privileges at least: RELOAD, PROCESS, SUPER, REPLICATION CLIENT, REPLICATION SLAVE.

'replUser' should have the following privileges at least: PROCESS, REPLICATION SLAVE.

'endpoint' show have the form "host:port".

If the final connection string generated is invalid, an error will be returned.

func ResetSlave

func ResetSlave(endpoint string, resetAll bool) error

ResetSlave executes "RESET SLAVE ALL" if resetAll is true. Otherwise executes "RESET SLAVE".

func SetGlobalVariable

func SetGlobalVariable(endpoint, key string, value interface{}) error

SetGlobalVariable executes the statement 'SET GLOBAL key=value'.

func StartSlave

func StartSlave(endpoint string) error

StartSlave executes "START SLAVE" at the endpoint.

func StopSlave

func StopSlave(endpoint string) error

StopSlave executes "STOP SLAVE" at the endpoint.

func Unregister

func Unregister(endpoint string)

Unregister deletes the information from msops's connection pool and closes the connections to endpoint.

Types

type InnoDBStatus

type InnoDBStatus struct {
	InnodbMutexSpinWaits  int
	InnodbMutexSpinRounds int
	InnodbMutexOSWaits    int
}

InnoDBStatus represents the innodb engine status of one endpoint. Based on 5.6.30-log MySQL Community Server.

Field specification can be found at https://dev.mysql.com/doc/refman/5.6/en/innodb-standard-monitor.html

func GetInnoDBStatus

func GetInnoDBStatus(endpoint string) (InnoDBStatus, error)

GetInnoDBStatus executes "SHOW engine InnoDB STATUS" and returns the 'Status' field.

type Instance

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

Instance records the connect information.

type InstanceStatus

type InstanceStatus int

InstanceStatus represents the running status of one instance.

The judgement is according to the result of db.Conn.Ping().

const (
	// InstanceOK implies that we can connect to the instance.
	InstanceOK InstanceStatus = iota

	// InstanceERROR implies that we can't connect to the instance.
	InstanceERROR

	// InstanceUnregistered implies that we haven't registered the instance.
	InstanceUnregistered
)

func CheckInstance

func CheckInstance(endpoint string) InstanceStatus

CheckInstance checks the status of a instance with the endpoint.

type MasterStatus

type MasterStatus struct {
	File            string
	Position        int
	BinlogDoDB      string
	BinlogIgnoreDB  string
	ExecutedGtidSet string
}

MasterStatus represents the master status of one endpoint. Based on 5.6.30-log MySQL Community Server.

Field specification can be found at https://dev.mysql.com/doc/refman/5.6/en/show-master-status.html

func GetMasterStatus

func GetMasterStatus(endpoint string) (MasterStatus, error)

GetMasterStatus executes "SHOW MASTER STATUS" and returns the resultset.

type Process

type Process struct {
	ID      int
	User    string
	Host    string
	DB      string
	Command string
	Time    int
	State   string
	Info    string
}

Process represents one row data of processlist. Based on 5.6.30-log MySQL Community Server.

Field specification can be found at https://dev.mysql.com/doc/refman/5.6/en/show-processlist.html

func GetProcessList

func GetProcessList(endpoint string) ([]Process, error)

GetProcessList executes "SHOW PROCESSLIST" and returns the resultset.

type ReplicationStatus

type ReplicationStatus int

ReplicationStatus represents the replication status between to instance.

The judgement is according to the result of `SHOW SLAVE STATUS` and `SHOW MASTER STATUS`.

Comparing the binlog file and binlog positions between master and slave.

const (
	// ReplicationOK implies that in the slave status of the slave instance,
	// 'Master_Host' and 'Master_Port' are the same as the master's,
	// 'Slave_SQL_Running' and 'Slave_IO_Running' are both 'Yes',
	// 'Master_Log_File' and 'Master_Log_Position'equals to '0'.
	ReplicationOK ReplicationStatus = iota

	// ReplicationError implies that in the slave status of the slave instance,
	// 'Master_Host' and 'Master_Port' are the same as the master's,
	// 'Slave_SQL_Running' and 'Slave_IO_Running' are not both 'Yes',
	// and 'Last_Error' is not empty.
	ReplicationError

	// ReplicationSyning implies that in the slave status of the slave instance,
	// 'Master_Host' and 'Master_Port' are the same as the master's,
	// 'Slave_SQL_Running' and 'Slave_IO_Running' are both 'Yes',
	// 'Second_Behind_Master' is larger than '0'.
	ReplicationSyning

	// ReplicationPausing implies that in the slave status of the slave instance,
	// 'Master_Host' and 'Master_Port' are the same as the master's,
	// and 'Slave_SQL_Running' and 'Slave_IO_Running' are both 'no'.
	ReplicationPausing

	// ReplicationWrongMaster implies that in the slave status of the slave instance,
	// 'Master_Host' and 'Master_Port' are not the same as the master's.
	ReplicationWrongMaster

	// ReplicationNone implies that the slave status of the endpoint is empty.
	ReplicationNone

	// ReplicationUnknown implies that we can't connect to the slave instance.
	ReplicationUnknown
)

func CheckReplication

func CheckReplication(slaveEndpoint, masterEndpoint string) ReplicationStatus

CheckReplication checks the replicaton status between slaveEndpoint and masterEndpoint. Note that if one of slave or master is not registered, or getting MasterStatus and SlaveStatus failed, ReplicationUnknown is returned.

type SlaveStatus

type SlaveStatus struct {
	SlaveIOState              string
	MasterHost                string
	MasterUser                string
	MasterPort                int
	ConnectRetry              string
	MasterLogFile             string
	ReadMasterLogPos          int
	RelayLogFile              string
	RelayLogPos               int
	RelayMasterLogFile        string
	SlaveIORunning            string
	SlaveSQLRunning           string
	ReplicateDoDB             string
	ReplicateIgnoreDB         string
	ReplicateDoTable          string
	ReplicateIgnoreTable      string
	ReplicateWildDoTable      string
	ReplicateWildIgnoreTable  string
	LastErrno                 int
	LastError                 string
	SkipCounter               int
	ExecMasterLogPos          int
	RelayLogSpace             int
	UntilCondition            string
	UntilLogFile              string
	UntilLogPos               int
	MasterSSLAllowed          string
	MasterSSLCAFile           string
	MasterSSLCAPath           string
	MasterSSLCert             string
	MasterSSLCipher           string
	MasterSSLKey              string
	SecondsBehindMaster       int
	MasterSSLVerifyServerCert string
	LastIOErrno               int
	LastIOError               string
	LastSQLErrno              int
	LastSQLError              string
	ReplicateIgnoreServerIds  string
	MasterServerID            int
	MasterUUID                string
	MasterInfoFile            string
	SQLDelay                  int
	SQLRemainingDelay         string
	SlaveSQLRunningState      string
	MasterRetryCount          int
	MasterBind                string
	LastIOErrorTimestamp      string
	LastSQLErrorTimestamp     string
	MasterSSLCrl              string
	MasterSSLCrlpath          string
	RetrievedGtidSet          string
	ExecutedGtidSet           string
	AutoPosition              bool
}

SlaveStatus represents the slave status of one endpoint. Based on 5.6.30-log MySQL Community Server.

Field specification can be found at https://dev.mysql.com/doc/refman/5.6/en/show-slave-status.html

func GetSlaveStatus

func GetSlaveStatus(endpoint string) (SlaveStatus, error)

GetSlaveStatus executes "SHOW SLAVE STATUS" and returns the resultset.

Jump to

Keyboard shortcuts

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