我无法使用Go连接到Azure上的MySQL数据库。我看到了:
API server listening at: 127.0.0.1:XXXXX
[mysql] 2019/08/09 16:42:17 packets.go:36: read tcp XXX.XXX.X.XX:XXXXX->XX.XX.XXX.XXX:1433: read: connection reset by peer
[mysql] 2019/08/09 16:42:23 packets.go:36: read tcp XXX.XXX.X.XX:XXXXX->XX.XX.XXX.XXX:1433: read: connection reset by peer
[mysql] 2019/08/09 16:42:29 packets.go:36: read tcp XXX.XXX.X.XX:XXXXX->XX.XX.XXX.XXX:1433: read: connection reset by peer
panic: driver: bad connection
goroutine 1 [running]:
db.ConnectToDB(0x134c7aa, 0x5, 0xc0000e6000, 0x9b)
/Users/myuser/go/src/db/db-connection.go:113 +0x249
main.main()
/Users/myuser/go/src/mydbapp/main.go:12 +0x58
我尝试过https://docs.microsoft.com/en-us/azure/mysql/connect-go上的说明
package db
import (
"database/sql"
"fmt"
"os"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
func ConnectToMydb() {
mysqlInfo := fmt.Sprintf("myusername@myservernameshort:mypassword@tcp" +
"(myservenameshort.database.windows.net:1433)/mydbname?" +
"&allowNativePasswords=true&tls=true&timeout=30s"
)
db, err := sql.Open("mysql", mysqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected mydb!")
}
我的JDBC等效连接字符串如下:
jdbc:sqlserver://myservernameshort.database.windows.net:1433;database=mydbname;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30
那行得通。
如果我要走那条路线,以下链接上的说明不清楚: https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig
请指导我如何进行/如果您需要更多信息/我可以尝试
答案 0 :(得分:0)
数据库是mssql,而不是mysql。我的错。端口#应该已经放弃了