Go and Compose for MySQL
Go has a generic interface for SQL databases and using it with the go-sql-driver will provide us with several options to connect to MySQL. The problem with the driver is that we cannot verify the self-signed certificate. The only TLS/SSL option we have with self-signed certificates is skip-verify
, which is less than optimal.
To connect to MySQL using Go, first install the go-sql-driver package to your $GOPATH
:
go get github.com/go-sql-driver/mysql
You can use the following code to connect to MySQL.
package main
import (
"database/sql"
"log"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "admin:[email protected](aws-us-east-1-portal.23.dblayer.com:15918)/compose?tls=skip-verify")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SHOW DATABASES")
if err != nil {
log.Fatal(err)
}
var dbNames string
for rows.Next() {
err := rows.Scan(&dbNames)
if err != nil {
log.Fatal(err)
}
fmt.Println(dbNames)
}
}
Still Need Help?
If this article didn't solve things, summon a human and get some help!
Updated over 3 years ago