Unable to open tcp connection. No connection could be made because the target machine actively refused it. exit status 1

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

Unable to open tcp connection. No connection could be made because the target machine actively refused it. exit status 1

问题

我尝试从 SQL Server 数据库中获取数据。当我运行时,一直出现以下错误:

> 无法打开与主机 'localhost:1433' 的 TCP 连接:拨号 tcp [::1]:1433: connectex: 由于目标计算机积极拒绝,无法建立连接。
退出状态 1


我的代码:

package main

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

	_ "github.com/denisenkom/go-mssqldb"
)

var kiosk string

var server = ".\\MSSQLSERVER01"
var port = 1433
var user = "DESKTOP-37624KK"
var password = "**********"
var database = "Kiosk"

func main() {
	connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s", server, user, password, port, database)

	db, err := sql.Open("mssql", connString)
	if err != nil {
		fmt.Println("连接数据库时出错")
		log.Fatal(err)
	}

	rows, err := db.Query("SELECT * FROM Kiosk")
	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		if err := rows.Scan(&kiosk); err != nil {
			log.Fatal(err)
		}
		fmt.Println(kiosk)
	}
	defer rows.Close()
}

  • 检查防火墙是否阻止所需端口
  • 尝试更改并连接到新端口
  • 尝试在互联网上搜索其他解决方案。目前还没有找到有效的帮助。
英文:

I tried to fetch data from a SQL Server database. When I run, it keeps giving me this error:

> unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex: No connection could be made because the target machine actively refused it.
exit status 1


My code:

package main

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

	_ "github.com/denisenkom/go-mssqldb"
)

var kiosk string

var server = ".\\MSSQLSERVER01"
var port = 1433
var user = "DESKTOP-37624KK"
var password = "**********"
var database = "Kiosk"

func main() {
	connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s", server, user, password, port, database)

	db, err := sql.Open("mssql", connString)
	if err != nil {
		fmt.Println("Error in connect DB")
		log.Fatal(err)
	}

	rows, err := db.Query("SELECT * FROM Kiosk")
	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		if err := rows.Scan(&kiosk); err != nil {
			log.Fatal(err)
		}
		fmt.Println(kiosk)
	}
	defer rows.Close()
}

  • Checked if firewall is blocking needed port
  • Tried to change and connect to new ports
  • Tried to search on the internet for other solutions. Nothing has helped yet

答案1

得分: 1

服务器变量值是错误的。点(.)表示“localhost”。如果SQL服务器是本地安装的,只需使用“。”(或“.\InstanceName”-如果安装了多个实例)。如果服务器安装在网络中,请使用SERVERNAME(不带前导反斜杠)或SERVERNAME\InstanceName。

英文:

The server variable value is wrong. The dot (.) means "localhost". If the SQL server is installed locally just use "." (or ".\InstanceName" - if there are multiple instances installed). If the server is installed in the network just use SERVERNAME (without the leading backslashes), or SERVERNAME\InstanceName.

huangapple
  • 本文由 发表于 2022年11月9日 19:27:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/74374150.html
匿名

发表评论

匿名网友

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

确定