This topic describes how to connect to OceanBase Database by using Go MySQL Driver.
Install Go MySQL Driver
Use the go get command to install Go MySQL Driver
If you use Go 1.13 to 1.16, you can run the following command to install Go MySQL Driver:
go get -u github.com/go-sql-driver/mysql
For more information about Go MySQL Driver, see Project description on Github.
Use the go install command to install Go MySQL Driver
If you cannot use the go get command due to the version or network issues, you can perform the following steps to install Go MySQL Driver:
Clone the Go MySQL Driver repository from Github to the
/srcdirectory.cd /usr/local/go/src git clone https://github.com/go-sql-driver/mysql.gitRun the
go installcommand.go install mysql
Notice
For some Go versions, the
go installcommand cannot be executed in the/srcdirectory. You can identify the correct directory based on the error reported after you run thego installcommand. For example, if the following error is reported:cannot find package "mysql" in: /usr/local/go/src/vendor/mysql, you need to put themysqlfolder in the/src/vendordirectory and then run thego installcommand.
Use Go MySQL Driver
You can set the connection string in the format of username:password@protocol(address)/dbname?param=value.
The following sample code uses the test.go file as an example:
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql" // Specify the exact installation path of Go MySQL Driver. If it is installed in the /src directory, you can specify "mysql" as the path.
)
type City struct {
Id int
Name string
Population int
}
func main() {
select_all()
}
func select_all() {
conn := "root:@tcp(127.0.0.1:2881)/testdb"
db, err := sql.Open("mysql", conn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
if err != nil {
log.Fatal(err)
}
res, err := db.Query("SELECT * FROM cities")
if err != nil {
log.Fatal(err)
}
defer res.Close()
if err != nil {
log.Fatal(err)
}
for res.Next() {
var city City
err := res.Scan(&city.Id, &city.Name, &city.Population)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", city)
}
}
Parameters
conn := "{username}:{password}@tcp({hostname}:{port})/{dbname}"
username: the connection account of the tenant. By default, the username of the tenant administrator is
root.password: the password of the connection account.
hostname: the IP address for connecting to OceanBase Database, which is usually the IP address of an OBProxy.
port: the port for connecting to OceanBase Database, which is also the listening port of the OBProxy. Default value: 2883, which can be customized.
dbname: the name of the database to be connected.
Example: conn := "root:@tcp(127.0.0.1:2881)/testdb"
After you edit the code, run the following command:
//Specify the actual installation path of Go.
export PATH=$PATH:/usr/local/go/bin
//Run the test.go file.
go run test.go
//Run the binary test file after it is generated.
go build test.go
./test
Troubleshoot the PreparedStatement error
If Go MySQL Driver reports a PreparedStatement error, log on to OceanBase Database as the root user and run the following command:
alter system set _ob_enable_prepared_statement = true;