How to connect to Amazon RDS using go-sql-driver

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

How to connect to Amazon RDS using go-sql-driver

问题

我可以使用mysql -h ...命令连接到RDS实例,所以我知道这不是一个安全组的问题。

我尝试使用以下代码:

sql.Open("mysql", "id:password@tcp(your-amazonaws-uri.com:3306)/dbname")

在go-sql-driver的readme文件中(https://github.com/go-sql-driver/mysql),但似乎不起作用。

不过,在这里我使用的是RDS实例下的用户名,而不是id。

编辑:
返回的错误是:panic runtime error: invalid memory address or nil pointer deference [signal 0xb code=0x1 addr=0x20 pc=0x5b551e] goroutine 16 [running] runtime.panic(0x7d4fc0, 0xa6ca73)...database/sql.(*Rows).Next...

在我的本地数据库中运行正常。

英文:

I can connect to the RDS instance using mysql -h ... command so I know it's not a security group problem.

I've tried to use:

sql.Open("mysql", "id:password@tcp(your-amazonaws-uri.com:3306)/dbname")

in the readme file of go-sql-driver(https://github.com/go-sql-driver/mysql), but it doesn't seem to work.

I'm using my username under the RDS instance instead of id here though.

Edit:
The error returned is: panic runtime error: invalid memory address or nil pointer deference [signal 0xb code=0x1 addr=0x20 pc=0x5b551e]
goroutine 16 [running]
runtime.panic(0x7d4fc0, 0xa6ca73)...database/sql.(*Rows).Next...

It works fine with my local DB.

答案1

得分: 4

sql.Open()的连接字符串采用DSN格式。

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

db, err := sql.Open("mysql", "<username>:<password>@tcp(<AWSConnectionEndpoint>:<port>)/<dbname>")

if err != nil {
    fmt.Print(err.Error())
}

defer db.Close()
英文:

The connection string for sql.Open() is in DSN format.

import (
	&quot;database/sql&quot;
    &quot;fmt&quot;

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

db, err := sql.Open(&quot;mysql&quot;, &quot;&lt;username&gt;:&lt;password&gt;@tcp(&lt;AWSConnectionEndpoint &gt;:&lt;port&gt;)/&lt;dbname&gt;&quot;)

if err != nil {
    fmt.Print(err.Error())
}

defer db.Close()

答案2

得分: 1

确保实际错误与导入问题无关(如问题266中所述)。

检查以下内容以确保您正在使用最新版本(如此问题中所述):

  • 您的Go-MySQL-Driver版本(或git SHA)
  • 您的Go版本(在控制台中运行go version

如果错误不是直接在Open步骤中出现,而是在访问Rows时出现,请检查此评论

> 使用for循环(for rows.Next() { ... })或类似以下方式:

if rows.Next() {
     // 具体操作
} else {
     // 使用rows.Err()捕获错误
}
rows.Close() // &lt;- 如果您不迭代所有结果,请不要忘记这一步
英文:

Make sure the actual error isn't related to an import issue (as in issues 266)

Check (to be sure you are using the latest versions, as in this issue):

  • your Go-MySQL-Driver version (or git SHA)
  • your Go version (run go version in your console)

If the error isn't directly in the Open step, but when accessing the Rows, check this comment out:

> Use either a for loop (for rows.Next() { ... }) or something like this:

if rows.Next() {
     // whatever
} else {
     // catch error with rows.Err()
}
rows.Close() // &lt;- don&#39;t forget this if you are not iterating over ALL results

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

发表评论

匿名网友

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

确定