error: failed to initialize database, got error dial tcp :0: connectex: The requested address is not valid in its context

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

error: failed to initialize database, got error dial tcp :0: connectex: The requested address is not valid in its context

问题

我正在尝试使用Go语言创建一个API,但是遇到了一个错误。如何解决这个问题?我将发布错误文本。

错误信息如下:
错误:初始化数据库失败,出现错误 dial tcp :0: connectex: 请求的地址在其上下文中无效

初始化数据库失败,出现错误 dial tcp :0: connectex: 请求的地址在其上下文中无效。
panic: DB错误:dial tcp :0: connectex: 请求的地址在其上下文中无效。

goroutine 1 [running]:
ToDoList-Server/model.DBConnection()
        C:/Users/81804/enviroment/todo/ToDoList-Server/model/db.go:21 +0x225
main.main()
        C:/Users/81804/enviroment/todo/ToDoList-Server/main.go:10 +0x2a

package model

import (
	"database/sql"
	"fmt"
	"os"

	_ "github.com/go-sql-driver/mysql"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB

// DBConnection函数用于连接数据库并创建表
func DBConnection() *sql.DB {
	dsn := GetDBConfig()
    var err error
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(fmt.Errorf("DB错误:%w", err))
	}
	CreateTable(db)
	sqlDB, err := db.DB()
	if err != nil {
		panic(fmt.Errorf("DB错误:%w", err))
	}
	return sqlDB
}

// 获取数据库配置信息
func GetDBConfig() string {
	user := os.Getenv("DB_USERNAME")
	password := os.Getenv("DB_PASSWORD")
	hostname := os.Getenv("DB_HOSTNAME")
	port := os.Getenv("DB_PORT")
	dbname := os.Getenv("DB_DBNAME")

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, hostname, port, dbname) + "?charset=utf8mb4&parseTime=True&loc=Local"
	return dsn
}

// 创建Task表
func CreateTable(db *gorm.DB) {
	db.AutoMigrate(&Task{})
}

希望这能帮到你解决问题!

英文:

I am trying to create an API in go language, but I get an error. How can this be resolved? I will post the error text.
error: failed to initialize database, got error dial tcp :0: connectex: The requested address is not valid in its context

failed to initialize database, got error dial tcp :0: connectex: The requested address is not valid in its context.
panic: DB Error: dial tcp :0: connectex: The requested address is not valid in its context.

goroutine 1 [running]:
ToDoList-Server/model.DBConnection()
        C:/Users/81804/enviroment/todo/ToDoList-Server/model/db.go:21 +0x225
main.main()
        C:/Users/81804/enviroment/todo/ToDoList-Server/main.go:10 +0x2a

package model

import (
	"database/sql"
	"fmt"
	"os"

	_ "github.com/go-sql-driver/mysql"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB

// DB接続とテーブルを作成する
func DBConnection() *sql.DB {
	dsn := GetDBConfig()
    var err error
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(fmt.Errorf("DB Error: %w", err))
	}
	CreateTable(db)
	sqlDB, err := db.DB()
	if err != nil {
		panic(fmt.Errorf("DB Error: %w", err))
	}
	return sqlDB
}

// DBのdsnを取得する
func GetDBConfig() string {
	user := os.Getenv("DB_USERNAME")
	password := os.Getenv("DB_PASSWORD")
	hostname := os.Getenv("DB_HOSTNAME")
	port := os.Getenv("DB_PORT")
	dbname := os.Getenv("DB_DBNAME")

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, hostname, port, dbname) + "?charset=utf8mb4&parseTime=True&loc=Local"
	return dsn
}

// Task型のテーブルを作成する
func CreateTable(db *gorm.DB) {
	db.AutoMigrate(&Task{})
}

答案1

得分: 2

请尝试按照以下方式格式化您的 DSN 字符串:

dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s post=%s", hostname, user, password, dbname, port)
英文:

try to format your dsn string as follows:

dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s post=%s", hostname, user, password, dbname, port)

huangapple
  • 本文由 发表于 2022年6月22日 08:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/72708471.html
匿名

发表评论

匿名网友

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

确定