我正在尝试使用以下软件包将:arg1
上的VM(Debain 9)服务器上的go应用程序连接到mysql数据库上
查看连接功能:
:arg2
我假设我输入了在GCP实例连接数据库页面上找到的实例“实例连接名称”: ` 包主
github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql
Where I find my instance connection name
我得到一个错误:
// Cfg returns the effective *mysql.Config to represent connectivity to the
// provided instance via the given user and password. The config can be
// modified and passed to DialCfg to connect. If you don't modify the returned
// config before dialing, consider using Dial or DialPassword.
func Cfg(instance, user, password string) *mysql.Config {
我已经将VM实例IP连接到数据库中的Authorized网络,所以我不知道我是否在此软件包中使用了正确的实例或用户。
答案 0 :(得分:0)
就我而言,问题是由于 "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
软件包的版本较低。使用命令 go get -v -u github.com/GoogleCloudPlatform/cloudsql-proxy
升级包解决了该问题。下面是代码片段
"database/sql"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
connString := "user:password@cloudsql(project_id:region:instance)/dbname?charset=utf8&parseTime=True&loc=Local"
db, err := sql.Open("mysql", connectionString)