This topic describes how to build an application using ActiveRecord and OceanBase Cloud database to 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 a cluster instance and a tenant in MySQL mode. For more information, see Create a cluster instance and Create a tenant.
Procedure
- Check the versions of Ruby and RubyGems.
- Install the required gems.
- Obtain the connection information for the OceanBase Cloud database.
- Create and configure the Rails application.
- Run the example 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 gems
Install system dependencies (using Ubuntu as an example):
sudo apt-get install ruby-dev mysql-client libmysqlclient-devIf using RVM (Ruby Version Manager):
rvm requirementsIf using rbenv:
rbenv install -l # View available versions rbenv install x.x.x # Install a specific version rbenv global x.x.x # Set the global versionUse
gemto install ActiveRecord and mysql2:gem install activerecord mysql2
Step 3: Obtain the connection information for the OceanBase Cloud database
Log in to the OceanBase Cloud console. On the instance list page, expand the target instance's information. Under the target tenant, select Connect > Get connection string.
For more detailed operations, see Get the connection string.
Fill in the corresponding information in the URL below based on your created OceanBase Cloud database.
mysql -h$host -P$port -u$user_name -p$password -D$database_nameParameter description:
$host: The connection address for the OceanBase Cloud database, for example,t********.********.oceanbase.cloud.$port: The connection port for the OceanBase Cloud database, which is 3306 by default.$database_name: The name of the database to be accessed.$user_name: The account used to access the database.$password: The password for the account.
Step 4: Create and configure the Rails application
Create a new Rails application:
rails new activerecord_oceanbase -d mysqlConfigure 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_portCreate a sample model:
# app/models/user.rb class User < ActiveRecord::Base validates :name, presence: true end
Step 5: Run the sample program
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 endRun the migration:
rails db:migrateTest in the Rails console:
rails c# Create Record user = User.create(name: "John Doe")# Query Records users = User.all
FAQ
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.
Permission error: If you encounter a permission-related error, ensure the user has sufficient permissions to perform the required operation.
SQL syntax error: If an SQL statement contains a syntax error, check whether the syntax of the SQL statement is correct.
Performance optimization suggestions
- Use batch operation methods of ActiveRecord (such as
insert_all) - Use preloading (preload) to avoid the N+1 query problem
- Use caching appropriately
- Optimize database indexes