Using MySQL database with Python 3.4 under Windows 7

Using MySQL database with Python 3.4 under Windows 7

The detailed process of using MySQL database with Python 3.4 is as follows

Windows version: Windows7-64bit
Python version: python3.4.14-32bit
MySQL version: MySQL 5.7.17

1. MySQL Community Server installation:

1.mysql-5.7.17-win64.zip download

URL: https://dev.mysql.com/downloads/mysql/

For the Windows version, I chose mysql-5.7.17-win64.zip for download. The two ZIP files at the bottom are for testing and can be ignored.

這里寫圖片描述

If you are not logged in, you can choose to download directly.

這里寫圖片描述

2. Unzip the ZIP file and extract it to the directory where you want to install it.

3. Configure environment variables.

Find the location of the decompressed file, the screenshot is as follows:

這里寫圖片描述

The variable value I configured is D:\MySQL\mysql-5.7.17-winx64\bin. Note that a semicolon - ";" needs to be added in front of this. The screenshot is as follows:

這里寫圖片描述

4. Configure the my.ini file.

Copy the my-default.ini file to the current directory and rename it to my.ini. As shown below:

這里寫圖片描述

Open the my.ini file and configure it as follows (Note: the path setting needs to be escaped with the escape character "\", or use a backslash "/", and comment out the last line):

這里寫圖片描述

5. Initialize the database and configure related information (the command execution order must be correct, otherwise initialization will fail)

(1) Run the Windows command line cmd as an administrator and enter the bin directory of the installation directory as follows:

這里寫圖片描述

(2) Initialize the data directory:

Enter the command (to create a root user without a password):

mysqld --initialize-insecure 

這里寫圖片描述

At this time, a data folder will be generated under the mysql folder, which contains some folders and files, which indicates that the initialization is successful.

(3) Register MySQL service:

Enter the command:

mysqld -install

The following figure appears, indicating that the registration is successful.

這里寫圖片描述

(4) Start the MySQL service:

Enter the command:

 net start mysql

The following figure appears, indicating that the startup is successful.

這里寫圖片描述

(5) Set password:

Enter the command:

mysqladmin -u root password password

(6) Log in to mysql using the password:

Enter the command:

mysql -u root -p

The final screenshot is as follows:

這里寫圖片描述

At this point, MySQL configuration is complete.

If you want to exit the MySQL service, use the command:

exit

If you want to remove the MySQL service, use the command:

mysqld --remove

2.Connector/Python installation:

The command window (cmd) uses the following command: easy_install pymysql3

After python3, MySQLdb was replaced by pymysql. Depending on the version used, different connectors are installed.

Note: This needs to be used after python 3.4 is installed. The results are as follows:

這里寫圖片描述

3. Test

The test.py program is written as follows:

import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='meditation',db='mysql')
cursor = conn.cursor()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone()
print("MySQL server version:", row[0])
cursor.close()
conn.close()

Run test.py to view the results, indicating that the configuration is successful.

這里寫圖片描述

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of connecting to mssql database using Python
  • Python implements data visualization to see how to monitor your crawler status [recommended]
  • Python MNIST handwriting recognition data calling API method
  • Python data analysis matplotlib sets the spacing between multiple subgraphs
  • Python example of reading and saving camera data
  • How to batch modify the database and execute Sql files using Python
  • Python+Pandas Get the database and add the DataFrame example
  • How to use Python pymysql to read data from MySQL database
  • Python batch modification/replacement data example
  • Analyzing Python request data

<<:  Automatically log out inactive users after login timeout in Linux

>>:  How React Hooks Work

Recommend

Detailed explanation of mysql transaction management operations

This article describes the MySQL transaction mana...

Implementation of vue-nuxt login authentication

Table of contents introduce Link start Continue t...

html opens a new window with a hyperlink and can control window properties

1. The window size opened by the HTML hyperlink C...

5 things to note when writing React components using hooks

Table of contents 01. Use useState when render is...

Detailed explanation of dynamically generated tables using javascript

*Create a page: two input boxes and a button *Cod...

How to modify the default storage location of Docker images (solution)

Due to the initial partitioning of the system, th...

VMware configuration VMnet8 network method steps

Table of contents 1. Introduction 2. Configuratio...

Detailed explanation of log processing of Docker containers

Docker has many log plug-ins. The default is to u...

Implementation of textarea adaptive height solution in Vue

Table of contents Hidden Problems Solution to ada...

How to use CSS to achieve data hotspot effect

The effect is as follows: analyze 1. Here you can...

Solution to ONLY_FULL_GROUP_BY error in Mysql5.7 and above

Recently, during the development process, the MyS...

Solution to Chinese garbled characters when operating MySQL database in CMD

I searched on Baidu. . Some people say to use the...

js method to delete a field in an object

This article mainly introduces the implementation...

Implementation of MySQL scheduled backup script under Windows

On a Windows server, if you want to back up datab...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...