|
| 1 | +/* |
| 2 | + Copyright 2023 GitHub Inc. |
| 3 | + See https://github.com/github/gh-ost/blob/master/LICENSE |
| 4 | +*/ |
| 5 | + |
| 6 | +package mysql |
| 7 | + |
| 8 | +import gosql "database/sql" |
| 9 | + |
| 10 | +// ServerInfo represents the online config of a MySQL server. |
| 11 | +type ServerInfo struct { |
| 12 | + Version string |
| 13 | + VersionComment string |
| 14 | + Hostname string |
| 15 | + Port gosql.NullInt64 |
| 16 | + BinlogFormat string |
| 17 | + BinlogRowImage string |
| 18 | + LogBin bool |
| 19 | + LogSlaveUpdates bool |
| 20 | + SQLMode string |
| 21 | + TimeZone string |
| 22 | + |
| 23 | + // @@global.extra_port is Percona/MariaDB-only |
| 24 | + ExtraPort gosql.NullInt64 |
| 25 | +} |
| 26 | + |
| 27 | +// GetServerInfo returns a ServerInfo struct representing |
| 28 | +// the online config of a MySQL server. |
| 29 | +func GetServerInfo(db *gosql.DB) (*ServerInfo, error) { |
| 30 | + var info ServerInfo |
| 31 | + query := `select /* gh-ost */ @@global.version, @@global.version_comment, @@global.hostname, |
| 32 | + @@global.port, @@global.binlog_format, @@global.binlog_row_image, @@global.log_bin, |
| 33 | + @@global.log_slave_updates, @@global.sql_mode, @@global.time_zone` |
| 34 | + if err := db.QueryRow(query).Scan(&info.Version, &info.VersionComment, &info.Hostname, |
| 35 | + &info.Port, &info.BinlogFormat, &info.BinlogRowImage, &info.LogBin, |
| 36 | + &info.LogSlaveUpdates, &info.SQLMode, &info.TimeZone, |
| 37 | + ); err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + extraPortQuery := `select @@global.extra_port` |
| 42 | + // swallow possible error. not all servers support extra_port |
| 43 | + _ = db.QueryRow(extraPortQuery).Scan(&info.ExtraPort) |
| 44 | + |
| 45 | + return &info, nil |
| 46 | +} |
0 commit comments