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

Seven different color schemes for website design experience

The color matching in website construction is ver...

Issues with locking in MySQL

Lock classification: From the granularity of data...

Implementation process of nginx high availability cluster

This article mainly introduces the implementation...

Simple understanding and examples of MySQL index pushdown (ICP)

Preface Index Condition Pushdown (ICP) is a new f...

React handwriting tab switching problem

Parent File import React, { useState } from '...

MySQL batch adding and storing method examples

When logging in to the stress test, many differen...

Why TypeScript's Enum is problematic

Table of contents What happened? When to use Cont...

10 key differences between HTML5 and HTML4

HTML5 is the next version of the HTML standard. M...

JavaScript Prototype Details

Table of contents 1. Overview 1.1 What is a proto...

Detailed explanation of how to select all child elements using CSS

How to recursively select all child elements usin...

Detailed examples of Linux disk device and LVM management commands

Preface In the Linux operating system, device fil...

css3 animation ball rolling js control animation pause

CSS3 can create animations, which can replace man...

Application of Beautiful Style Sheets in XHTML+CSS Web Page Creation

This is an article written a long time ago. Now it...