英文:
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
我的两分钱:
- 使用sql.DB中的Ping()方法验证是否真正连接到mysql - 但是您必须注意,某些错误可能是暂时的。例如,如果您遇到网络问题或数据库“重新启动”(高可用性取决于许多因素)。
- 检查您是否使用正确的IP和端口。也许数据库和应用程序位于不同的网络中,或者数据库服务器绑定了多个网络卡。
此外,请检查以下链接是否有所帮助:
https://github.com/go-sql-driver/mysql#allowcleartextpasswords
英文:
My two cents:
- 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)
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论