英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论