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 Cloud

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive 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 Cloud
    iconOceanBase Cloud
    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

      ActiveRecord sample application for OceanBase Cloud

      Last Updated:2026-05-29 03:53:36  Updated
      Share
      What is on this page
      Prerequisites
      Procedure
      Step 1: Check the versions of Ruby and RubyGems
      Step 2: Install the required gem
      Step 3: Obtain the connection information of OceanBase Cloud
      Step 4: Create and configure a Rails application
      Step 5: Run the sample application
      Common issues
      Performance optimization recommendations

      folded

      Share

      This topic describes how to use ActiveRecord and OceanBase Cloud to build an application that can perform basic operations such as creating tables, inserting data, and querying data.

      Prerequisites

      • You have installed Ruby and RubyGems.
      • You have registered an OceanBase Cloud account and created an instance and a MySQL compatible tenant. For more information, see Create an instance and Create a tenant.

      Procedure

      1. Check the versions of Ruby and RubyGems.
      2. Install the required gem.
      3. Obtain the connection information of OceanBase Cloud.
      4. Create and configure a Rails application.
      5. Run the sample application.

      Step 1: Check the versions of Ruby and RubyGems

      Open the terminal and run the following command to check the versions of Ruby and RubyGems:

      ruby -v
      gem -v
      

      Step 2: Install the required gem

      1. Install the system dependencies (Ubuntu is used as an example):

        sudo apt-get install ruby-dev mysql-client libmysqlclient-dev
        
      2. If you use RVM (Ruby Version Manager):

        rvm requirements
        
      3. If you use rbenv:

        rbenv install -l  # View available versions
        rbenv install x.x.x  # Install a specific version
        rbenv global x.x.x  # Set the global version
        
      4. Use gem to install ActiveRecord and mysql2:

        gem install activerecord mysql2
        

      Step 3: Obtain the connection information of OceanBase Cloud

      1. Log in to the OceanBase Cloud console. On the instance list page, expand the information of the target instance, and in the target tenant, choose Connect > Get Connection String.

        For more information, see Get the connection string.

      2. Fill in the URL with the information of the created OceanBase Cloud database.

        mysql -h$host -P$port -u$user_name -p$password -D$database_name
        

        Parameter description:

        • $host: the connection address of the OceanBase Cloud database, for example, t********.********.oceanbase.cloud.
        • $port: the connection port of the OceanBase Cloud database. The default value is 3306.
        • $database_name: the name of the database to be accessed.
        • $user_name: the account for accessing the database.
        • $password: the password of the account.

      Step 4: Create and configure a Rails application

      1. Create a new Rails application:

        rails new activerecord_oceanbase -d mysql
        
      2. Configure the database connection information:

        Add the following configuration to config/database.yml:

        development:
          adapter: mysql2
          encoding: utf8mb4
          database: your_database
          username: your_username
          password: your_password
          host: your_host
          port: your_port
        
      3. Create a sample model:

        # app/models/user.rb
        class User < ActiveRecord::Base
          validates :name, presence: true
        end
        

      Step 5: Run the sample application

      1. Create a database table:

        # db/migrate/20230520_create_users.rb
        class CreateUsers < ActiveRecord::Migration[6.1]
          def change
            create_table :users do |t|
              t.string :name
              t.timestamps
            end
          end
        end
        
      2. Run the migration:

        rails db:migrate
        
      3. Test in the Rails console:

        rails c
        
        # Create a record
        user = User.create(name: "John Doe")
        
        # Query records
        users = User.all
        

      Common issues

      1. Connection error: If you cannot connect to the database, check the following:

        • Whether the database address and port are correct
        • Whether the username and password are correct
        • Whether the network connection is normal
      2. Permission error: If you encounter permission-related errors, make sure the user has sufficient permissions to perform the required operations.

      3. SQL syntax error: If there is a syntax error in the SQL statement, check whether the syntax of the SQL statement is correct.

      Performance optimization recommendations

      1. Use ActiveRecord's batch operation methods (such as insert_all)
      2. Use eager loading (preload) to avoid N+1 query issues
      3. Use caching reasonably
      4. Optimize database indexes

      Previous topic

      SeaORM example for connecting to OceanBase Cloud
      Last

      Next topic

      Connect to OceanBase Cloud by using mysql2
      Next
      What is on this page
      Prerequisites
      Procedure
      Step 1: Check the versions of Ruby and RubyGems
      Step 2: Install the required gem
      Step 3: Obtain the connection information of OceanBase Cloud
      Step 4: Create and configure a Rails application
      Step 5: Run the sample application
      Common issues
      Performance optimization recommendations