我正在通过私有IP将我的go应用程序连接到cloudsql-mysql。下面是我的代码
package main
import (
"context"
"database/sql"
"fmt"
"log"
"net"
"sync"
"cloud.google.com/go/cloudsqlconn"
"github.com/go-sql-driver/mysql"
)
const instanceURI = "%s:%s:%s"
func main(){
instance := fmt.Sprintf(instanceURI, db.Project, db.GCRegion, db.Instance)
d, err := cloudsqlconn.NewDialer(context.Background(), cloudsqlconn.WithOptions(cloudsqlconn.WithCredentialsJSON(db.ServiceAccount)))
if err != nil {
log.Println(err)
}
mysql.RegisterDialContext("cloudsqlconn",
func(ctx context.Context, addr string) (net.Conn, error) {
return d.Dial(ctx, instance, cloudsqlconn.WithPrivateIP())
})
dbURI := fmt.Sprintf("%s:%s@cloudsqlconn(%s)/%s?parseTime=true",
db.User, db.Password, instance, db.DbName)
dbPool, err := sql.Open("mysql", dbURI)
if err != nil {
log.Println(err.Error())
}
err = dbPool.PingContext(context.Background())
if err != nil {
log.Printf("Errors %s pinging DB", err)
}
}
我得到了下面的错误。unexpect resp from server for caching_sha2_password perform full authentication
有人能帮我解释一下错误的原因吗?
我按照下面的文档https://cloud.google.com/sql/docs/mysql/connect-connectors
1条答案
按热度按时间xfb7svmp1#
看起来您已连接到数据库,但授权失败(源代码)。
您是否尝试过使用相同的用户名和密码直接连接?