TdOrm/DB/initDB.go

37 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-10-30 15:29:19 +08:00
package DB
import (
"database/sql"
_ "github.com/taosdata/driver-go/v3/taosWS"
"log"
)
var Db *sql.DB
func InitDB() {
// use
// var taosDSN = "root:taosdata@ws(localhost:6041)/dbName"
// if you want to connect a specified database named "dbName"
// 公司局域网服务器
var taosDSN = "root:taosdata@ws(192.168.151.102:6041)/test"
// 远程服务器
//var taosDSN = "root:taosdata@ws(123.249.22.213:6041)/test"
var err error
Db, err = sql.Open("taosWS", taosDSN)
if err != nil {
log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error())
}
//fmt.Println("Connected to ", db, " successfully.")
// SetMaxOpenConns sets the maximum number of open connections to the database. 0 means unlimited.
Db.SetMaxOpenConns(0)
// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
Db.SetMaxIdleConns(2)
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
Db.SetConnMaxLifetime(5)
// SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
Db.SetConnMaxIdleTime(10)
}