Connect to TiDB with mysqlclient
TiDB is a MySQL-compatible database, and mysqlclient is a popular open-source driver for Python.
In this tutorial, you can learn how to use TiDB and mysqlclient to accomplish the following tasks:
- Set up your environment.
- Connect to your TiDB cluster using mysqlclient.
- Build and run your application. Optionally, you can find sample code snippets for basic CRUD operations.
Note
This tutorial works with TiDB Serverless, TiDB Dedicated, and TiDB Self-Hosted.
Prerequisites
To complete this tutorial, you need:
- Python 3.10 or higher.
- Git.
- A TiDB cluster.
If you don’t have a TiDB cluster, you can create one as follows:
- (Recommended) Follow Creating a TiDB Serverless cluster to create your own TiDB Cloud cluster.
- Follow Deploy a local test TiDB cluster or Deploy a production TiDB cluster to create a local cluster.
If you don’t have a TiDB cluster, you can create one as follows:
- (Recommended) Follow Creating a TiDB Serverless cluster to create your own TiDB Cloud cluster.
- Follow Deploy a local test TiDB cluster or Deploy a production TiDB cluster to create a local cluster.
Run the sample app to connect to TiDB
This section demonstrates how to run the sample application code and connect to TiDB.
Step 1: Clone the sample app repository
Run the following commands in your terminal window 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
Run the following command to install the required packages (including mysqlclient
) for the sample app:
pip install -r requirements.txt
If you encounter installation issues, refer to the mysqlclient official documentation.
Step 3: Configure connection information
Connect to your TiDB cluster depending on the TiDB deployment option you’ve selected.
- TiDB Serverless
- TiDB Dedicated
- TiDB Self-Hosted
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
Ensure the configurations in the connection dialog match your operating environment.
- Endpoint Type is set to
Public
- Branch is set to
main
- Connect With is set to
General
- Operating System matches your environment.
Tip
If your program is running in Windows Subsystem for Linux (WSL), switch to the corresponding Linux distribution.
- Endpoint Type is set to
Click Generate Password to create a random password.
Tip
If you have created a password before, you can either use the original password or click Reset Password to generate a new one.
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .env
Copy and paste the corresponding connection string into the
.env
file. The example result is as follows:TIDB_HOST='{gateway-region}.aws.tidbcloud.com'
TIDB_PORT='4000'
TIDB_USER='{prefix}.root'
TIDB_PASSWORD='{password}'
TIDB_DB_NAME='test'
CA_PATH=''
Be sure to replace the placeholders
{}
with the connection parameters obtained from the connection dialog.TiDB Serverless requires a secure connection. Since the
ssl_mode
of mysqlclient defaults toPREFERRED
, you don’t need to manually specifyCA_PATH
. Just leave it empty. But if you have a special reason to specifyCA_PATH
manually, you can refer to the TLS connections to TiDB Serverless to get the certificate paths for different operating systems.Save the
.env
file.Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
Click Allow Access from Anywhere and then click Download CA cert to download the CA certificate.
For more details about how to obtain the connection string, refer to TiDB Dedicated standard connection.
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .env
Copy and paste the corresponding connection string into the
.env
file. The example result is as follows:TIDB_HOST='{host}.clusters.tidb-cloud.com'
TIDB_PORT='4000'
TIDB_USER='{username}'
TIDB_PASSWORD='{password}'
TIDB_DB_NAME='test'
CA_PATH='{your-downloaded-ca-path}'
Be sure to replace the placeholders
{}
with the connection parameters obtained from the connection dialog, and configureCA_PATH
with the certificate path downloaded in the previous step.Save the
.env
file.Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .env
Copy and paste the corresponding connection string into the
.env
file. The example result is as follows:TIDB_HOST='{tidb_server_host}'
TIDB_PORT='4000'
TIDB_USER='root'
TIDB_PASSWORD='{password}'
TIDB_DB_NAME='test'
Be sure to replace the placeholders
{}
with the connection parameters, and remove theCA_PATH
line. If you are running TiDB locally, the default host address is127.0.0.1
, and the password is empty.Save the
.env
file.
Step 4: Run the code and check the result
Execute the following command to run the sample code:
python mysqlclient_example.py
Check the Expected-Output.txt to see if the output matches.
Sample code snippets
You can refer to the following sample code snippets to complete your own application development.
For complete sample code and how to run it, check out the tidb-samples/tidb-python-mysqlclient-quickstart repository.
Connect to TiDB
def get_mysqlclient_connection(autocommit:bool=True) -> MySQLdb.Connection:
db_conf = {
"host": ${tidb_host},
"port": ${tidb_port},
"user": ${tidb_user},
"password": ${tidb_password},
"database": ${tidb_db_name},
"autocommit": autocommit
}
if ${ca_path}:
db_conf["ssl_mode"] = "VERIFY_IDENTITY"
db_conf["ssl"] = {"ca": ${ca_path}}
return MySQLdb.connect(**db_conf)
When using this function, you need to replace ${tidb_host}
, ${tidb_port}
, ${tidb_user}
, ${tidb_password}
, ${tidb_db_name}
and ${ca_path}
with the actual values of your TiDB cluster.
Insert data
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
player = ("1", 1, 1)
cursor.execute("INSERT INTO players (id, coins, goods) VALUES (%s, %s, %s)", player)
For more information, refer to Insert data.
Query data
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute("SELECT count(*) FROM players")
print(cur.fetchone()[0])
For more information, refer to Query data.
Update data
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
player_id, amount, price="1", 10, 500
cursor.execute(
"UPDATE players SET goods = goods + %s, coins = coins + %s WHERE id = %s",
(-amount, price, player_id),
)
For more information, refer to Update data.
Delete data
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
player_id = "1"
cursor.execute("DELETE FROM players WHERE id = %s", (player_id,))
For more information, refer to Delete data.
Useful notes
Using driver or ORM framework?
The Python driver provides low-level access to the database, but it requires the developers to:
- Manually establish and release database connections.
- Manually manage database transactions.
- Manually map data rows (represented as tuples in
mysqlclient
) to data objects.
Unless you need to write complex SQL statements, it is recommended to use ORM framework for development, such as SQLAlchemy, Peewee, and Django ORM. It can help you:
- Reduce boilerplate code for managing connections and transactions.
- Manipulate data with data objects instead of a number of SQL statements.
Next steps
- Learn more usage of
mysqlclient
from the documentation of mysqlclient. - Learn the best practices for TiDB application development with the chapters in the Developer guide, such as Insert data, Update data, Delete data, Single table reading, Transactions, and SQL performance optimization.
- Learn through the professional TiDB developer courses and earn TiDB certifications after passing the exam.
Need help?
Ask questions on the Discord, or create a support ticket.
Ask questions on the Discord, or create a support ticket.