Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.