-
-
Save irtiza-baig-unmind/256328a07d25f955025ae6f37b1ab09c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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