This topic introduces how to connect to OceanBase Cloud using the Go-SQL-Driver/MySQL driver.
Prerequisites
You have registered an OceanBase Cloud account, and have created a cluster instance and a MySQL-compatible tenant. For details, refer to Create a cluster instance and Create a tenant.
You have downloaded the sample project used in this topic.
Procedure
Note
This topic uses macOS for demonstration. The steps may vary slightly if you are using a different operating system environment or compiler.
Step 1: (Optional) Install the Go language and driver
If you have already installed the Go language and Go-SQL-Driver/MySQL driver, you can skip this step. Otherwise, follow the steps below to install.
Install the Go language.
Download the Go language installer package from the Go official website and install it.
Note
The Go package used in this topic is go1.22.2.darwin-amd64.pkg.
Configure the environment variable by adding the installation path of the Go language to the system's PATH environment variable.
In Linux or macOS environment, you can edit the
~/.bashrcor~/.bash_profilefile and add the following content:export PATH=$PATH:/usr/local/go/binIn Windows environment, you can add the value of Path to
C:\usr\local\go\binin Control Panel > System and Security > System > Advanced system settings > Environment Variables > System variables.
Note
Replace
\usr\local\go\binwith the actual installation directory if you modified it during Go language installation.Enter the following command in the command line tool to view the Go language version information to verify that the installation was successful:
go versionSample output:
go version go1.22.2 darwin/amd64
Install the Go-SQL-Driver/MySQL driver.
Depending on the version of the Go language, you can choose different installation methods. To install the Go-SQL-Driver/MySQL driver, you need to enter the corresponding project directory first. In this example, you need to install the driver under the
go-oceanbasefolder. For more details aboutGo-SQL-Driver/MySQL, refer to Github.Use the following command to install:
cd go-oceanbase go get -u github.com/go-sql-driver/mysqlIf you cannot install it using the
go getcommand due to version or network reasons, you can installgo-sql-driver/mysqlusing thego installcommand.Clone the
go-sql-driver/mysqlrepository from GitHub into thego/srcdirectory.cd /usr/local/go/src git clone https://github.com/go-sql-driver/mysql.gitNote
Replace
/usr/local/go/srcwith the actual Go installation directory operation.Install it using
go install.go install mysqlNote
For some versions, the default execution directory of
go installmay not be/src. You can determine the actual directory by checking the error message after executinggo install. For example, if the error message iscannot find package "mysql" in: /usr/local/go/src/vendor/mysql, then you should put the mysql folder under the/src/vendordirectory and then execute the installation command.Check if the Go-SQL-Driver/MySQL driver has been installed. If the installation fails, please make modifications according to the error message.
go list -m github.com/go-sql-driver/mysql
Step 2: Obtain an OceanBase Cloud connection string
Log in to the OceanBase Cloud console. On the Instances page, expand the target instance and click Create under the target tenant.

In the pop-up window, click Connect with Public IP.
In the Connect with Public IP window, complete the following settings to generate the connection string:
- Under 1. Add an IP address to the allowlist, click Add to add your exit IP address(es) used for the connection to the allowlist.
- (Optional) Under 2. Download the CA certificate to connect securely to the tenant, download the CA certificate and complete the verification.
- Under 3. Connect to your instance, click the drop-down list for Database and Account to create a database and an account for the connection. Select MySQL CLI as the connection method.
Notice
Please keep your password in a secure place after creating your account.

Step 3: Modify the database connection information in the go-oceanbase sample project
Based on the connection string obtained in Step 2: Obtain an OceanBase Cloud connection string, modify the following content in the test.go file of the sample project go-oceanbase and save it:
conn := "user_name:password@tcp(host:port)/schema_name"
// Database connection parameters
- user_name: Taken from the
-uparameter in the connection string, which is the account name, for example,test. - password: Taken from the
-pparameter in the connection string, which is the account password. - host: Taken from the
-hparameter in the connection string, which is the hostname of OceanBase Cloud database, for example,t5******.aws-ap-southeast-1.oceanbase.cloud. - port: Taken from the
-Pparameter in the connection string, which is the OceanBase Cloud database connection port. - schema_name: Modify it to
test, the name of the schema to be accessed istest.
Here's an example:
conn := "testaccount:111****QW@tcp(t5******.aws-ap-southeast-1.oceanbase.cloud:3306)/test"
Step 4: Run the go-oceanbase sample project
After editing the code, navigate to the go-oceanbase folder and run the sample project using the following command:
go run test.go
If the following content is returned after running, it indicates that the database connection is successful and the sample project is executed correctly:
success to connect OceanBase with go_mysql driver
Hello OceanBase
Note
This content has omitted the result of "drop table t1" after running.
More information
For more information about Go-SQL-Driver/MySQL, you can refer to the OceanBase Database open source community, please visit Go-SQL-Driver/MySQL.
Click to download the go-oceanbase sample project