All Products
Search
Document Center

ApsaraDB for OceanBase:Connect to OceanBase Database by using the Go client

Last Updated:Dec 05, 2023

This topic describes how to connect to OceanBase Database by using the Go client in proxy mode.

Prerequisites

  • You have downloaded the Go client of the latest version from the GitHub Go client repository.

  • You have imported the obkv-table-client-go dependency. You can run the following command in the local Go client repository to obtain the obkv-table-client-go dependency of the latest or specified version:

    go get github.com/oceanbase/obkv-table-client-go

Connection methods

ApsaraDB for OceanBase allows you to connect to a database in proxy mode. Specifically, you can call a function or use a TOML file to connect to the database.

Method 1: Call a function

Call the following function in your Go code and modify parameters based on description in the following tables:

func NewOdpClient(
	fullUserName string,
	passWord string,
	odpIP string,
	odpRpcPort int,
	database string,
	cliConfig *config.ClientConfig) (Client, error)
  • Parameters

    Parameter

    Description

    fullUserName

    The full username in the format of <account name>@<tenant name>#<cluster name>.

    passWord

    The password of the account in fullUserName.

    odpIP

    The domain name of the OceanBase database to be connected.

    odpRpcPort

    The service port for the wide table engine (or Key-Value mode) of OceanBase Database. Default value: 3307.

    database

    The name of the database.

    cliConfig

    The client configuration. For more information, see ClientConfig.

Method 2: Use a TOML file

  1. Customize your TOML configuration file based on the template file configurations/obkv-table-default.toml. Set the mode to proxy, modify OdpClientConfig, and configure other parameters based on the actual business needs. For more information, see ClientConfig.

  2. Call the following function in your Go code and enter the address or location of your TOML file:

    func NewClientWithTomlConfig(configFilePath string) (Client, error)

ClientConfig

Here is the sample code:

type ClientConfig struct {
        ConnPoolMaxConnSize int
        ConnConnectTimeOut  time.Duration
        ConnLoginTimeout    time.Duration
        OperationTimeOut    time.Duration
        LogLevel            log.Level

        TableEntryRefreshLockTimeout     time.Duration
        TableEntryRefreshTryTimes        int
        TableEntryRefreshIntervalBase    time.Duration
        TableEntryRefreshIntervalCeiling time.Duration

        MetadataRefreshInterval    time.Duration
        MetadataRefreshLockTimeout time.Duration

        RsListLocalFileLocation    string
        RsListHttpGetTimeout       time.Duration
        RsListHttpGetRetryTimes    int
        RsListHttpGetRetryInterval time.Duration

        EnableRerouting bool
}

The following table describes the key parameters.

Parameter

Description

ConnPoolMaxConnSize

The number of connections in the connection pool. Default value: 1.

ConnConnectTimeOut

The timeout period of TCP connections. Default value: 1s.

ConnLoginTimeout

The timeout period of authentication. Default value: 1s.

OperationTimeOut

The timeout period of a single request. Default value: 10s.

EnableRerouting

Specifies whether to enable rerouting, which is disabled by default.