Python社区  »  DATABASE

golang mysql连接字符串与TLS连接错误

PeggyScott • 1 周前 • 24 次点击  

我无法使用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

请指导我如何进行/如果您需要更多信息/我可以尝试什么

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50706
 
24 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 1 周前
PeggyScott
Reply   •   1 楼
PeggyScott    3 月前

数据库是mssql而不是mysql。我的错。港口应该把它给人