All Products
Document Center

ApsaraDB for OceanBase:Connect to OceanBase Database by using a native MySQL driver

Last Updated:Jan 13, 2023

OceanBase Database in MySQL mode supports open source MySQL drivers in any language. This topic uses the JDBC driver for MySQL as an example to describe how to connect to OceanBase Database by using a native MySQL driver.


You have installed MySQL Connector/J and configured the operating environment.

For more information about how to download and install MySQL Connector/J, see Connector/J Downloads and Connector/J Installation.

Connect to OceanBase Database

The following example describes how to use Connector/J 5.1.47 to connect to an OceanBase database in Linux.

After you install MySQL Connector/J 5.1.47 and configure the operating environment, you can connect to the database based on the sample code in the file.


For the MySQL Connector/J 8.x version, you must replace com.mysql.jdbc.Driver with com.mysql.cj.jdbc.Driver in Class.forName("com.mysql.jdbc.Driver").

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
   public static void main(String[] args) {
       try {


                Connection connection = DriverManager.getConnection("jdbc:mysql://172.30.xx.xx:2883/test?user=r***&password=");
                Statement sm = connection.createStatement();
                // Perform operations such as dropping a table, creating a table, and inserting data into a table. 
                String q1="drop table if exists t_meta_form";
                String q2="CREATE TABLE t_meta_form ( name varchar(36) NOT NULL DEFAULT ' ', id int NOT NULL ) DEFAULT CHARSET = utf8mb4";
                String q3="insert into t_meta_form (name,id) values ('an','1')";

            }catch(SQLException se){
                se.printStackTrace() ;
            }catch (Exception ex) {


connection = DriverManager.getConnection("jdbc:mysql://{hostname}:{port}/{dbname}?user={username}&password={password}")
  • hostname: the IP address for connecting to the OceanBase database.

  • port: the port for connecting to the OceanBase database, which is also the listening port of OBProxy. The default value is 3306. You can customize the value as required.

  • dbname: the name of the database to access.

  • username: the username of the tenant connection account.

  • password: the password of the tenant connection account.

Example: jdbc:mysql://172.30.xx.xx:3306/test?user=r***&password=***1**

After you edit the code, you can run the following commands to compile and run the code. If true is returned, you have connected to the database.

// Manually add the driver to the environment configuration based on the actual installation path of mysql-connector-java-5.1.47.jar.
export CLASSPATH=/usr/share/java/mysql-connector-java-5.1.47.jar:$CLASSPATH
// Compile the code.
// Run the code.
java Test