OceanBase
English日本語简体中文
Docs Home
Products
Integrations
Release Notes

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog ServiceOceanBase DataPilot

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog ServiceOceanBase DataPilot
OceanBase DatabaseOceanBase Cloud ToolsConnectors and MiddlewareLifecycle Support Policy

OceanBase Database

  • Overview of the ecosystem
  • ORM frameworks
    • Java
      • Spring Boot
      • Spring Batch
      • Use Spring JDBC to connect to OceanBase Database
      • Spring Data JPA
      • Hibernate
      • MyBatis
      • jOOQ
    • Python
      • Use SQLAlchemy to connect to OceanBase Database
      • Connect to OceanBase Database by using peewee
      • Use Tortoise ORM to connect to OceanBase Database
    • Go
      • Connect to OceanBase Database by using GORM
      • beego ORM
    • Rust
      • SeaORM Connection to OceanBase Database
      • Diesel Connection to OceanBase Database
    • Ruby
      • Active Record
      • Sequel Connection to OceanBase Database
      • Connect to OceanBase Database by using Ruby on Rails
    • Node.js
      • Sequelize
      • TypeORM
      • Prisma
      • Drizzle
    • C#
      • Dapper
      • SqlSugar
      • FreeSQL
    • PHP
      • Connect to OceanBase Database by using the Yii 2.0 ORM
      • Connect to OceanBase Database by using Laravel Eloquent ORM
      • CakePHP
  • Database version management
    • Liquibase
    • Flyway
  • Data integration
    • Flink
      • Apache Flink
      • Flink DirectLoad
      • Flink Connector OceanBase
    • dbt
    • SeaTunnel
    • Maxwell
    • Debezium
    • Spark Catalog
    • NiFi
    • Qlik Replicate
    • RisingWave CDC
    • Flume
    • Migrate data from SQL Server to OceanBase Database by using MySQL Workbench
  • SQL development
    • WebLogic
    • DBeaver
    • Navicat
    • DataGrip
    • TablePlus
    • DMS
  • Orchestration and scheduling
    • Airflow
    • PowerJob
    • DolphinScheduler
    • Linkis
  • Visualization tools
    • Superset
    • Excel
    • Power BI
    • Quick BI
    • Tableau
    • GuanData
    • vividime
    • DataV
    • Metabase
    • Redash
    • FineReport
  • Database management
    • NineData
    • Bytebase
    • DataHub
    • Zabbix
  • AI
    • OpenAI
    • Qwen
    • LangChain
    • LlamaIndex
    • Dify
    • n8n
    • Trae
    • SpringAI
    • Cline
    • Cursor
    • Continue
    • MCP Toolbox
    • CamelAI
    • Firecrawl
    • Hugging Face
    • Ollama
    • Google Gemini
    • Cloudflare Workers AI
    • DB GPT
    • Jina AI
    • Augment Code
    • Claude Code
    • Kiro
    • GPT4All
  • Application
    • Ghost
    • Jira
    • Seata
    • SAP Crystal Reports
  • Middleware
    • Apache Pulsar

Download PDF

Document Feedback
  1. Documentation Center
  2. OceanBase Database
  3. master

ActiveRecord sample program for connecting to OceanBase Database

Last Updated:2026-09-04 07:40:45

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 Database
Step 4: Create and configure a Rails application
Step 5: Run the sample program
FAQs
Performance optimization suggestions

Applicability

ActiveRecord is applicable to OceanBase Database in MySQL mode.

This topic describes how to use ActiveRecord and OceanBase Database to build an application that can perform basic operations such as table creation, data insertion, and data query.

Prerequisites

  • You have installed Ruby and RubyGems.
  • You have installed OceanBase Database and created a MySQL tenant.

Procedure

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

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. Here is an example for Ubuntu:

    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 the 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 Database

Contact the deployment personnel or administrator of OceanBase Database to obtain the database connection string.

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

Parameter description:

  • $host: the IP address for connecting to OceanBase Database.
  • $port: the port for connecting to OceanBase Database.
  • $database_name: the name of the database to be accessed.
  • $user_name: the username of the tenant.
  • $password: the password of the tenant.

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 program

  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 a record
    users = User.all
    

FAQs

  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 that the user has sufficient permissions to perform the required operations.

  3. SQL syntax error: If the SQL statement has syntax errors, check whether the syntax of the SQL statement is correct.

Performance optimization suggestions

  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

Diesel Connection to OceanBase Database
Last

Next topic

Sequel Connection to OceanBase Database
Next
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 Database
Step 4: Create and configure a Rails application
Step 5: Run the sample program
FAQs
Performance optimization suggestions