dbconfigdatastr字符串:用户名:密码@tcp(ip:端口)/数据名
连接数据库的那个go文件一定要引用: _ "github.com/go-sql-driver/mysql"
import ("database/sql""fmt"_ "github.com/go-sql-driver/mysql""log""net/http"
)
/*
CreateMySQLConn 用于创建数据库链接,新的方法用于数据库核心认证
*/
func CreateMySQLConn(dbconfigdatastr string) (*sql.DB, error) {//建立 mysql//fmt.Println("dbconfigdatastr", dbconfigdatastr)//formatOracleConnStr(&dbconfigdatastr)sqldbdata, err := sql.Open("mysql", dbconfigdatastr)//sqldbdata, err := sql.Open("oci8", dbconfigdatastr)if err != nil {sbjlog.Debug("sql application pool:%s\n", err)defer sqldbdata.Close()return sqldbdata, err}//测试创建链接是否成功err = sqldbdata.Ping()dbName := strings.Split(dbconfigdatastr, "/")[1]if err != nil {sbjlog.Debug("%s DB Ping err : %s\n", dbName, err)defer sqldbdata.Close()} else {sbjlog.Printf("The Sql link is successful - %s.", dbName)}return sqldbdata, err
}