youtu_ecpm/pkg/db/gorm.go
2025-01-20 00:14:43 +08:00

30 lines
647 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package db
import (
"fmt"
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/pkg/config"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var Db *gorm.DB
func init() {
//配置MySQL连接参数
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
config.GetDBUserName(), //账号
config.GetDBPassword(), //密码
config.GetDBHost(), //数据库地址可以是Ip或者域名
config.GetDBPort(), //数据库端口
config.GetDBName(), //数据库名
)
//创建数据库连接
db, err := gorm.Open(mysql.Open(dsn))
if err != nil {
panic("连接数据库失败, error=" + err.Error())
}
Db = db
}