Connect to OceanBase Cloud by using mysqlclient

2026-01-21 03:00:30  Updated

This topic describes how to use mysqlclient and OceanBase Cloud to build an application for basic operations, such as table creation, data insertion, and data query.

Prerequisites

  • You have installed Python 3.x and pip.
  • You have registered an OceanBase Cloud account and created an instance in OceanBase Cloud. For more information, see Create an instance.
  • You have obtained the connection string of the instance. For more information, see Obtain the connection string.

Procedure

  1. Check the versions of Python and pip.
  2. Install the mysqlclient library.
  3. Modify the database connection information in the config.py file.
  4. Run the main.py file.
  5. Perform database operations on the CLI.

Step 1: Check the versions of Python and pip

Open Command Prompt or PowerShell, and run the python --version and pip --version commands to ensure that Python and pip are properly installed.

Here is an example:

PS C:\Windows\system32> python --version
Python 3.11.2
PS C:\Windows\system32> pip --version
pip 23.3.1 from C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\pip (python 3.11)

Step 2: Install the mysqlclient library

When you install the mysqlclient library, you must compile and link the MySQL C API to connect to OceanBase Cloud. Therefore, you must install the development files of MySQL Connector C or MySQL C API for later compilation.

Windows
Linux

Method 1: Install the mysqlclient library by using a precompiled binary file

In Windows, you can install the mysqlclient library by using a precompiled binary file. To do so, download the whl file compatible with your Python version and operating system from Download files, and then run the pip install command.

Here is an example:

Open Command Prompt or PowerShell, and run the following commands to install the mysqlclient library.

  1. Navigate to the directory where the whl file is located.

    cd D:\download
    
  2. Run the following command to install the mysqlclient library:

    PS D:\download> pip install mysqlclient-2.2.0-cp311-cp311-win_amd64.whl
    Processing d:\download\mysqlclient-2.2.0-cp311-cp311-win_amd64.whl
    Installing collected packages: mysqlclient
    Successfully installed mysqlclient-2.2.0
    

Method 2: Install the mysqlclient library directly

  1. Install MySQL.

    Before you use mysqlclient, you need to install the MySQL database server. mysqlclient depends on the client program and library files of MySQL to communicate with MySQL. You can download the installation program from the MySQL official website and install it as prompted.

  2. Install a C compiler.

    In Windows, you can install Visual Studio or MinGW.

  3. Install a development tool.

    In Windows, you can use MySQL Connector/C to obtain the development libraries of MySQL. You can perform the following steps to install MySQL Connector C:

    1. Go to the download page on the official website of MySQL.

    2. Download the appropriate installation package for your operating system.

    3. Run the installation package and follow the instructions in the installation wizard to install it.

    4. During the installation, select the Custom installation type and ensure that Development Components are installed.

    5. After the installation is complete, restart your computer.

  4. Install the mysqlclient library.

    Open Command Prompt or PowerShell, and run the following commands to install the mysqlclient library.

    1. Run the following command to go to the python-mysqlclient project directory:

      cd python-mysqlclient
      
    2. Run the following command to install the mysqlclient library:

      pip install -r requirements.txt
      

    Note

    You can also run the pip install mysqlclient command to install the mysqlclient library.

In Linux, Python C extensions rely on the Python.h header file, which is usually contained in the python3-devel package. If the python3-devel package is not installed, an error will be returned when you compile the MySQL C API, indicating that the Python.h header file cannot be found.

Open Command Prompt or PowerShell, and run the following commands to install the mysqlclient library.

  1. Check whether the python3-devel package has been installed.

    Run the following command to check whether the python3-devel package has been installed:

    rpm -q python3-devel
    
    • If yes, the following information is displayed:

      python3-devel-3.x.x-x.el7.x86_64
      

      3.x.x indicates the version of Python3, and x86_64 indicates the CPU architecture of the system.

    • If the package has been not installed, the following information is displayed:

      package python3-devel is not installed
      

      In this case, you can run the following command to install the python3-devel package:

      sudo yum install python3-devel
      

      After the installation is complete, run the rpm -q python3-devel command again to verify whether the installation is successful.

  2. Install the mysqlclient library.

    1. Run the following command to go to the python-mysqlclient project directory:

      cd python-mysqlclient
      
    2. Run the following command to install the Python library required by the project:

      sudo pip install -r requirements.txt
      

    Note

    You can also run the sudo pip install mysqlclient command to install the mysqlclient library.

Step 3: Modify the database connection information in the config.py file

Modify the database connection information in the config.py file in the python-mysqlclient/ directory based on the obtained connection string mentioned in the "Prerequisites" section.

  1. Go to the python-mysqlclient project directory.

  2. Modify the database connection information in the config.py file.

    • In Windows, use the text editor to open the config.py file and modify the database connection information in the file based on the actual situation.
    • In Linux, use the vi config.py or vim config.py command to open the config.py file and modify the database connection information based on the actual situation.

    Here is an example of the database connection information in the config.py file:

    OCEANBASE_CONFIG = {
        'host': 't5******.********.oceanbase.cloud',
        'port': 3306,
        'user': 'test',
        'password': '******',
        'database': 'test',
        'charset': 'utf8mb4'
    }
    

Step 4: Run the main.py file

Open Command Prompt or PowerShell, and run the python main.py command to start the application.

  1. Go to the python-mysqlclient project directory.

    Here is an example:

    cd /home/admin/python-mysqlclient
    
  2. Run the following command to start the main.py application:

    python main.py
    

    The return result is as follows:

    Table created successfully
    Instruction: 1.Insert Data; 2.Query Data; 3.Exit.
    Enter the command [1/2/3]>
    

Step 5: Perform database operations on the CLI

  • Example of data insertion success

    1. On the CLI, enter 1 and press Enter.

      Here is an example:

      Enter the command [1/2/3]> 1
      
    2. After the Enter name: prompt, enter a name and press Enter.

      Here is an example:

      Enter name: A1
      
    3. After the Enter age: prompt, enter an age and press Enter.

      Here is an example:

      Enter age: 18
      
    4. The system displays Record inserted successfully, indicating that the data is successfully inserted. Finally, an instruction is displayed, prompting you to enter 1, 2, or 3 and press Enter to perform the corresponding operation.

      Here is an example:

      Record inserted successfully
      Instruction: 1.Insert Data; 2.Query Data; 3.Exit.
      Enter the command [1/2/3]>
      
  • Example of data insertion failure

    1. On the CLI, enter 1 and press Enter.

      Here is an example:

      Enter the command [1/2/3]> 1
      
    2. After the Enter name: prompt, enter a name and press Enter.

      Here is an example:

      Enter name: A2
      
    3. After the Enter age: prompt, enter an age and press Enter.

      Notice

      The data type of the age field is INT.

      Here is an example:

      Enter age: Eighteen
      
    4. The system displays (1366, 'Incorrect integer value'), indicating that the data failed to be inserted. Finally, an instruction is displayed, prompting you to enter 1, 2, or 3 and press Enter to perform the corresponding operation.

      Here is an example:

      (1366, 'Incorrect integer value')
      Instruction: 1.Insert Data; 2.Query Data; 3.Exit.
      Enter the command [1/2/3]>
      
  • Example of data query

    1. On the CLI, enter 2 and press Enter.

      Here is an example:

      Enter the command [1/2/3]> 2
      
    2. The data of the table is displayed. Finally, an instruction is displayed, prompting you to enter 1, 2, or 3 and press Enter to perform the corresponding operation.

      Here is an example:

      (1, 'A1', 18)
      Instruction: 1.Insert Data; 2.Query Data; 3.Exit.
      Enter the command [1/2/3]>
      
  • Example of entering an incorrect command

    1. On the CLI, enter a value other than 1, 2, and 3, and press Enter.

      Here is an example:

      Enter the command [1/2/3]> A
      
    2. The following error message is displayed: Invalid command, please enter command again [1/2/3]. Finally, an instruction is displayed, prompting you to enter 1, 2, or 3 and press Enter to perform the corresponding operation.

      Here is an example:

      Invalid command, please enter command again [1/2/3]
      Instruction: 1.Insert Data; 2.Query Data; 3.Exit.
      Enter the command [1/2/3]>
      
  • Example of exiting the application

    On the CLI, enter 3 and press Enter.

    Here is an example:

    Enter the command [1/2/3]> 3
    

Project code

Click here to download the project code, which is a package named python-mysqlclient.zip.

Decompress the package to obtain a folder named python-mysqlclient. The directory structure is as follows:

python-mysqlclient
├── config.py
├── db.py
├── main.py
└── requirements.txt

The files and directories are described as follows:

  • config.py: manages database connection configurations.

  • db.py: operates the database, such as creating tables, inserting data, and querying data.

  • main.py: serves as the entry to the application and provides a simple CLI. You can enter commands on the CLI to perform corresponding operations.

  • requirements.txt: lists the Python libraries required for the project and their version requirements.

    Note

    The requirements.txt file in the package obtained in this topic lists only the mysqlclient library and its version requirements. You can run the pip install -r requirements.txt command to automatically install the required library.

Code in config.py

When you connect to a database by using Python, you must specify database connection parameters. You can store the parameters in a separate configuration file such as config.py. You can encapsulate the parameters in a dictionary so that other Python files can reference the dictionary to connect to the database, without the need to write the parameters to each file.

Code in the config.py file obtained in this topic defines a dictionary variable named OCEANBASE_CONFIG, which is used to manage the connection parameters of OceanBase Cloud.

The sample code is as follows:

OCEANBASE_CONFIG = {
    'host': 'localhost',
    'port': port,
    'user': 'user_name',
    'password': '',
    'database': 'db_name',
    'charset': 'utf8mb4'
}

The parameters are described as follows:

  • host: the access address of OceanBase Cloud. The value is sourced from the -h parameter in the connection string.
  • port: the access port of OceanBase Cloud. The value is sourced from the -P parameter in the connection string.
  • user: the account name. The value is sourced from the -u parameter in the connection string.
  • password: the account password. The value is sourced from the -p parameter in the connection string.
  • database: the name of the database to be accessed. The value is sourced from the -D parameter in the connection string.
  • charset: the character set for connecting to the database.

Notice

The actual parameter configurations depend on the project requirements and database characteristics. We recommend that you adjust and configure the parameters based on the actual situation.

Code in db.py

The db.py file is a Python module in which database operations are encapsulated. You can use it to add, delete, modify, and query data in a database.

Perform the following steps to configure the db.py file:

  1. Import the MySQLdb module and database connection parameters.

    The sample code is as follows:

    import MySQLdb
    from config import OCEANBASE_CONFIG
    
  2. Define a function for creating a table.

    Define the create_table function to create a table named test_tbl1 in OceanBase Cloud. Use the with statement to manage the lifecycle of database connections and cursor objects. This ensures that database connections and cursor objects can be securely closed without memory leaks. Define an SQL statement, execute the statement, and print the execution result or error message.

    The sample code is as follows:

    def create_table():
        with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
            with conn.cursor() as cursor:
                try:
                    create_table_sql = """
                        CREATE TABLE test_tbl1 (
                            id INT UNSIGNED NOT NULL AUTO_INCREMENT,
                            name VARCHAR(50) NOT NULL,
                            age INT UNSIGNED NOT NULL,
                            PRIMARY KEY (id)
                        ) ENGINE=OCEANBASE AUTO_INCREMENT=1
                    """
                    cursor.execute(create_table_sql)
    
                    print("Table created successfully")
                except MySQLdb.Error as err:
                    print(err)
    
  3. Define a function for inserting data.

    Define the insert_record function to insert a record into a specified table. The record contains the name and age fields. Use the with statement to manage the lifecycle of database connections and cursor objects. This ensures that database connections and cursor objects can be securely closed without memory leaks. Define an SQL statement, execute the statement, commit the transaction, and print the execution result or error message.

    The sample code is as follows:

    def insert_record(table_name, name, age):
        with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
            with conn.cursor() as cursor:
                try:
                    insert_sql = f"INSERT INTO {table_name} (name, age) VALUES (%s, %s)"
                    cursor.execute(insert_sql, (name, age))
                    conn.commit()
    
                    print("Record inserted successfully")
                except MySQLdb.Error as err:
                    print(err)
    
  4. Define a function for querying table data.

    Define the select_all function to query all records in a specified table. Use the with statement to manage the lifecycle of database connections and cursor objects. This ensures that database connections and cursor objects can be securely closed without memory leaks. Define an SQL statement, execute the statement, traverse query results, and print all records. If an error occurs, capture the error and print the error message.

    The sample code is as follows:

    def select_all(table_name):
        with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
            with conn.cursor() as cursor:
                try:
                    select_sql = f"SELECT * FROM {table_name}"
                    cursor.execute(select_sql)
                    result = cursor.fetchall()
                    for row in result:
                        print(row)
    
                except MySQLdb.Error as err:
                    print(err)
    

Code in main.py

The main.py file in this topic shows how to use Python and MySQLdb to operate a database. The file provides a CLI where you can run commands to perform database operations. You can use this application to implement basic database operations such as creating tables, inserting records, and querying all records.

Perform the following steps to configure the db.py file:

  1. Import the functions defined in the db.py file.

    Import the create_table, insert_record, and select_all functions from the db module.

    The sample code is as follows:

    from db import create_table, insert_record, select_all
    
  2. Define a function for operating the database.

    Define the main function to implement a simple CLI application for database operations. The application calls the create_table function to create a table named test_tbl1, and then enters a while loop to wait for you to enter a command. The application calls the corresponding function to insert or query data based on your command until you enter the 3 command to exit the application. If you enter an invalid command, the application prompts you to re-enter a command.

    Command 1 is to insert data, 2 is to query data, and 3 is to exit the application.

    The sample code is as follows:

    def main():
        create_table()
        while True:
            print("Instruction: 1.Insert Data; 2.Query Data; 3.Exit." )
    
            command = input("Enter the command [1/2/3]> ")
            if command == "1":
                name = input("Enter name:" )
                age = input("Enter age:" )
                insert_record("test_tbl1", name, age)
            elif command == "2":
                select_all("test_tbl1")
            elif command == "3":
                break
            else:
                print("Invalid command, please enter command again [1/2/3]")
    
  3. Set the application scenario of the main function.

    Set the main function to be called only when the main.py file is directly run. If the main module is just imported into another module, the main function is not called.

    The sample code is as follows:

    if __name__ == "__main__":
        main()
    

    Note

    This setting can avoid automatic execution of the main function when the main module is imported, thus ensuring the reusability and extensibility of the application.

Complete code

config.py
db.py
main.py
OCEANBASE_CONFIG = {
    'host': 'localhost',
    'port': port,
    'user': 'user_name',
    'password': '',
    'database': 'db_name',
    'charset': 'utf8mb4'
}
import MySQLdb
from config import OCEANBASE_CONFIG


def create_table():
    with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
        with conn.cursor() as cursor:
            try:
                create_table_sql = """
                    CREATE TABLE test_tbl1 (
                        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
                        name VARCHAR(50) NOT NULL,
                        age INT UNSIGNED NOT NULL,
                        PRIMARY KEY (id)
                    ) ENGINE=OCEANBASE AUTO_INCREMENT=1
                """
                cursor.execute(create_table_sql)

                print("Table created successfully")
            except MySQLdb.Error as err:
                print(err)


def insert_record(table_name, name, age):
    with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
        with conn.cursor() as cursor:
            try:
                insert_sql = f"INSERT INTO {table_name} (name, age) VALUES (%s, %s)"
                cursor.execute(insert_sql, (name, age))
                conn.commit()

                print("Record inserted successfully")
            except MySQLdb.Error as err:
                print(err)


def select_all(table_name):
    with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
        with conn.cursor() as cursor:
            try:
                select_sql = f"SELECT * FROM {table_name}"
                cursor.execute(select_sql)
                result = cursor.fetchall()
                for row in result:
                    print(row)

            except MySQLdb.Error as err:
                print(err)
from db import create_table, insert_record, select_all

def main():
    create_table()
    while True:
        print("Instruction: 1.Insert Data; 2.Query Data; 3.Exit." )

        command = input("Enter the command [1/2/3]> ")
        if command == "1":
            name = input("Enter name:" )
            age = input("Enter age:" )
            insert_record("test_tbl1", name, age)
        elif command == "2":
            select_all("test_tbl1")
        elif command == "3":
            break
        else:
            print("Invalid command, please enter command again [1/2/3]")

if __name__ == "__main__":
    main()

Contact Us