Updated on 2025-09-04 GMT+08:00

Connecting to a DB Instance Through Python

If you are connecting to an instance through Python, an SSL certificate is optional, but using an SSL certificate can improve the security of your data. By default, SSL is enabled for new TaurusDB instances. SSL encrypts connections to instances but prolongs connection response time and increases CPU usage. Before enabling SSL, evaluate the impact on service performance. For details about how to enable or disable SSL, see Configuring SSL.

Prerequisites

You should be familiar with:

  • Computer basics.
  • Python programming language.
  • Python knowledge.

Connecting to a DB Instance Through Python

Download the SSL certificate and verify it before connecting to your instance.

  1. Download the CA certificate or certificate bundle.

    1. On the Instances page, click the instance name to go to the Basic Information page.
    2. Click Download under SSL.

  2. Connect to your TaurusDB instance through Python.

    conn = pymysql.connect(
                host=instance_ip,
                port=instance_port,
                user=username,
                password=password,
                database=database_name,
                ssl=ssl_config
            )
    Table 1 Parameter description

    Parameter

    Description

    instance_ip

    IP address of the instance.

    • If you are accessing the instance through an ECS, instance_ip is the private IP address of the instance. You can view the private IP address in the Network Information area on the Basic Information page.
    • If you are accessing the instance through a public network, instance_ip is the EIP that has been bound to the instance. You can view the EIP in the Network Information area on the Basic Information page.
    • If you are accessing the instance through a proxy instance, instance_ip is the proxy address. You can view the proxy address on the Database Proxy page.

    instance_port

    Database port of the instance. The default port is 3306.

    You can view the database port in the Network Information area on the Basic Information page.

    database_name

    Database name used for connecting to the instance.

    ssl_config

    Name of the CA certificate. The certificate should be stored in the directory where the command is executed.

    Code example (Python code for connecting to a TaurusDB database):

    import pymysql
    import os
    
    // Configure database connection parameters.
    instance_ip =  '*.*.*.*' // Replace it with the instance IP address.
    instance_port = 3306
    database_name = 'my_db_test'
    
    // Configure SSL (use the CA bundle to verify the server certificate).
    ssl_config = {
        'ssl': {
            'ca': '/path/to/ca-bundle.pem',  // CA certificate chain file
            'check_hostname': True  // Verify the host name.
        }
    }
    
        conn = None
        try:
            // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
            // In this example, the username and password are stored in the environment variables. Before running the example commands, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
            username = os.getenv('EXAMPLE_USERNAME_ENV')
            password = os.getenv('EXAMPLE_PASSWORD_ENV')
    
            // Set up a database connection.
            conn = pymysql.connect(
                host=instance_ip ,
                port=instance_port ,
                user=username,
                password=password,
                database=database_name ,
                ssl=ssl_config
            )
    
            print("Database connected(SSL with CA verification)")
    
            with conn.cursor() as cursor:
                // Execute a query.
                sql = "SELECT * FROM mytable WHERE columnfoo = 500"
                cursor.execute(sql)
    
                // Obtain and print the results.
                results = cursor.fetchall()
                for row in results:
                    print(row)
    
        finally:
            // Close the database connection.
            if conn and conn.open:
                conn.close()
                print("Database connection closed")

You do not need to download the SSL certificate because certificate verification on the server is not required.

  1. Connect to your TaurusDB instance through Python.

    conn = pymysql.connect(
                host=instance_ip,
                port=instance_port,
                user=username,
                password=password,
                database=database_name,
                ssl=ssl_config
            )
    Table 2 Parameter description

    Parameter

    Description

    instance_ip

    IP address of the instance.

    • If you are accessing the instance through an ECS, instance_ip is the private IP address of the instance. You can view the private IP address in the Network Information area on the Basic Information page.
    • If you are accessing the instance through a public network, instance_ip is the EIP that has been bound to the instance. You can view the EIP in the Network Information area on the Basic Information page.

    instance_port

    Database port of the instance. The default port is 3306.

    You can view the database port in the Network Information area on the Basic Information page.

    database_name

    Database name used for connecting to the instance.

    Code example (Python code for connecting to a TaurusDB database):

    import pymysql
    import os
    
    // Configure database connection parameters.
    instance_ip= '*.*.*.*' // Replace it with the instance IP address.
    instance_port= 3306
    database_name= 'my_db_test'
    
        conn = None
        try:
            // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
            // In this example, the username and password are stored in the environment variables. Before running the example commands, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
            
            username = os.getenv('EXAMPLE_USERNAME_ENV')
            password = os.getenv('EXAMPLE_PASSWORD_ENV')
    
            // Set up a database connection.
            conn = pymysql.connect(
                host=instance_ip,
                port=instance_port,
                user=username,
                password=password,
                database=database_name,
            )
    
            print("Database connected")
    
            with conn.cursor() as cursor:
                // Execute a query.
                sql = "SELECT * FROM mytable WHERE columnfoo = 500"
                cursor.execute(sql)
    
                // Obtain and print the results.
                results = cursor.fetchall()
                for row in results:
                    print(row)
    
        finally:
            // Close the database connection.
            if conn and conn.open:
                conn.close()
                print("Database connection closed")