Created
April 21, 2017 07:39
-
-
Save artefactop/31c781907672e6b9e024564fabe43884 to your computer and use it in GitHub Desktop.
Get mysql bin log and execute those operations in other server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/siddontang/go-mysql/client" | |
"github.com/siddontang/go-mysql/mysql" | |
"github.com/siddontang/go-mysql/replication" | |
"os" | |
) | |
const ( | |
host string = "172.0.0.1" | |
port int = 3306 | |
user string = "root" | |
password string = "root" | |
) | |
func start_replica(host string, port int, user string, password string, binLogFile string, binLogPos int64) { | |
// Create a binlog syncer with a unique server id, the server id must be different from other MySQL's. | |
// flavor is mysql or mariadb | |
syncer := replication.NewBinlogSyncer(100, "mysql") | |
// Register slave, the MySQL master is at 127.0.0.1:3306, with user root and root password | |
syncer.RegisterSlave(host, uint16(port), user, password) | |
// Start sync with sepcified binlog file and position | |
streamer, _ := syncer.StartSync(mysql.Position{binLogFile, uint32(binLogPos)}) | |
for { | |
ev, _ := streamer.GetEvent() | |
// Dump event | |
ev.Dump(os.Stdout) | |
} | |
} | |
func get_bin_file_and_pos(host string, port int, user string, password string) (string, int64) { | |
conn, err := client.Connect(fmt.Sprintf("%s:%d", host, port), user, password, "") | |
if err != nil { | |
fmt.Print(err.Error()) | |
} | |
conn.Ping() | |
defer conn.Close() | |
//get current master binlog file and position | |
r, err := conn.Execute("SHOW MASTER STATUS") | |
binFile, _ := r.GetString(0, 0) | |
binPos, _ := r.GetInt(0, 1) | |
return binFile, binPos | |
} | |
func main() { | |
binFile, binPos := get_bin_file_and_pos(host, port, user, password) | |
start_replica(host, port, user, password, binFile, binPos) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment