All Products
Search
Document Center

ApsaraDB for OceanBase:Connect to an OceanBase database by using Go MySQL Driver

Last Updated:Oct 08, 2024

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****** -Dtest

The 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/mysql

For 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:

  1. Go to the go/src directory.

    $ cd /usr/local/go/src 
    Important

    You must replace /usr/local/go/src with the installation directory of the Go language.

  2. Clone the go-sql-driver/mysql repository from Github.

    $ git clone https://github.com/go-sql-driver/mysql.git 
  3. Run the go install command.

    $ go install mysql
    Important

    For some Go versions, the go install command cannot be executed in the /src directory. You can identify the correct directory based on the error reported after you run the go install command. 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/vendor directory 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 -u option.

  • password: the tenant account password, which corresponds to the -p option.

  • hostname: the domain name of the OceanBase database to be connected, which corresponds to the -h option.

  • port: the port for connecting to the OceanBase database, which corresponds to the -P option. By default, the port is 3306 in MySQL mode.

  • dbname: the database name, which corresponds to the -D option.

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:

  1. Configure temporary environment variables according to the actual installation path of the Go package.

    $ export PATH=$PATH:/usr/local/go/bin
  2. Run code

    1. Run the go run command to execute the go file.

      $ go run test.go
    2. Run the go build command to generate a binary file.

      $ go build test.go

      Run test.

      $ ./test
  3. 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