Skip to content

Instantly share code, notes, and snippets.

@irtiza-baig-unmind
Created April 13, 2023 17:01
Show Gist options
  • Save irtiza-baig-unmind/256328a07d25f955025ae6f37b1ab09c to your computer and use it in GitHub Desktop.
Save irtiza-baig-unmind/256328a07d25f955025ae6f37b1ab09c to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
)
func main() {
c := mysql.Config{
User: "REDACTED",
Passwd: "PWD",
Net: "tcp",
Addr: "RDSURL:3306",
TLSConfig: "true",
AllowCleartextPasswords: true,
}
fmt.Println(c.FormatDSN())
fmt.Println(constructed)
constructed := fmt.Sprintf("%s:%s@tcp(%s)/?tls=true&allowCleartextPasswords=true", c.User, c.Passwd, c.Addr)
db, err := sql.Open("mysql", c.FormatDSN()) // the formatted DSN throws the error we see from terraform, however using the constructed variable works fine
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
results, err := db.Query("SHOW DATABASES;")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// defer the close till after the main function has finished
// executing
defer db.Close()
fmt.Println(results)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment