按照官网教程,可以使用`db.ping()`来查看,可是当远程机器关闭之后,`db.ping()`没有返回错误,
请问有什么办法可以持续的监控一台机器的mysql连接
package main
import (
"database/sql"
"flag"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
func main() {
var (
server = flag.String("mysql", "192.168.2.200:3306", "mysql server")
user = flag.String("user", "root", "mysql user")
pass = flag.String("password", "password", "mysql password")
)
flag.Parse()
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/", *user, *pass, *server))
if err != nil {
log.Fatalln(err)
}
defer db.Close()
for {
err = db.Ping()
if err != nil {
log.Fatalln(err)
}else {
log.Println("mysqld is alive")
}
}
}
有疑问加站长微信联系(非本文作者)