OceanBase logo

OceanBase

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

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

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

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 - V3.1.4Community Edition

  • Get Started
    • Overview
    • Quick Start
    • Hands on for OceanBase SQL
      • Before You Start
      • Basic Operations
    • Build Applications and Connect to OceanBase
      • Connect a Python application to OceanBase Database
      • Connect an ODBC application to OceanBase Database (using Unix ODBC)
      • Connect a Java application to OceanBase Database
      • Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL)
      • Connect a C application to OceanBase Database
    • Experience OceanBase Advanced Features
      • Experience Scalable OLTP
        • Run the TPC-C benchmark test in OceanBase Database
        • Experience the hot row update capability of OceanBase Database
      • Experience operational OLAP
      • Experience parallel import and data compression
      • Experience the multi-tenant feature
    • FAQ
    • Glossary

Download PDF

Overview Quick Start Before You Start Basic Operations Connect a Python application to OceanBase Database Connect an ODBC application to OceanBase Database (using Unix ODBC) Connect a Java application to OceanBase Database Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL) Connect a C application to OceanBase Database Experience operational OLAP Experience parallel import and data compression Experience the multi-tenant feature FAQ Glossary
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
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. V3.1.4
iconOceanBase Database
SQL - V 3.1.4Community Edition
SQL
KV
  • 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
Community Edition
  • V 3.1.4

Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL)

Last Updated:2023-07-21 09:10:58  Updated
share
What is on this page
Install the Golang driver
Connect to OceanBase Database
Troubleshoot the PreparedStatement error

folded

share

Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL)

This topic provides a code example on how to connect a Golang application to OceanBase Database.

Install the Golang driver

Use the go get command (for Go V1.13 - V1.16)

Run the following command to install the Golang driver:

go get -u github.com/go-sql-driver/mysql

Note: For details about Go-SQL-Driver/MySQL, see GitHub.

Use the go install command

If the go get command cannot be used due to version or network reasons, you can try the following:

  1. Clone the go-sql-driver/mysql repository in GitHub to the src directory.

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

    Note: In some versions, the default directory for go install may not be /src, so you can determine the actual directory by the error reported after go install is executed. For example, if the error is cannot find package "mysql" in: /usr/local/go/src/vendor/mysql, then put the mysql folder in the /src/vendor directory before running the install command.

  2. Run the following command to install the Golang driver:

    go install mysql
    

Connect to OceanBase Database

Run the following sample code to connect to OceanBase Database, where you can use username:password@protocol(address)/dbname?param=value to set the connection string.

package main

···

import (
    "database/sql"
    "fmt"
    "log"
    
    _ "github.com/go-sql-driver/mysql" //Fill in the exact path for installing go-sql-driver/mysql. If installed in the src directory, you can fill in "mysql".
)

type City struct {
    Id         int
    Name       string
    Population int
}

func select_all() {
    conn := "root:@tcp(127.0.0.1:2881)/testdb"
    db, err := sql.Open("mysql", conn)
    if err != nil {
        log.Fatal(err)
    }
    
    defer db.Close()
    
    if err != nil {
        log.Fatal(err)
    }
    
    res, err := db.Query("SELECT * FROM cities")
    if err != nil {
        log.Fatal(err)
    }
    
    defer res.Close()
    
    if err != nil {
        log.Fatal(err)
    }
    
    for res.Next() {
        
        var city City
        err := res.Scan(&city.Id, &city.Name, &city.Population)
        
        if err != nil {
            log.Fatal(err)
        }
        
        fmt.Printf("%v\n", city)
    }
}

Troubleshoot the PreparedStatement error

Log on to OceanBase Database as the root user and run the following command:

alter system set _ob_enable_prepared_statement = true;

Previous topic

Connect a Java application to OceanBase Database
Last

Next topic

Connect a C application to OceanBase Database
Next
What is on this page
Install the Golang driver
Connect to OceanBase Database
Troubleshoot the PreparedStatement error