Complete tutorial on operating MySQL database in Python
1. Preface
In modern web development, data crawling, automated reporting and other scenarios, relational-database is still one of the core choices for structured data management. Among them, MySQL has become the most widely used database from entry to production environments due to its advantages of free open source, mature ecology, and stable performance.
This tutorial adopts the idea of from 0 to usable, and then to production optimization**, and uses concise and practical code examples to help you quickly master the core skills of operating MySQL in Python. The content covers environment-setup, secure connections, CRUD, transaction processing and ORM practice, with production-level best practices attached.
2. Environment preparation
2.1 Rapid deployment of MySQL (Docker)
If you don’t want to install the MySQL service directly on your local machine, using Docker to start it with one click is the fastest solution:
After startup, you can passdocker exec -it local-mysql8 mysql -uroot -p123456Enter the container and create a test database in advance:
You can also use graphical tools such as Navicat and DBeaver to connect.
2.2 Install Python driver library
Commonly used libraries for Python operations on MySQL include the following:
PyMySQL is the first choice at the entry stage, and can be combined with SQLAlchemy later to improve development efficiency. Install basic libraries:
cryptographyUsed to support MySQL 8.0 defaultcaching_sha2_passwordAuthentication plugin.
3. Safe and efficient database connection
3.1 Entry level: single basic connection
This connection method is suitable for quick verification and learning, and should not be used directly in production environments.
3.2 Production level: using connection pooling
Frequently creating and destroying database connections is extremely expensive, so connection pools must be used in production environments. recommendDBUtils:
Connection pool configuration example:
All subsequent database operations passget_connection()Obtain a connection and resources are managed uniformly by the connection pool.
4. Core CRUD operations
We pass astudentsTable to demonstrate addition, deletion, modification and query.
4.1 Initialize table structure
with get_connection() as connThe transaction will be automatically processed: if there is no exception, it will be submitted, and if an exception occurs, it will be rolled back.
4.2 Insert data
4.2.1 Single insertion
⚠️ placeholder must be used
%s, Never use string concatenation, otherwise there is a risk of SQL injection!
4.2.2 Batch insert (strongly recommended)
To insert 1,000 pieces of data, a single insertion may require 1,000 network interactions, while a batch insertion only takes about 1 time.
4.2.3 Update if it exists, insert if it does not exist (Upsert)
This is a necessary operation for crawler data synchronization and idempotent writing.
Return value rules: If the number of affected rows is 1, it means inserting a new record, and if it is 2, it means updating existing records.
4.3 Update, delete, query
Update data
Delete data
Query data
Single query:
Multiple queries:
Paging query:
When paging large amounts of data,
OFFSETIf it is too large, it will cause performance degradation. You can use cursor paging based on auto-incrementing primary keys instead:WHERE id > last_id LIMIT page_size
Large data volume streaming query:
When the result set may be very large, and to avoid loading all the data into memory at once, a streaming cursor can be used:
SSCursorThe result set will be left on the server side and read line by line by the client. The memory usage is constant, which is very suitable for exporting large amounts of data.
5. Transaction processing: ensuring data consistency
In key businesses such as transfers and orders, a series of operations are required to either all succeed or all fail (ACID characteristics). The following transfer example demonstrates the use of transactions.
because of usingwithstatement, even if it is not submitted explicitly, will automatically exit when there is no exception.commit(); Once an exception occurs, the context manager will executerollback(). explicit callrollback()It can make the intention clearer.
6. Advanced: Use SQLAlchemy ORM to simplify code
Although handwritten SQL is flexible, code readability and maintainability will decrease when the business logic is complex. SQLAlchemy As the most popular ORM for Python, it allows you to operate databases like objects.
Install:
Complete example:
Through ORM, query, update and delete become intuitive Python object operations, greatly reducing the error rate of handwritten SQL.
7. Production-level best practices and pitfalls to avoid
When using Python + MySQL in a production environment, be sure to adhere to the following principles:
- Connection pooling must be used: either
DBUtilsEither SQLAlchemy's built-in connection pool can significantly reduce connection overhead. - Must use parameterized query: all user input should pass
%sPlaceholders are passed in to prevent SQL injection. - Character sets are used uniformly
utf8mb4: Supports Emoji and full Unicode characters to avoid garbled characters. - Proper use of transactions: Key business logic (such as payment, inventory deduction) must be wrapped in transactions to ensure data consistency.
- Release resources in a timely manner: Make full use of
withstatement ortry-finallyClose the connection and cursor. - Batch operation efficiency improvement: Use it first when writing a large amount of data
executemany()or batchINSERTstatement. - Configure connection timeout and automatic reconnection: Settings
pool_recycle(maximum connection reuse time) andconnect_timeout, to prevent the connection from being disconnected idle. - Add indexes for commonly used query fields: Analyze slow query logs and optimize accordingly
WHERE、JOINConditional fields. - Decoupling of sensitive information: Configurations such as database passwords should be read from environment variables or the configuration center, and hard coding is strictly prohibited.
8. Summary
This tutorial starts with basic connections and gradually explains how to operate MySQL safely and efficiently in Python:
- Get started quickly: PyMySQL single connection, suitable for learning and gadgets.
- Production solution: PyMySQL + DBUtils connection pool, suitable for lightweight backend services.
- Advanced method: SQLAlchemy ORM, suitable for complex business systems, greatly improving development efficiency.
Mastering these contents is enough for you to handle most data interaction scenarios between Python and MySQL. With good habits of transactions, batch operations, and resource management, you can write database code that is both robust and efficient.

