This topic describes how to connect to and use an OceanBase database by using Go MySQL Driver.
Prerequisites
You have deployed a Go environment on your computer.
Use Go MySQL Driver to connect to a database
Step 1: Obtain the database connection parameters
For more information, see Obtain the connection parameters. Example:
$ obclient -hxxx.xxx.xxx.xxx -P3306 -u a**** -p****** -DtestThe database connection parameters specify the information required to access the database. You can verify the database connection parameters by logging on to the database before using them in the sample code.
Options:
-h: the domain name of the OceanBase database to be connected.
-P: the port for connecting to the OceanBase database. By default, the port is 3306 in MySQL mode.
-u: the tenant account.
-p: the account password.
-D: the database name.
Step 2: Install Go-SQL-Driver/MySQL
You can install Go MySQL Driver by using different methods based on the version of the Go language.
Run the go get command (for Go 1.13 to 1.16 only)
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/mysqlFor more information about Go-SQL-Driver/MySQL, see GitHub.
Run the go install command
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:
Go to the
go/srcdirectory.$ cd /usr/local/go/srcImportantYou must replace
/usr/local/go/srcwith the installation directory of the Go language.Clone the
go-sql-driver/mysqlrepository from Github.$ git clone https://github.com/go-sql-driver/mysql.gitRun the
go installcommand.$ go install mysqlImportantFor 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 must put the mysql folder in the/src/vendordirectory and then run the go install command.
Step 3: Write the sample code
The following test.go sample file is for your reference:
package main
import (
"database/sql"
"fmt"
"log"
_ "mysql"
// Specify the installation path of Go MySQL Driver. If it is installed in the /src directory, you can specify "mysql" as the path.
)
type Str struct {
Name string
}
func main() {
select_all()
}
func select_all() {
conn := "a****:******@tcp(xxx.xxx.xxx.xxx:3306)/test"
db, err := sql.Open("mysql", conn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("success to connect OceanBase with go_mysql driver\n")
// Create a table named t1.
db.Query("create table t1(str varchar(256))")
// Insert data.
db.Query("insert into t1 values ('Hello OceanBase')")
// Query data.
res, err := db.Query("SELECT * FROM t1")
// Drop the t1 table.
db.Query("drop table t1")
if err != nil {
log.Fatal(err)
}
defer res.Close()
if err != nil {
log.Fatal(err)
}
for res.Next() {
var str Str
res.Scan(&str.Name)
fmt.Printf("%s\n", str.Name)
}
}
Modify the database connection parameters in the code. Refer to the following parameters and format. The parameter values are obtained in Step 1.
conn := "{username}:{password}@tcp({hostname}:{port})/{dbname}"Parameters:
username: the tenant account, which corresponds to the
-uoption.password: the tenant account password, which corresponds to the
-poption.hostname: the domain name of the OceanBase database to be connected, which corresponds to the
-hoption.port: the port for connecting to the OceanBase database, which corresponds to the
-Poption. By default, the port is 3306 in MySQL mode.dbname: the database name, which corresponds to the
-Doption.
Sample code:
conn := "a****:******@tcp(xxx.xxx.xxx.xxx:3306)/test"Step 4: Execute the sample code
After you edit the code, run the following command:
Configure temporary environment variables according to the actual installation path of the Go package.
$ export PATH=$PATH:/usr/local/go/binRun code
Run the go run command to execute the go file.
$ go run test.goRun the go build command to generate a binary file.
$ go build test.goRun test.
$ ./test
If the following result is returned, the database is connected and the sample statement is executed correctly.
success to connect OceanBase with go_mysql driver Hello OceanBase