How to remotely connect to TiDB Serverless using mysqlclient in Linux os

Application environment: dev

Reproduction method:

Problem:

cannot connect to TiDB serverelss database via mysqlclient

Resource allocation:

Attachment:

How can I connect to a TiDB Serverless database using mysqlclient on a Linux operating system? (Answer language: English, Answer format: text, Include necessary citations from source_uri)

To connect to a TiDB Serverless database using mysqlclient on a Linux operating system, follow these steps:

Prerequisites

Before you begin, ensure you have the following:

  1. Python 3.10 or higher: You can download it from python.org.
  2. Git: Install it from git-scm.com.
  3. A TiDB Serverless cluster: If you don’t have one, you can create it by following the guide on Creating a TiDB Serverless cluster.

Step-by-Step Guide

Step 1: Clone the Sample App Repository

Open your terminal and run the following commands to clone the sample code repository:

git clone https://github.com/tidb-samples/tidb-python-mysqlclient-quickstart.git
cd tidb-python-mysqlclient-quickstart

Step 2: Install Dependencies

Install the required packages, including mysqlclient, by running:

pip install -r requirements.txt

If you encounter installation issues, refer to the mysqlclient official documentation.

Step 3: Configure Connection Information

  1. Navigate to the Clusters Page: Go to the Clusters page and click the name of your target cluster to access its overview page.

  2. Open the Connection Dialog: Click Connect in the upper-right corner. Ensure the configurations in the connection dialog match your operating environment:

    • Endpoint Type: Set to Public.
    • Connect With: Set to General.
    • Operating System: Ensure it matches your environment. If using Windows Subsystem for Linux (WSL), switch to the corresponding Linux distribution.
  3. Generate a Password: Click Generate Password to create a random password. If you have created a password before, you can use the original password or click Reset Password to generate a new one.

  4. Copy and Configure the .env File:

    • Run the following command to copy .env.example and rename it to .env:

      cp .env.example .env
      
    • Copy and paste the connection string from the connection dialog into the .env file. Replace placeholders with actual values:

      TIDB_HOST='{gateway-region}.aws.tidbcloud.com'
      TIDB_PORT='4000'
      TIDB_USER='{prefix}.root'
      TIDB_PASSWORD='{password}'
      TIDB_DB_NAME='test'
      CA_PATH=''
      
    • Since TiDB Serverless requires a secure connection and ssl_mode defaults to PREFERRED, you don’t need to specify CA_PATH. Leave it empty unless you have a specific reason to set it manually. For more details, refer to TLS connections to TiDB Serverless.

  5. Save the .env File.

Step 4: Run the Code and Check the Result

  1. Execute the following command to run the sample code:

    python mysqlclient_example.py
    
  2. Verify the output by checking the Expected-Output.txt to ensure it matches the expected results.

By following these steps, you can successfully connect to a TiDB Serverless database using mysqlclient on a Linux operating system.

Source: https://docs.pingcap.com/tidbcloud/dev-guide-sample-application-python-mysqlclient.

Hey Shiyang, this is Xin, Product Manager of TiDB Serverless. Thank you for posting the question. May I know which Linux distro you are using and the version of mysqlclient? What is the error message you are seeing? I guess the issue might be related to the cert configuration but need these details from you to better help.