Ask Your Question
0

Is it not possible to establish a connection between the Golang container and the MySQL container?

asked 2023-07-08 04:52:27 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-08 05:11:02 +0000

devzero gravatar image

It is definitely possible to establish a connection between Golang and MySQL containers.

To do this, you need to ensure that first, your MySQL container is running and listening on the port 3306 (default MySQL port). Next, in your Golang code, you need to set up a connection to the MySQL container's IP address and port number.

Here's an example code snippet of how you can establish a MySQL connection in Golang:

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    // Set up a connection string
    connectionString := "root:password@tcp(mysql_container_ip:3306)/database_name"

    // Open the connection
    db, err := sql.Open("mysql", connectionString)
    if err != nil {
        fmt.Println(err.Error())
    }

    // Close the connection
    defer db.Close()
}

In the above code, replace mysql_container_ip with the IP address of your MySQL container, and database_name with the name of the database you want to connect to.

Once you run this code, you should be able to establish a connection and query your MySQL database from the Golang container.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-08 04:52:27 +0000

Seen: 12 times

Last updated: Jul 08 '23