Connect to an OceanBase database using a Go client
This topic describes how to use a Go client to connect to a database in proxy mode.
Prerequisites
Use the latest version of the Go client from the GitHub Go client repository.
Import the `obkv-table-client-go` dependency. Run the following command in your local Go repository to download the latest `obkv-table-client-go` dependency. You can also download a specific version.
go get github.com/oceanbase/obkv-table-client-go
Connection methods
ApsaraDB for OceanBase supports database connections in proxy mode. You can use one of the following methods to connect to your database.
Method 1: Call a function directly
Call the function in your Go code. Modify the parameters as described in the following table to connect to the database.
func NewOdpClient(
fullUserName string,
passWord string,
odpIP string,
odpRpcPort int,
database string,
cliConfig *config.ClientConfig) (Client, error)Parameter descriptions
Parameter
Description
fullUserName
The format is <username>@<tenant_name>#<cluster_name>.
passWord
The password for the account in fullUserName.
odpIP
The connection domain name of the OceanBase database.
odpRpcPort
Enter the service port for the OceanBase database's wide-table engine (or KV pattern). The default is 3307.
database
The name of the database to connect to.
cliConfig
The client configuration. For more information, see ClientConfig.
Return value description
Parameter
Description
Client
The client interface. The interface includes methods such as `insert`, `get`, and `update`. For more information, see Use a Go client to connect to a database: examples.
error
The error message.
Method 2: Use a TOML file
Create your configuration file based on the `configurations/obkv-table-default.toml` template file. Set `Mode` to `proxy` and modify the `OdpClientConfig` content. Modify other content as needed. For more information, see ClientConfig.
Call the function in your Go code and provide the path of the TOML file.
func NewClientWithTomlConfig(configFilePath string) (Client, error)Parameter descriptions
Parameter
Description
configFilePath
The path of the TOML file: `configurations/obkv-table-default.toml`.
Return value description
Parameter
Description
Client
The client interface. The interface includes methods such as `insert`, `get`, and `update`. For more information, see Use a Go client to connect to a database: examples.
error
The error message.
ClientConfig
Example:
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
}Important configuration items:
Configuration item | Description |
ConnPoolMaxConnSize | The number of connections in the connection pool. The default value is 1. |
ConnConnectTimeOut | The TCP connection timeout. The default value is 1 s. |
ConnLoginTimeout | The authentication timeout. The default value is 1 s. |
OperationTimeOut | The timeout for a single request. The default value is 10 s. |
EnableRerouting | Specifies whether to enable the secondary routing feature. This feature is disabled by default. |