OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.3.5

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.3.5
    iconOceanBase Database
    SQL - V 4.3.5
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Build a Go application

    Last Updated:2026-04-09 02:53:54  Updated
    Share
    What is on this page
    Prerequisites
    Procedure
    Step 1: Obtain a database connection string
    Step 2: Install Go MySQL Driver
    Step 3: Write an application
    Step 4: Run the application
    More information

    folded

    Share

    This topic describes how to build a Go application with OceanBase Database and Go MySQL Driver.

    Prerequisites

    You have deployed a Go environment on your computer.

    Procedure

    Step 1: Obtain a database connection string

    Obtain a database connection string from a database deployment engineer or administrator. For example:

    obclient  -h100.88.xx.xx -uroot@test -p****** -P2881 -Doceanbase
    

    The database connection string contains parameters required for accessing OceanBase Database. Before you create an application, you can log in to OceanBase Database by using the database connection string, to verify that the parameters are correct.

    The parameters are described as follows:

    • -h: the IP address for connecting to OceanBase Database, which is sometimes the IP address of an OceanBase Database Proxy (ODP).
    • -u: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The default tenant of a cluster is sys, and the default administrator of a tenant is root. The cluster name is not required when you directly connect to OceanBase Database, but is required when you connect to OceanBase Database through an ODP.
    • -p: the user password.
    • -P: the port for connecting to OceanBase Database, which is also the listening port of the ODP.
    • -D: the name of the database to be accessed.

    Step 2: Install Go MySQL Driver

    You can install Go MySQL Driver by using different methods based on the version of the Go language.

    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 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 go/src directory.

      cd /usr/local/go/src   
      git clone https://github.com/go-sql-driver/mysql.git
      

      Notice

      You must replace /usr/local/go/src with the installation directory of the Go language.

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

    Step 3: Write an application

    The following test.go sample file is for your reference:

    package main
    
    import (
        "database/sql"
        "fmt"
        "log"
    
        _ "mysql"
        // Specify the installation path of Go MySQL Driver. If it is installed in the /src directory, you can specify "mysql" as the path.
    )
    
    type Str struct {
        Name       string
    }
    
    func main() {
        select_all()
        }
    
    func select_all() {
        conn := "root:@tcp(127.0.0.1:2881)/test"
        db, err := sql.Open("mysql", conn)
        if err != nil {
            log.Fatal(err)
        }
    
        defer db.Close()
    
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("success to connect OceanBase with go_mysql driver\n")
        // Create a table named t1.
        db.Query("create table t1(str varchar(256))")
        // Insert data.
        db.Query("insert into  t1 values ('Hello OceanBase')")
        // Query data.
        res, err := db.Query("SELECT * FROM t1")
        // Drop the t1 table.
        db.Query("drop table t1")
        if err != nil {
            log.Fatal(err)
        }
    
        defer res.Close()
    
        if err != nil {
            log.Fatal(err)
        }
    
        for res.Next() {
    
            var str Str
            res.Scan(&str.Name)
            fmt.Printf("%s\n", str.Name)
        }
    }
    

    Modify the database connection parameters in the code. The parameters are concatenated, and the values of the parameters are from the database connection string obtained in Step 1.

    // Format
    conn := "{username}:{password}@tcp({hostname}:{port})/{dbname}"
    // Example
    conn := "root:@tcp(127.0.0.1:2881)/test"
    

    The parameters are described as follows:

    • username: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The value of this parameter is obtained from the -u parameter. The default tenant of a cluster is sys, and the default administrator of a tenant is root. The cluster name is not required when you directly connect to OceanBase Database, but is required when you connect to OceanBase Database through an ODP.

    • password: the user password. The value of this parameter is obtained from the -p parameter.

    • hostname: the IP address for connecting to OceanBase Database, which is sometimes the IP address of an ODP. The value of this parameter is obtained from the -h parameter.

    • port: the port for connecting to OceanBase Database, which is also the listening port of the ODP. The value of this parameter is obtained from the -P parameter.

    • dbname: the name of the database to be accessed. The value of this parameter is obtained from the -D parameter.

    Step 4: Run the application

    After you edit the code, run the following command:

    // Configure temporary environment variables based on the installation path of the Go language.
    export PATH=$PATH:/usr/local/go/bin
    
    // Execute the sample script.
    go run test.go
    
    // Alternatively, generate a binary file and then run it.
    go build test.go
    ./test
    

    If the following results are returned, you have connected to OceanBase Database, and the sample script is correctly executed.

    success to connect OceanBase with go_mysql driver
    Hello OceanBase
    

    More information

    For more information about how to create a Go application, see sample Go application in the open-source community of OceanBase Database.

    Previous topic

    Build a C application
    Last

    Next topic

    Build a Java application
    Next
    What is on this page
    Prerequisites
    Procedure
    Step 1: Obtain a database connection string
    Step 2: Install Go MySQL Driver
    Step 3: Write an application
    Step 4: Run the application
    More information