0

I have been learning golang in the last couple days, but I am so frustrated because I am trying to make a simple CRUD api and I can't create a simple user.

I am using Fiber (very similar to Express.js) with golang's sql with mysql driver.

Here is my entire handler:

func CreateUser(c *fiber.Ctx) {
type InputData struct {
    Username string `json:"username"`
    Email    string `json:"email"`
    Password string `json:"password"`
}
// input data
input := new(InputData)
c.BodyParser(input)
// connects to db
db, err := sql.Open("mysql", "root:root@tcp(localhost:8877)/sql")
if err != nil {
    log.Fatal(err)
}
// the code freezes here
if err := db.Ping(); err != nil {
    log.Fatal(err)
}
fmt.Println("Connected to the MySQL database! ")
// defer will close the connection when the main function has finished
defer db.Close()
// inserts the user
prep, err := db.Prepare("INSERT INTO users (username, email, password) VALUES (?,?,?)")
if err != nil {
    panic(err.Error())
}
result, err := prep.Exec(input.Username, input.Email, input.Password)
if err != nil {
    panic(err.Error())
}
// response
c.JSON(result)
}

Am I doing something I shouldn't be?

(btw, this is almost my entire code, I removed the "err" handling)

Have a nice day guys!

learnbydoing
  • 309
  • 1
  • 5
  • 14
  • 2
    What does the error say? – mkopriva Sep 06 '20 at 13:10
  • When I test it with the Insomnia Client it just "doesn't stop" the process. I have a log saying that the database is connected. But the problem starts from the db.Prepare. – learnbydoing Sep 06 '20 at 13:12
  • Are you doing anything with the errors, or just ignoring them? – Marc Sep 06 '20 at 13:15
  • 3
    *"But the problem starts from the db.Prepare."*? What is the problem? What starts there? Are you saying your program just hangs at the db.Prepare line? Or you're saying that db.Prepare returns an error? If so what does the error say? Also after you connected to the db with sql.Open did you try db.Ping? If not try it, do you get back an error? If so what does it say? – mkopriva Sep 06 '20 at 13:15
  • 1
    And *please* include the full code of the handler, the code you've shown so far seems ok, everything else you've considered unrelated and omitted from the question may actually be the cause of the problem. – mkopriva Sep 06 '20 at 13:20
  • Everything works fine until the code reaches the prep. I don't have any error, the request just takes a long time to finnish but I never have an error. (I hope I am being clear ) – learnbydoing Sep 06 '20 at 13:27
  • What does `Ping` say? A successful [Open](https://golang.org/pkg/database/sql/#Open) does not necessarily mean connected: *"Open may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping."* – mkopriva Sep 06 '20 at 13:31
  • I didn't have Ping in my code. I added it, and now, the same problem persists. But it only reaches the Ping line of code and "freezes" the request. – learnbydoing Sep 06 '20 at 13:37
  • Is the database server running? Is the connection string correct? Do you have a database called `sqlx` setup on your server? Can you connect to that database, using the same string, with a different app? perhaps the mysql client, or some GUI app? – mkopriva Sep 06 '20 at 13:41
  • I can connect to the database using phpmyadmin. – learnbydoing Sep 06 '20 at 13:42
  • Using the same exact string? With no additional query parameters? – mkopriva Sep 06 '20 at 13:43
  • With the exact username, password, host, port and database. – learnbydoing Sep 06 '20 at 13:45
  • assuming you use the latest version of mySQL - at some point new protocol was introduced, maybe try to disable that using `mysqlx=0` param. Another thing to try is to configure auth plugin to be native password. – ain Sep 06 '20 at 14:12
  • You'll need to examine the environment in which the db runs and the configuration of the db server, and you'll need to make sure that *multiple* clients can connect, opening *multiple* connections, without any issues. Also check the documentation for the driver you're using and see if it has something written on how to connect and if it does, read it thoroughly to see if there aren't any gotchas. If you can't find anything obvious check the driver's "issues" page on github to see if there isn't someone with the same problem as you. – mkopriva Sep 06 '20 at 14:15
  • What happens if you try to access the table from the command line in a terminal, like this: mysql -u root -proot --host localhost --port 8877 -e 'select * from sql.users'; – Peter Gloor Sep 07 '20 at 13:27
  • If you can access via an phpadmin portal - but not from code - and the open hangs, I'd guess a firewall is in place. Can you telnet to the mysql server/port. If that hangs, then this is a network path issue. – colm.anseo Sep 07 '20 at 14:38
  • @colm.anseo yes it was my firewall that was blocking the port. Thank you! – learnbydoing Sep 07 '20 at 18:20
  • Thank you all for helping me out, by reying all your suggestions I learned a bunch! – learnbydoing Sep 07 '20 at 18:21

0 Answers0