This topic describes how to use Go to connect to an AnalyticDB for MySQL cluster.
Prerequisites
- Git is installed and its path is added to the Path environment variable.
- Go is downloaded and installed. For more information, see Download and install.
- Go MySQL Driver is installed.
- Download Go MySQL Driver from go-sql-driver/mysql.
- Use the go tool in the shell script to install the package to
$GOPATH
.go get github.com/go-sql-driver/mysql
Connect to an AnalyticDB for MySQL cluster
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
const (
// user: the account used to connect to the AnalyticDB for MySQL cluster. AnalyticDB for MySQL offers the following types of accounts: privileged accounts and standard accounts.
user = "adb_test"
// password: the password of the account used to connect to the AnalyticDB for MySQL cluster.
password = "xxx"
// host: the IP address of the AnalyticDB for MySQL cluster. You can obtain the IP address on the Cluster Information page in the AnalyticDB for MySQL console.
host = "127.0.xx.xx"
// 3306: the port used to connect to the AnalyticDB for MySQL cluster.
port = 3306
// database: the name of the database in the AnalyticDB for MySQL cluster.
database = "database_name"
// connectTimeout: the timeout period of the database connection.
connectTimeout = "10s"
)
func main() {
// Enable 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())
}
// Set the maximum number of enabled connections. The default value is 0, which indicates that no limit applies to the maximum number of enabled connections.
db.SetMaxOpenConns(2)
// Set the maximum number of idle connections.
db.SetMaxIdleConns(1)
// Set the maximum duration of connections. By default, no limit applies to the duration of connections, and connections are always kept alive.
// This setting cannot ensure that connections in the connection pool are persistent for a full hour.
// This setting is not about the timeout period for idle connections. A connection expires one hour after it is created for the first time.
// Theoretically, a shorter maximum duration of connections indicates a higher frequency of creating connections from scratch.
db.SetConnMaxLifetime(time.Hour)
// defer the close till after the main function has finished
// executing
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 a prepared statement for a server
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"time"
)
const (
// user: the account used to connect to the AnalyticDB for MySQL cluster. AnalyticDB for MySQL offers the following types of accounts: privileged accounts and standard accounts.
user = "adb_test"
// password: the password of the account used to connect to the AnalyticDB for MySQL cluster.
password = "xxx"
// host: the IP address of the AnalyticDB for MySQL cluster. You can obtain the IP address on the Cluster Information page in the AnalyticDB for MySQL console.
host = "127.0.xx.xx"
// 3306: the port used to connect to the AnalyticDB for MySQL cluster.
port = 3306
// database: the name of the database in the AnalyticDB for MySQL cluster.
database = "database_name"
// connectTimeout: the timeout period of the database connection.
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())
}
// Set the maximum number of enabled connections. The default value is 0, which indicates that no limit applies to the maximum number of enabled connections.
db.SetMaxOpenConns(2)
// Set the maximum number of idle connections.
db.SetMaxIdleConns(1)
// Set the maximum duration of connections. By default, no limit applies to the duration of connections, and connections are always kept alive.
// This setting cannot ensure that connections in the connection pool are persistent for a full hour. A connection may become unavailable and be automatically closed due to some reasons.
// This setting is not about the timeout period for idle connections. A connection does not expire after it becomes idle for one hour, but expires one hour after it is created for the first time.
// Theoretically, a shorter maximum duration of connections indicates a higher frequency of creating connections from scratch.
db.SetConnMaxLifetime(time.Hour)
defer db.Close()
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 string
var name string
var 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 a prepare statement for a client
You must set
interpolateParams
to true on the client to enable a prepare statement for Go MySQL Driver.
Notice
db.Prepare
and stmt.Query
cannot identify whether interpolateParams
is set to true. You must use db.Query
to enable a prepare statement for the client.
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"time"
)
const (
// user: the account used to connect to the AnalyticDB for MySQL cluster. AnalyticDB for MySQL offers the following types of accounts: privileged accounts and standard accounts.
user = "adb_test"
// password: the password of the account used to connect to the AnalyticDB for MySQL cluster.
password = "xxx"
// host: the IP address of the AnalyticDB for MySQL cluster. You can obtain the IP address on the Cluster Information page in the AnalyticDB for MySQL console.
host = "127.0.xx.xx"
// 3306: the port used to connect to the AnalyticDB for MySQL cluster.
port = 3306
// database: the name of the database in the AnalyticDB for MySQL cluster.
database = "database_name"
// connectTimeout: the timeout period of the database connection.
connectTimeout = "10s"
)
func main() {
// Enable the database connection.
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())
}
// Set the maximum number of enabled connections. The default value is 0, which indicates that no limit applies to the maximum number of enabled connections.
db.SetMaxOpenConns(2)
// Set the maximum number of idle connections.
db.SetMaxIdleConns(1)
// Set the maximum duration of connections. By default, no limit applies to the duration of connections, and connections are always kept alive.
// This setting cannot ensure that connections in the connection pool are persistent for a full hour. A connection may become unavailable and be automatically closed due to some reasons.
// This setting is not about the timeout period for idle connections. A connection does not expire after it becomes idle for one hour, but expires one hour after it is created for the first time.
// Theoretically, a shorter maximum duration of connections indicates a higher frequency of creating connections from scratch.
db.SetConnMaxLifetime(time.Hour)
defer db.Close()
rows, err := db.Query("select * from student where id = ?", 9)
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
var id string
var name string
var unit string
err := rows.Scan(&id, &name, &unit)
if err != nil {
panic(err.Error())
}
fmt.Println(fmt.Sprintf("%s, %s, %s", id, name, unit))
}
}