MySQL database usage tutorial (2024 latest version)
1. Introduction to MySQL
MySQL is the world's most popular open source relational-database management system (RDBMS). Since its birth in 1995, it has become the database of choice for the vast majority of web application development due to its lightweight, stable and highly compatible characteristics.
For example: If an application is compared to a company, data is the company's assets, and MySQL is like an efficient and secure electronic warehouse, responsible for storing, searching, and updating these assets in an orderly manner.
Core Features
- High performance, high reliability: After verification in numerous high-concurrency scenarios, the default InnoDB storage engine supports automatic recovery from crashes.
- Cross-platform: Runs seamlessly on Windows, Linux, macOS.
- Flexible storage engine: InnoDB is recommended by default (supports transactions, row-level locks and foreign keys), while MyISAM (suitable for read-only analysis scenarios) is also retained.
- Standard SQL syntax: Follows SQL92, SQL99 and other standards, with low learning cost and easy migration.
- Full Unicode Support: Passed
utf8mb4Character set that can store all emoji and rare words.
MySQL vs SQLite quick comparison
Many students will ask: If you use SQLite for small projects, when should you upgrade to MySQL? The table below will help you clarify your thoughts.
If you are developing an application that requires simultaneous access by multiple users and a growing amount of data, then MySQL is the right choice for you.
2. MySQL installation and configuration
As of 2024, the officially recommended stable version is MySQL Community Server 8.4 LTS, which provides long-term maintenance for up to 5 years and is very suitable for production environments.
2.1 Traditional installation (suitable for deep customization)
If you need to install and perform various low-level tuning directly on the operating system, you can do this:
- Visit MySQL 官方下载中心 and select the installation package corresponding to the platform.
- Follow the wizard to complete the installation. Be sure to set a strong password (recommendation: uppercase and lowercase letters + numbers + special symbols, length ≥ 8 characters).
- Remember to adjust the character set settings after installation (see next section).
2.2 Core configuration optimization
No matter which installation method is used, it is strongly recommended to configure the character set and default storage engine immediately to avoid headaches such as Chinese garbled characters in the future.
- Linux/macOS configuration file path is usually
/etc/my.cnfor/etc/mysql/my.cnf - For Windows, it is in the installation directory.
my.inidocument
Add or modify the following content in the configuration file:
After the modification is completed, restart the MySQL service, then log in to the database and execute the following command to verify:
You should see output similar to the following (key items areutf8mb4):
3. Docker rapid deployment (highly recommended!)
If you don’t want to mess with the environment and hate all kinds of dependency conflicts, then Docker is your savior. Start it with one click and go away after use, and the data can be persisted.
3.1 One-click start command
Run the following line of commands to start a container with MySQL 8.4 LTS (remember to change the password to your own):
3.2 Detailed explanation of parameters
3.3 Verify deployment
- Check if the container is running:
- Enter the container and connect to MySQL using the command line:
Enter the password you just set and seemysql>The prompt indicates success!
4. Python connects to MySQL
In the Python world, we recommend using the official drivermysql-connector-python. It fully follows the Python DB-API 2.0 specification, is stable and has good compatibility.
4.1 Install driver
4.2 Complete operation example
The following example covers all core steps from connection, table creation, addition, deletion, modification, transaction submission, and resource release.
5. Daily development best practices
5.1 Security aspects
- Don’t use root for everything: Create dedicated users for each application and strictly limit their IP access.
- Strongly use placeholders: Never use string concatenation in SQL.
WHERE name = '" + user_input + "'Code like this is a doorway to disaster. - Encrypted transmission in production environment: Turn on SSL/TLS to prevent data from "streaking" in the network.
- Regular Backup: Available
mysqldumpScheduled export can also directly back up the data directory mounted by Docker.
5.2 Performance
- Use connection pool: Database connections are very valuable resources, and repeated establishment and destruction will seriously slow down the application.
mysql-connector-pythonComes with connection pool support. - Reasonable Index Creation: Give
WHERE、JOIN、ORDER BYWith the addition of indexes to the fields involved, the query speed can often change from seconds to milliseconds. - Batch operation as much as possible: If you can insert 100 pieces of data at a time, don't loop 100 times.
executemanyOr inserting multiple rows with a single SQL is a good practice. - Avoid inefficient queries: Don't
SELECT *out all fields, let alone useLIKE '%keyword'This writing method will lead to a full table scan.
6. Summary
- Version Selection: New projects can be directly upgraded to MySQL 8.4 LTS for stability and longevity.
- environment-setup: Docker is preferred for development and testing, with fast deployment and clean environment.
- Python operation: Use the official driver, pay attention to Explicitly commit transactions and Release resources in a timely manner.
- Long-Term Development: Adhering to security and performance guidelines from the beginning will allow you to avoid 90% of pitfalls.

