Use the Go MySQL Driver (go-sql-driver/mysql) to connect your Go application to an AnalyticDB for MySQL cluster.
Prerequisites
Before you begin, make sure you have:
Git installed with its path added to the
PATHenvironment variableGo installed — see Download and install
Go MySQL Driver installed:
Download the driver from go-sql-driver/mysql.
Run the following command to install it to
$GOPATH:go get github.com/go-sql-driver/mysql
Connect to a cluster
The following example opens a connection to your AnalyticDB for MySQL cluster, configures the connection pool, and runs a SHOW TABLES query.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"time"
)
const (
// Account used to connect. AnalyticDB for MySQL supports privileged accounts and standard accounts.
user = "adb_test"
// Password of the account.
password = "xxx"
// IP address of the cluster. Find it on the Cluster Information page in the AnalyticDB for MySQL console.
host = "127.0.xx.xx"
// Port used to connect to the cluster.
port = 3306
// Name of the database in the cluster.
database = "database_name"
// Connection timeout.
connectTimeout = "10s"
)
func main() {
// Open the database connection.
url := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=%s", user, password, host, port, database, connectTimeout)
db, err := sql.Open("mysql", url)
if err != nil {
panic(err.Error())
}
// Maximum number of open connections. Default is 0 (unlimited).
db.SetMaxOpenConns(2)
// Maximum number of idle connections.
db.SetMaxIdleConns(1)
// Maximum lifetime of a connection (see Usage notes below).
db.SetConnMaxLifetime(time.Hour)
defer db.Close()
rows, err := db.Query("show tables")
if err != nil {
panic(err.Error())
}
for rows.Next() {
var tableName string
err := rows.Scan(&tableName)
if err != nil {
panic(err.Error())
}
fmt.Println(tableName)
}
}Enable server-side prepared statements
Use db.Prepare to create a prepared statement that the server compiles and caches.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"time"
)
const (
user = "adb_test"
password = "xxx"
host = "127.0.xx.xx"
port = 3306
database = "database_name"
connectTimeout = "10s"
)
func main() {
url := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=%s", user, password, host, port, database, connectTimeout)
db, err := sql.Open("mysql", url)
if err != nil {
panic(err.Error())
}
defer db.Close()
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(1)
db.SetConnMaxLifetime(time.Hour)
stmt, err := db.Prepare("select * from student where id = ?")
if err != nil {
panic(err.Error())
}
defer stmt.Close()
rows, err := stmt.Query(9)
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
var id, name, unit string
err := rows.Scan(&id, &name, &unit)
if err != nil {
panic(err.Error())
}
fmt.Println(fmt.Sprintf("%s, %s, %s", id, name, unit))
}
}Enable client-side prepared statements
To use client-side prepared statements, set interpolateParams=true in the connection URL. The driver then interpolates query parameters locally before sending the statement to the server.
db.Prepare and stmt.Query do not detect whether interpolateParams is set. Use db.Query to execute client-side prepared statements.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"time"
)
const (
user = "adb_test"
password = "xxx"
host = "127.0.xx.xx"
port = 3306
database = "database_name"
connectTimeout = "10s"
)
func main() {
// Set interpolateParams=true to enable client-side prepared statements.
url := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=%s&interpolateParams=true", user, password, host, port, database, connectTimeout)
db, err := sql.Open("mysql", url)
if err != nil {
panic(err.Error())
}
defer db.Close()
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(1)
db.SetConnMaxLifetime(time.Hour)
rows, err := db.Query("select * from student where id = ?", 9)
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
var id, name, unit string
err := rows.Scan(&id, &name, &unit)
if err != nil {
panic(err.Error())
}
fmt.Println(fmt.Sprintf("%s, %s, %s", id, name, unit))
}
}Usage notes
Connection pool settings
The three connection pool methods control how the database/sql package manages connections:
| Method | Default | Description |
|---|---|---|
SetMaxOpenConns(n) | 0 (unlimited) | Maximum number of open connections to the database |
SetMaxIdleConns(n) | — | Maximum number of idle connections in the pool |
SetConnMaxLifetime(d) | 0 (no limit) | Maximum lifetime of a connection |
About SetConnMaxLifetime: A connection expires after the specified duration from when it was first created, not from when it last became idle. Setting a shorter lifetime increases the frequency of new connection creation. The driver may close a connection before the lifetime expires if the connection becomes unavailable.
Server-side vs. client-side prepared statements
| Mode | How it works | When to use |
|---|---|---|
| Server-side | The server parses and caches the statement | Default behavior; use db.Prepare + stmt.Query |
| Client-side | The driver interpolates parameters before sending | Set interpolateParams=true; use db.Query |
What's next
Learn about connection parameters supported by Go MySQL Driver in the go-sql-driver/mysql documentation.
Explore AnalyticDB for MySQL SQL syntax to build queries for your application.