All Products
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.


  • 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

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




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


    The password of the account in fullUserName.


    The domain name of the OceanBase database to be connected.


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


    The name of the database.


    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)


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.




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


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


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


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


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