How to ping remote mysql using golang

huangapple go评论95阅读模式
英文:

How to ping remote mysql using golang

问题

我正在使用go来ping一些Linux机器。
但是它的工作不准确,因为它总是需要近10秒钟才能连接到远程的mysql。

我使用的命令是:

mysql -u USER -p PASSWORD -h REMOTE_IP

所以我想知道如何使用Linux命令来判断它是否打开。

然后我将使用golang的exec.Command来执行这个命令。

这是我的ping代码:

package main

import (
    "errors"
    "fmt"
    "github.com/tatsushid/go-fastping"
    "net"
    "net/smtp"
    "strings"
    "time"
)

func main(){
    err := ping("192.168.2.1")
    if err != nil{
        fmt.Println("WARNING!!!!!!")
    }
}
func ping(ip string) error {
    p := fastping.NewPinger()
    ra, err := net.ResolveIPAddr("ip", ip)
    if err != nil {
        return err
    }
    p.AddIPAddr(ra)
    found := false
    p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
        found = true
    }
    err = p.Run()
    if err != nil {
        return err
    }
    if !found {
        return errors.New("response error")
    }
    return nil
}
英文:

I am using go to ping some Linux machines.
But it is not work accurate, because it always needs almost 10 seconds to connect the remote mysql.

I use this command:

> mysql -u USER -p PASSWORD -h REMOTE_IP

So i want to know how to use linux command to know whether it open or not.

Then I will use golang 's exec.Command to do that .

Here is my ping code:

package main

import (
    "errors"
    "fmt"
    "github.com/tatsushid/go-fastping"
    "net"
    "net/smtp"
    "strings"
    "time"
)

func main(){
    err := ping("192.168.2.1")
    if err != nil{
        fmt.Println("WARNING!!!!!!")
    }
}
func ping(ip string) error {
    p := fastping.NewPinger()
    ra, err := net.ResolveIPAddr("ip", ip)
    if err != nil {
        return err
    }
    p.AddIPAddr(ra)
    found := false
    p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
        found = true
    }
    err = p.Run()
    if err != nil {
        return err
    }
    if !found {
        return errors.New("response error")
    }
    return nil
}

答案1

得分: 4

我假设你想知道mysql是否可达。你可以使用以下示例代码:

package main

import (
	"database/sql"
	"flag"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	var (
		server = flag.String("mysql", "localhost:3306", "mysql服务器")
		user   = flag.String("user", "root", "mysql用户")
		pass   = flag.String("password", "", "mysql密码")
	)
	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()

	err = db.Ping()

	if err != nil {
		log.Fatalln(err)
	} else {
		log.Println("mysqld is alive")
	}
}

这段代码用于检查mysql是否可达。你可以根据需要修改服务器地址、用户名和密码。

英文:

I assume you want to know if mysql is reachable or not. You can use following example:

package main

import (
	"database/sql"
	"flag"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	var (
		server = flag.String("mysql", "localhost:3306", "mysql server")
		user   = flag.String("user", "root", "mysql user")
		pass   = flag.String("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()

	err = db.Ping()

	if err != nil {
		log.Fatalln(err)

	} else {
		log.Println("mysqld is alive")

	}

}

答案2

得分: 2

如果你只需要检查与特定主机和端口的连接,你可以这样做:

package main

import (
	"fmt"
	"net"
)

const (
	host = "192.168.2.1"
	port = "3306" // 默认的MySQL端口
)

func main() {

	conn, err := net.Dial("tcp", host+":"+port)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	fmt.Printf("已连接到 %s \n", conn.RemoteAddr().String())
}

希望对你有帮助!

英文:

If you just need to check connection to a specific host & port, you can do this as:

package main

import (
	"fmt"
	"net"
)

const (
	host = "192.168.2.1"
	port = "3306" // default MySQL port
)

func main() {

	conn, err := net.Dial("tcp", host+":"+port)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	fmt.Printf("Connected to %s \n", conn.RemoteAddr().String())
}

huangapple
  • 本文由 发表于 2016年12月9日 13:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/41053830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定