I'm making a Golang API to be able to interact with a SQL database from my Android application. The problem I have is the following:
I have a sql database hosted on https://phpmyadmin.ovh.net/
I'm trying to connect my locale API to this one. According to the documentation:
func Open(driverName, dataSourceName string) (*DB, error)
The driverName params would be "mysql" isn't it? But, what would be the dataSourceName params?
I saw lot of post about Open, however, I don't think I understand how to organize the url string about the dataSourceName params.
package main
import (
"net/http"
"database/sql"
"fmt"
)
var db *sql.DB // global variable to share it between main and the HTTP handler
func main() {
fmt.Println("starting up")
db, err := sql.Open("mysql", "????????????")
if err != nil {
fmt.Println("Error on initializing database connection: %s", err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println("Error on opening database connection: %s", err.Error())
}
http.HandleFunc("/", hello)
http.ListenAndServe(":8080", nil)
}
Thank for read & help !
/!\ EDIT /!\
So, after your both answers, I tried the code below:
package main
import (
"fmt"
"net/http"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type databaseinfos struct {
user string
password string
name string
address string
port string
url string
}
var db *sql.DB // global variable to share it between main and the HTTP handler
var dbi *databaseinfos
func main() {
dbi := databaseinfos{
user: "Emixam23",
password: "",
name: "mydb",
address: "127.0.0.1",
port: "3306",
url: ""}
dbi.url = (dbi.user + ":" + dbi.password + "@tcp(" + dbi.address + ":" + dbi.port + ")/" + dbi.name)
fmt.Println(dbi.url)
db, err := sql.Open("mysql", dbi.url)
if err != nil {
fmt.Println("Error on initializing database connection: %s", err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println("Error on opening database connection: %s", err.Error())
}
/*http.HandleFunc("/", hello)*/
http.ListenAndServe(":8080", nil)
}
However, I got an error which is "Error on opening database connection: %s dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it.".
Does the problem comes because of my computer or because of the dataSourceName params?
mysql -u <username> -p<password> -h <host> <database>