What is the scenario for getting error "dial tcp xxx:xxx:xxx:xxx:3306: connect: connection refused" and how to avoid this error

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

What is the scenario for getting error "dial tcp xxx:xxx:xxx:xxx:3306: connect: connection refused" and how to avoid this error

问题

我有一个调用 SQL 连接的 API。这是连接到某个远程服务器的 SQL 连接。在执行查询时,我遇到了错误 "dial tcp xxx:xxx:xxx:xxx:3306: connect: connection refused"。

SQL 连接代码如下:

func ConnectToMysqlDB(dbUser, dbPassword, dbHost, dbName string) *sql.DB {
    connstr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPassword, dbHost, "3306", dbName)
    var err error
    log.Println("正在执行 MySQL 查询", connstr)
    dB, err := sql.Open("mysql", connstr)
    if err != nil {
        log.Fatalf("连接到 MySQL 出错 [%v] ..!!", err)
    }
    log.Print("MySQL 初始化完成 ..!!")
    return dB
}

查询函数如下:

func GetUserIDs(dB *sql.DB, users []string) []int {
    var newUsers []int
    var newUser int

    query := "SELECT id FROM phplist_user_user WHERE foreignkey in (%s)"
    var usersStr string
    for _, val := range users {
        usersStr = usersStr + "'" + val + "',"
    }
    usersStr = strings.TrimSuffix(usersStr, ",")

    query = fmt.Sprintf(query, usersStr)

    rows, err := dB.Query(query)
    if err != nil {
        fmt.Printf("查询出错: [%v]", err)
    }
    defer rows.Close()
    for rows.Next() {
        rows.Scan(&newUser)
        newUsers = append(newUsers, newUser)
    }

    return newUsers
}

请问在什么情况下会出现这个错误:"查询出错: [dial tcp xxx.xxx.xxx.xxx:3306: connect: connection refused]"?

英文:

I have a api which is calling the sql connection. This is sql connection to some remote server. While performing a query I am getting error "dial tcp xxx:xxx:xxx:xxx:3306: connect: connection refused"

Sql connection code

func ConnectToMysqlDB(dbUser, dbPassword, dbHost, dbName string) *sql.DB {
	connstr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPassword, dbHost, "3306", dbName)
	var err error
	log.Println("Carrying mysql query", connstr)
	dB, err := sql.Open("mysql", connstr)
	if err != nil {
		log.Fatalf("error connection to mysql [%v] ..!!", err)
	}
	log.Print("mysql initialized ..!!")
	return dB
}

Query function

func GetUserIDs(dB *sql.DB, users []string) []int {
	var newUsers []int
	var newUser int

	query := "SELECT id FROM phplist_user_user WHERE foreignkey in (%s)"
	var usersStr string
	for _, val := range users {
		usersStr = usersStr + "'" + val + "'" + ","
	}
	usersStr = strings.TrimSuffix(usersStr, ",")

	query = fmt.Sprintf(query, usersStr)

	rows, err := dB.Query(query)
	if err != nil {
		fmt.Printf("Error in query : [%v]", err)
	}
	defer rows.Close()
	for rows.Next() {
		rows.Scan(&newUser)
		newUsers = append(newUsers, newUser)
	}

	return newUsers
}

what is the sceario where I am getting the error: Error in query : [dial tcp xxx.xxx.xxx.xxx:3306: connect: connection refused]

答案1

得分: 0

我的两分钱:

  1. 使用sql.DB中的Ping()方法验证是否真正连接到mysql - 但是您必须注意,某些错误可能是暂时的。例如,如果您遇到网络问题或数据库“重新启动”(高可用性取决于许多因素)。
  2. 检查您是否使用正确的IP和端口。也许数据库和应用程序位于不同的网络中,或者数据库服务器绑定了多个网络卡。

此外,请检查以下链接是否有所帮助:
https://github.com/go-sql-driver/mysql#allowcleartextpasswords

英文:

My two cents:

  1. Validate if you are really connected to mysql using the Ping() method in sql.DB — however you must pay attention that some errors may be temporary. For instance if you have a network issue or a database “reboot” (high availability depends of many factors)
  2. Check if you are using the right ip and port. Perhaps the database and application are in different networks or the database server binds more than one network card.

Also, Check if this helps
https://github.com/go-sql-driver/mysql#allowcleartextpasswords

huangapple
  • 本文由 发表于 2022年3月22日 14:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71567749.html
匿名

发表评论

匿名网友

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

确定