错误无法初始化数据库,出现错误无法打开与主机“localhost:1433”的 tcp 连接:拨打 tcp [::1]:1433: connectex:

问题描述 投票:0回答:2

我的环境:Windows 10 x64,go 1.20.1,Microsoft SQL Server 2014 Express 版本。

文件

vy_sql_server.go

package vy_gen_sql_server

// dsn := "sqlserver://sa:12345678@localhost:1433?database=gorm"
import (
    "gorm.io/driver/sqlserver"
    "gorm.io/gen"
    "gorm.io/gorm"
)

// Dynamic SQL
type Querier interface {
    // SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
    FilterWithNameAndRole(name, role string) ([]gen.T, error)
}

func Main() {
    g := gen.NewGenerator(gen.Config{
        OutPath: "../query",
        Mode:    gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
    })

    gormdb, _ := gorm.Open(sqlserver.Open("sqlserver://sa:12345678@localhost:1433?database=MISASME2023Sample"))
    g.UseDB(gormdb) // reuse your gorm db

    // Generate basic type-safe DAO API for struct `model.User` following conventions
    //g.ApplyBasic(model.User{})

    // Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
    //g.ApplyInterface(func(Querier) {}, model.User{}, model.Company{})

    // Generate the code
    g.Execute()
}

错误


2023/03/01 15:24:11 C:/Users/Admin/GolandProjects/vy_learn_gorm_2023_03_01/vy_gen_sql_server/vy_sql_server.go:22
[error] failed to initialize database, got error unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex: No connecti
on could be made because the target machine actively refused it.
2023/03/01 15:24:11 Start generating code.
2023/03/01 15:24:11 Generate code done.   

Process finished with the exit code 0

enter image description here

如何解决?

sql-server go go-gorm
2个回答
0
投票

您的 SQL Server 2014 是否正在侦听端口 1433?

::1
是 IPv6,可能是 SQL Server 仅使用 IPv4,尝试将其更改为
127.0.0.1


0
投票

enter image description here

    dns := "sqlserver://dovy:[email protected]:53573?database=FooSample&encrypt=disable&connection+timeout=30"

© www.soinside.com 2019 - 2024. All rights reserved.