英文:
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,OPTIONSDBNAME/USER/PASSWD
// tcp:ADDRDBNAME/USER/PASSWD
// tcp:ADDR,OPTIONSDBNAME/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/。
有几点可能会有所帮助:
sql.Open()
的连接字符串采用 DSN 格式。对于我的连接,db, err := sql.Open("mysql", "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")
是有效的。请检查 TCP 连接中括号的使用方式。- 你正在使用的驱动程序有一个名为
mysql.New()
的命令,可以使用你上面列出的格式打开连接:db := mysql.New(proto, "", addr, user, pass, dbname)
- 令人困惑的是,
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:
- The connection string for
sql.Open()
is in DSN format.db, err := sql.Open("mysql", "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")
works for me for my connections. Check the use of parenthesis for TCP connections. - 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, "", addr, user, pass, dbname)
- Confusingly,
sql.Open
doesn't actually open a connection, it just creates a db resource. You can verify that it's working by runningdb.Ping()
答案2
得分: 15
以下是翻译好的内容:
以下语句适用于我:
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
db, err := sql.Open("mysql", "db_user:password@tcp(localhost:3306)/my_db")
)
英文:
Following statement works for me:
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
db, err := sql.Open("mysql", "db_user:password@tcp(localhost:3306)/my_db")
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论