MySQL is a popular open source relational database management system, and Python is a high-level programming language. In actual development, we usually need to use Python to interact with MySQL in order to manage and retrieve data in the database. This article will introduce how to install MySQL and Python related packages under Windows and Linux systems to use MySQL in Python.
1. Installation of MySQL and Python under Windows system
First, we need to download and install the MySQL database server. We can download MySQL Community Server from the official MySQL website and install it according to the steps provided. During the installation process, please remember to set your MySQL username and password. At the same time, you can also choose to install the MySQL toolkit for MySQL management, backup, and recovery operations.
Next, we need to install the Python and PyMySQL libraries under the Windows system. The Python installation package can be downloaded from the Python official website, select the corresponding version, and follow the steps provided to install it. At the same time, we also need to use the pip tool to install the PyMySQL library. Use the following command in command line mode to complete the installation:
pip install PyMySQL
After installation, you can use the PyMySQL library in the Python program for MySQL data management.
2. Installation of MySQL and Python under Linux system
In Linux system, we can use apt-get or yum Wait for the package management tool to install the MySQL database server. Under the Debian/Ubuntu system, we can use the following command to install:
sudo apt-get install mysql-server
Under the CentOS system, we can use the following command to install:
sudo yum install mysql-server
During the installation process, you also need to set MySQL username and password.
We also need to install the Python and PyMySQL libraries under the Linux system. Python installation packages can be installed using package management tools such as apt-get or yum. Under the Ubuntu system, we can use the following command to install:
sudo apt-get install python3 python3-pip
Under the CentOS system, we can use the following command to install:
sudo yum install python3 python3-pip
Next, we also need to use the pip tool to install PyMySQL library. Use the following command in command line mode to complete the installation:
pip install PyMySQL
After installation, you can use the PyMySQL library in the Python program for MySQL data management.
3. Python operation MySQL example
After completing the installation of MySQL and Python, we can use the following sample code to manage MySQL data:
import pymysql # 打开数据库连接 db = pymysql.connect("localhost","testuser","test123","testdb" ) # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 使用 execute() 方法执行 SQL 查询 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取单条数据. data = cursor.fetchone() print ("Database version : %s " % data) # 关闭数据库连接 db.close()
This code will Connect to the MySQL database named testdb and print out the MySQL version number. Through the above code, we can understand how to use Python and the PyMySQL library for MySQL data management.
Summary
This article introduces how to install MySQL and Python related packages under Windows and Linux systems, and gives a sample program to show how to use Python and PyMySQL libraries for MySQL. Data management. Both MySQL and Python are open source tools and are commonly used tools for data management and processing. An in-depth understanding of the combination of MySQL and Python can help us process large amounts of data more efficiently.
The above is the detailed content of mysql python installation. For more information, please follow other related articles on the PHP Chinese website!