How to connect to Amazon RDS using go-sql driver

I can connect to an RDS instance using the mysql -h ... command, so I know this is not a security group problem.

I tried to use:

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

in the readme file go-sql-driver ( https://github.com/go-sql-driver/mysql ), but it does not work.

I use my username under the RDS instance instead of the identifier here.

Edit: Return error: 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.

+6
source share
2 answers

Make sure the actual error is not related to the import problem (as in issues 266 )

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

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

If the error is not directly in the Open step, but when accessing the lines, check this comment :

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

 if rows.Next() { // whatever } else { // catch error with rows.Err() } rows.Close() // <- don't forget this if you are not iterating over ALL results 
+1
source

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

 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() 
0
source

Source: https://habr.com/ru/post/975726/


All Articles