Hello Everyone,
I am MySQL database administrator and I’m testing out Golang for some administrative MySQL tasks.
Presently, I am trying to handle the output of a MySQL command called “SHOW SLAVE STATUS.”
Here is what the output looks like from the Linux / MySQL commandline
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: server.hostname.com
Master_User: replUser
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 999999999
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 999999999
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 376515189
Relay_Log_Space: 376515572
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Master_Server_Id: 340472056
Master_UUID: a75ae239-4444-11e6-4444-1402ec6b1540
Master_Info_File: /mnt/server/foldername/mysql/master.info
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
1 row in set (0.00 sec)
As you can see, the output is verbose and there are many “columns” in the output.
This is the code I am using to run the “SHOW SLAVE STATUS” command.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql",
"$USER:$PASS@tcp($HOSTNAME:3306)/Testing")
if err != nil {
log.Println(err)
}
var str string
err = db.QueryRow("SHOW SLAVE STATUS").Scan(&str)
if err != nil && err != sql.ErrNoRows {
log.Println(err)
}
fmt.Println(str)
defer db.Close()
}
I want to save this output into some variable or object and pick and choose from it later.
Here’s some (not correct) pseudo code to explain what (I think) I want to do.
//Fake golang code to explain what I'm trying to do
//Get slave status
slaveStatus := getSlaveStatus()
//Check if MySQL slave is running
patternToFind := regexp.Find('Slave_IO_Running: Yes', slaveStatus)
//Evaluate if MySQL replication is running
if regexp.MatchString("Slave_IO_Running: Yes", patternToFind) {
fmt.Println("Slave is running")
} else {
fmt.Println("Slave is not running") }
I have the following questions.
1.) Is there a function or method to find out how Golang is interpreting the output of “SHOW SLAVE STATUS” data?
Is it receiving a map, a slice, a string, a blob? How can I learn how Golang sees this output?
2.) Different MySQL servers have different “columns” in the output of “SHOW SLAVE STATUS.” What is the best way to have Golang deal with a result set that never has the same amount of columns? How should I deal with ever changing number columns in the result set from a query?
====
Any advice, direction, suggestions would be appreciated. Thanks!!