Comments (4)
@retrychx 上代码看看。
from blog.
package models
import (
"fmt"
"log"
"server/pkg/setting"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var db *gorm.DB
func init() {
var (
err error
dbType, dbName, user, password, host, tablePrefix string
)
sec, err := setting.Cfg.GetSection("database")
if err != nil {
log.Fatal(2, "Fail to get section 'database':%v", err)
}
dbType = sec.Key("TYPE").String()
dbName = sec.Key("NAME").String()
user = sec.Key("USER").String()
password = sec.Key("PASSWORD").String()
host = sec.Key("HOST").String()
tablePrefix = sec.Key("TABLE_PREFIX").String()
fmt.Printf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
user,
password,
host,
dbName)
db, err = gorm.Open(dbType, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
user,
password,
host,
dbName))
if err != nil {
log.Println(err)
}
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return tablePrefix + defaultTableName
}
db.SingularTable(true)
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
}
func CloseDB() {
defer db.Close()
}
代码和你的教程基本一样,但是会出现上面的错误,使用终端可以连接mysql,使用navicat也能,所以有点搞不懂
from blog.
@retrychx 你可以把我的 go-gin-example 的示例代码拉下来,然后配置试试看看行不行。如果不行的话,可以关注我的公众号然后加我的微信,我帮你看看。
因为你这是个人本地环境问题,非项目本身的问题,所以我先关闭了。
from blog.
package models
import (
"fmt"
"log"
"server/pkg/setting""github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql"
)
var db *gorm.DB
func init() {
var (
err error
dbType, dbName, user, password, host, tablePrefix string
)sec, err := setting.Cfg.GetSection("database") if err != nil { log.Fatal(2, "Fail to get section 'database':%v", err) } dbType = sec.Key("TYPE").String() dbName = sec.Key("NAME").String() user = sec.Key("USER").String() password = sec.Key("PASSWORD").String() host = sec.Key("HOST").String() tablePrefix = sec.Key("TABLE_PREFIX").String() fmt.Printf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", user, password, host, dbName) db, err = gorm.Open(dbType, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", user, password, host, dbName)) if err != nil { log.Println(err) } gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string { return tablePrefix + defaultTableName } db.SingularTable(true) db.DB().SetMaxIdleConns(10) db.DB().SetMaxOpenConns(100)
}
func CloseDB() {
defer db.Close()
}代码和你的教程基本一样,但是会出现上面的错误,使用终端可以连接mysql,使用navicat也能,所以有点搞不懂
你好,请问解决了吗?
from blog.
Related Issues (20)
- 4.6 Unary and Stream interceptor一节中关于拦截器链的描述有误
- 《「连载九」将Golang应用部署到Docker》建议使用docker network
- posts/20210121-laravel-rabbitmq/ HOT 1
- zipkin-go-opentracing 问题
- posts/go/go-array-slice/ HOT 3
- 煎鱼大佬,开发个类似filebrowser那样的项目吧!普通人都能用得上。 HOT 5
- 用 Go struct 不能犯的一个低级错误!
- posts/go/grpc/2018-09-23-client-and-server/ HOT 6
- posts/go/slice-discuss/ HOT 2
- posts/go/var/ HOT 3
- 文档 [Go 泛型的 3 个核心设计,你学会了吗? ]中的Code是不是有误
- 使用go 1.18,在连载二中 添加 go.mod replace之后 出现错误 求教 HOT 3
- posts/go/map-con/ HOT 1
- 大佬在「连载二」Gin搭建Blog API's (一)里代码要更新下了
- posts/go/gin/2018-03-26-cgo/ HOT 1
- grpc超时控制
- 「连载一」Go 介绍与环境安装文章内【链接地址错误】
- posts/go/value-quote/ HOT 4
- posts/go/generics-design/ HOT 2
- posts/go/nil-func/ HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.