How do I specify the server's port number with github.com/go-sql-driver/mysql?

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

How do I specify the server's port number with github.com/go-sql-driver/mysql?

问题

我正在使用以下的MySQL包:

http://godoc.org/github.com/go-sql-driver/mysql#MySQLDriver.Open

我的代码如下:

import (
  "bufio"
  "database/sql"
  _ "github.com/go-sql-driver/mysql"
)

db, err := sql.Open("mysql", "me_id:username@tcp(db1.abc.com)/dataname?timeout=2s")

但是我得到了error: dial tcp: missing port in address db1.abc.com的错误信息。

有没有办法可以在不指定端口号的情况下指定服务器?我正在将原始的Python代码移植过来,它没有端口号。

英文:

I am using the following package for MySQL

http://godoc.org/github.com/go-sql-driver/mysql#MySQLDriver.Open

And my code is:

import (
  "bufio"
  "database/sql"
  _ "github.com/go-sql-driver/mysql"
)

db, err := sql.Open("mysql", "me_id:username@tcp(db1.abc.com)/dataname?timeout=2s")

But I am getting the error message of error: dial tcp: missing port in address db1.abc.com

Is there anyway that I can specify the server without any port number?
I am porting the oroginal code in Python and it has no port number.

答案1

得分: 3

根据ANisus提到的,MySQL的默认端口是3306。

请尝试以下代码:

db, err := sql.Open("mysql", "me_id:username@tcp(db1.abc.com:3306)/dataname?timeout=2s")

看看是否解决了问题。如果没有指定端口,MySQL驱动程序似乎不会提供默认端口。

英文:

As Mentioned by ANisus, the default port for MySQL is 3306.

Please try with:

db, err := sql.Open("mysql", "me_id:username@tcp(db1.abc.com:3306)/dataname?timeout=2s")

and see if that fixes the problem. The MySQL driver doesn't seem to provide a default port if one isn't specified.

huangapple
  • 本文由 发表于 2014年4月23日 11:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/23234433.html
匿名

发表评论

匿名网友

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

确定