mysql.go
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package database
import (
"encoding/json"
_ "github.com/Go-SQL-Driver/MySQL"
"github.com/lunny/xorm"
"log"
"os"
)
var Engine *xorm.Engine
type confInfo struct {
Host string
Username string
Password string
Database string
}
func init() {
var err error
file, err := os.Open("./conf/conf.json")
if err != nil {
log.Println("配置文件打开错误!")
log.Fatal(err)
}
defer file.Close()
conf := confInfo{}
decoder := json.NewDecoder(file)
// str := `{"host":"rm-2zenjpu465r1ee1rd.mysql.rds.aliyuncs.com:3306","username":"icesimba_root","password":"Icesimba858680668866sdk121","database":"icesimbasdk_pro"}`
err = decoder.Decode(&conf)
if err != nil {
log.Println("配置文件解析错误!")
log.Fatal(err)
}
log.Printf("%+v", conf)
//测试数据库
// Engine, err = xorm.NewEngine("mysql", "trump:Trump123@(rm-2zenjpu465r1ee1rd.mysql.rds.aliyuncs.com:3306)/icesimbasdk_test?charset=utf8")
//正式数据库
Engine, err = xorm.NewEngine("mysql", conf.Username+`:`+conf.Password+`@(`+conf.Host+`)/`+conf.Database+`?charset=utf8`)
if err != nil {
log.Println("数据库连接错误")
log.Fatal(err)
}
if err = Engine.Ping(); err != nil {
log.Println("数据库连接失败")
log.Fatal(err)
}
}