Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to open tcp connection with host #762

Closed
hajsf opened this issue Jul 18, 2022 · 2 comments
Closed

unable to open tcp connection with host #762

hajsf opened this issue Jul 18, 2022 · 2 comments

Comments

@hajsf
Copy link

hajsf commented Jul 18, 2022

I goth the below error:

2022/07/18 10:15:38 Prepare failed:unable to open tcp connection with host 'my-server:1433': dial tcp 172.16.20.185:1433: connectex: No connection could be made because the target machine actively refused it.

Though that I did the following:

  1. Installed fresh version of MSSQL dev, first time MSSQL is installed at this machine, and confirmed it is running using sqlcm

image

  1. Installed MSSQL studio (I'm to confirm MSSQLSERVER) is connected

image

  1. At Windows definder firewall, I allowed the connection of port 1433

image

  1. At SQL server network connection -> protocols for MSSQLSERVER I enabled both Named Pipes and TCP/IP

image

The code I'm trying to test is the simple one provided in the examples:

package main

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

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

var (
	debug         = flag.Bool("debug", false, "enable debugging")
	password      = flag.String("password", "", "the database password")
	port     *int = flag.Int("port", 1433, "the database port")
	server        = flag.String("server", "my-server", "the database server") 
	user          = flag.String("user", "sa", "the database user")                  
)

func main() {
	flag.Parse()

	if *debug {
		fmt.Printf(" password:%s\n", *password)
		fmt.Printf(" port:%d\n", *port)
		fmt.Printf(" server:%s\n", *server)
		fmt.Printf(" user:%s\n", *user)
	}

	connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
	if *debug {
		fmt.Printf(" connString:%s\n", connString)
	}
	conn, err := sql.Open("mssql", connString)
	if err != nil {
		log.Fatal("Open connection failed:", err.Error())
	}
	defer conn.Close()

	stmt, err := conn.Prepare("select 1, 'abc'")
	if err != nil {
		log.Fatal("Prepare failed:", err.Error())
	}
	defer stmt.Close()

	row := stmt.QueryRow()
	var somenumber int64
	var somechars string
	err = row.Scan(&somenumber, &somechars)
	if err != nil {
		log.Fatal("Scan failed:", err.Error())
	}
	fmt.Printf("somenumber:%d\n", somenumber)
	fmt.Printf("somechars:%s\n", somechars)

	fmt.Printf("bye\n")
}
@dainiauskas
Copy link

Replace my-server to IP address, bug in IP address resolve from name

@kardianos
Copy link
Collaborator

This isn't an issue with the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants