Application environment: dev
Reproduction method:
Problem:
cannot connect to TiDB serverelss database via mysqlclient
cannot connect to TiDB serverelss database via mysqlclient
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:
Before you begin, ensure you have the following:
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
Install the required packages, including mysqlclient
, by running:
pip install -r requirements.txt
If you encounter installation issues, refer to the mysqlclient official documentation.
Navigate to the Clusters Page: Go to the Clusters page and click the name of your target cluster to access its overview page.
Open the Connection Dialog: Click Connect in the upper-right corner. Ensure the configurations in the connection dialog match your operating environment:
Public
.General
.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.
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.
Save the .env
File.
Execute the following command to run the sample code:
python mysqlclient_example.py
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.