There are many examples on how to connect to a mariadb/mysql database using go/golang when only username and password is needed. But I have not found an easy example where the client needs certificates (TLS/SSL) to connect.
This works for a vanilla connection
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
// Test that db is usable
// prints current date & time to stdout
func queryDB(db *sql.DB) {
// Query the database
var result string
err := db.QueryRow("SELECT NOW()").Scan(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
func main() {
// generate connection string
cs := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", "username", "password", "dbHost", "dbPort", "database")
db, err := sql.Open("mysql", cs)
if err != nil {
log.Printf("Error %s when opening DB\n", err)
log.Printf("%s", cs)
return
}
defer db.Close()
e := db.Ping()
fmt.Println(cs, e)
queryDB(db)
}
But if the client needs certs to connect, where do I put that information?
in my my.cnf it would be theese lines:
[mysql]
## MySQL Client Configuration ##
ssl-ca=cert/ca-cert.pem
ssl-cert=cert/client-cert.pem
ssl-key=cert/client-key.pem