I have a database hosted on a platform called "hostinger", and the database is a mysql database, and my connection to it is like this: package database
import (
"database/sql"
"fmt"
"log"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
)
func Init() *sql.DB {
err := godotenv.Load(".env")
if err != nil {
log.Println("Erro ao carregar variavel de ambiente", err)
}
host := os.Getenv("HOST")
database := os.Getenv("DATABASE")
user := os.Getenv("USER")
password := os.Getenv("PASSWORD")
connection := fmt.Sprintf(`%s:%s@tcp(%s)/%s`, user, password, host, database)
db, err := sql.Open("mysql", connection)
if err != nil {
log.Println("Erro ao conectar com o banco de dados", err)
} else {
log.Println("Sucesso ao realizar conexão com banco de dados")
}
return db
}
in my controller I use the connection with mysql like this:
package controllers
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/guilherm5/database"
"github.com/guilherm5/models"
)
var DB = database.Init()
func NewUser(c *gin.Context) {
var newUser models.User
fmt.Println(DB)
err := c.ShouldBindJSON(&newUser)
if err != nil {
log.Println("Error in read body", err)
c.JSON(http.StatusBadRequest, gin.H{
"Error in read body": err.Error(),
})
return
}
query, err := DB.Prepare(`INSERT INTO usuario (nome, email, senha) VALUES (?,?,?)`)
if err != nil {
log.Println("Error in prepare query insert", err)
c.JSON(http.StatusInternalServerError, gin.H{
"Error in prepare query insert": err.Error(),
})
return
} else {
query.Exec(newUser.Nome, newUser.Email, newUser.Senha)
log.Println("Success")
c.JSON(http.StatusOK, "Success")
}
}
on my localhostb everything works as expected, I can insert users smoothly, but when I deploy the application on fly.io and try to send a request, I get the error:
{
"Error in prepare query insert": "dial tcp 127.0.0.1:3306: connect: connection refused"
}
my question is: the possibility that this error is occurring could be some firewall blocking my connection? for example the firewall where I am hosting the database is blocking the connection or Is a firewall where I'm hosting my backend blocking the connection?
thanks in advance, if you need more info I'll be happy to provide