Connect to OceanBase Database by using Go MySQL Driver

2023-07-21 09:11:01  Updated

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:

  1. Clone the Go MySQL Driver repository from Github to the /src directory.

    cd /usr/local/go/src   
    git clone https://github.com/go-sql-driver/mysql.git
    
  2. Run the go install command.

    go install mysql
    

Notice

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 returned after you run the go install command. For example, put the mysql folder in the /src/vendor directory and then run the go install command if the following error is returned: cannot find package "mysql" in: /usr/local/go/src/vendor/mysql.

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 takes 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 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 accessed.

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 file after it is generated.
go build test.go
./test

Troubleshoot the PreparedStatement error

If Go MySQL Driver returns 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;

Contact Us