如何打开远程MySQL连接?

huangapple go评论78阅读模式
英文:

How to open a remote mysql connection?

问题

我正在尝试使用Go语言和database/sql包连接到远程MySQL数据库。
我发现go/mysql的文档令人困惑。似乎没有一个单一的示例来连接到远程主机。就好像每个人都在使用localhost一样。
到目前为止,我有以下代码:

import (
	"database/sql"
	_ "github.com/ziutek/mymysql/godrv"
)

db, err := sql.Open("mymysql", "tcp:"+dbHost+dbName+"/"+user+"/"+pass)
defer db.Close()

根据https://github.com/ziutek/mymysql的文档:

[PROTOCOL_SPECIFIC*]DBNAME/USER/PASSWD
//
// 其中协议特定部分可以为空(这意味着使用默认协议连接到本地服务器)。目前可能的形式有:
// DBNAME/USER/PASSWD
// unix:SOCKPATHDBNAME/USER/PASSWD
// unix:SOCKPATH,OPTIONS
DBNAME/USER/PASSWD
// tcp:ADDRDBNAME/USER/PASSWD
// tcp:ADDR,OPTIONS
DBNAME/USER/PASSWD

我还尝试了以下代码:

db, err := sql.Open("mymysql", "tcp:"+dbHost, dbName+"/"+user+"/"+pass)

但是它也不起作用。整个语法看起来很晦涩。

英文:

I'm trying to connect to remote mysql database using go and the database/sql package.
I find the go/mysql documentation confusing. It seems there is no single example how to connect to a remote host. Like everyone would use localhost.
So far I have this

   import (
    	"database/sql"
    	_ "github.com/ziutek/mymysql/godrv"    
    	db, err := sql.Open("mymysql", "tcp:"+dbHost*dbName+"/"+user+"/"+pass)
    	defer db.Close()

Based on the docs from https://github.com/ziutek/mymysql

[PROTOCOL_SPECFIIC*]DBNAME/USER/PASSWD
//
// where protocol specific part may be empty (this means connection to
// local server using default protocol). Currently possible forms:
//   DBNAME/USER/PASSWD
//   unix:SOCKPATH*DBNAME/USER/PASSWD
//   unix:SOCKPATH,OPTIONS*DBNAME/USER/PASSWD
//   tcp:ADDR*DBNAME/USER/PASSWD
//   tcp:ADDR,OPTIONS*DBNAME/USER/PASSWD

I also tried

 db, err := sql.Open("mymysql", "tcp:"+dbHost, dbName+"/"+user+"/"+pass) 

and it's not working either. The whole syntax seems cryptic.

答案1

得分: 49

这些网站对于理解Go SQL非常有帮助:https://github.com/go-sql-driver/mysql/(即使你使用不同的驱动程序)和http://go-database-sql.org/。

有几点可能会有所帮助:

  1. sql.Open() 的连接字符串采用 DSN 格式。对于我的连接,db, err := sql.Open("mysql", "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>") 是有效的。请检查 TCP 连接中括号的使用方式。
  2. 你正在使用的驱动程序有一个名为 mysql.New() 的命令,可以使用你上面列出的格式打开连接:db := mysql.New(proto, "", addr, user, pass, dbname)
  3. 令人困惑的是,sql.Open 实际上并不打开连接,它只是创建一个数据库资源。你可以通过运行 db.Ping() 来验证它是否工作。
英文:

These sites are both really helpful in understanding Go SQL: https://github.com/go-sql-driver/mysql/ (even if you are using a different driver) and http://go-database-sql.org/

There are a few things that might help:

  1. The connection string for sql.Open() is in DSN format. db, err := sql.Open(&quot;mysql&quot;, &quot;&lt;username&gt;:&lt;pw&gt;@tcp(&lt;HOST&gt;:&lt;port&gt;)/&lt;dbname&gt;&quot;) works for me for my connections. Check the use of parenthesis for TCP connections.
  2. The driver you are using has the command mysql.New() which can open connections using the format you've listed above: db := mysql.New(proto, &quot;&quot;, addr, user, pass, dbname)
  3. Confusingly, sql.Open doesn't actually open a connection, it just creates a db resource. You can verify that it's working by running db.Ping()

答案2

得分: 15

以下是翻译好的内容:

以下语句适用于我:

import (
	&quot;database/sql&quot;
	_ &quot;github.com/go-sql-driver/mysql&quot;
)

db, err := sql.Open(&quot;mysql&quot;, &quot;db_user:password@tcp(localhost:3306)/my_db&quot;)

)

英文:

Following statement works for me:

import (
	&quot;database/sql&quot;
	_ &quot;github.com/go-sql-driver/mysql&quot;
)

db, err := sql.Open(&quot;mysql&quot;, &quot;db_user:password@tcp(localhost:3306)/my_db&quot;)

)

huangapple
  • 本文由 发表于 2014年5月9日 03:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/23550453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定