27 lines
619 B
Go
27 lines
619 B
Go
package db
|
||
|
||
import (
|
||
"fmt"
|
||
"gorm.io/driver/mysql"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
var Db *gorm.DB
|
||
|
||
func init() {
|
||
//配置MySQL连接参数
|
||
username := "root" //账号
|
||
password := "youtu!0113" //密码
|
||
host := "localhost" //数据库地址,可以是Ip或者域名
|
||
port := 3306 //数据库端口
|
||
Dbname := "ecpm" //数据库名
|
||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, Dbname)
|
||
fmt.Println(dsn)
|
||
db, err := gorm.Open(mysql.Open(dsn))
|
||
if err != nil {
|
||
panic("连接数据库失败, error=" + err.Error())
|
||
}
|
||
Db = db
|
||
|
||
}
|