I have a MySQL server on CentOS 7 and I want to try connecting a program written in Go to that database server. For the database server (MySQL) is work correctly and I tried to make one database and table.
+-----+------------+
| no | nama |
+-----+------------+
| 001 | Example |
+-----+------------+
MySQL username: golang
MySQL password: cobadatabase
CentOS 7 (MySQL Server) Machine's IP: 192.168.102.175:3306
(Default Port) and My laptop's IP: 192.168.102.170
**. So, Database server exist on different machine (remotely).
Here is my Go code:
func main() {
/* Ini konekin database-nya dulu */
app_database, err := sql.Open("mysql", "golang:cobadatabase@tcp(192.168.102.175:3360)/example")
/* Nah klo ini Error Handling */
if err != nil {
log.Fatal(err) // informasi log jika koneksi database gagal
}
defer app_database.Close() // Close koneksi jika benar-benar gagal
/* End of Error Handling Connection */
var (
no string
nama string
)
baris, err := app_database.Query("SELECT * FROM example_table")
if err != nil {
log.Fatal(err)
}
defer baris.Close()
for baris.Next() {
err := baris.Scan(&no, &nama)
if err != nil {
log.Fatal(err)
}
log.Println(no, nama)
}
err = baris.Err()
if err != nil {
log.Fatal(err)
}
}
When I run my program, it fails with this error:
dial tcp 192.168.102.175:3360 connectex: A connection attempt failed because the connected party did not properly respond.