Python programming is popular for writing codes for complex and huge datasets. The language is useful for developers who are well versed with command based tools.
Many databases can be connected with Python when working on applications. In this article, the users shall know how to install MySQL that can be used with Python in the Windows system.
Table of Contents
Below are the things to be considered before installing and configuring MySQL in the Windows system.
- Choose the full installer that contains MySQL and MySQL connector for python
- Check in Python whether MySQL connector is available as a module or not
- Note and save the Environment Variables path set for python in the system
Go to www.mysql.com
From the top bar, navigate to DOWNLOADS. The downloads page is shown.
Scroll down to view the hyperlink – MySQL Community (GPL) Downloads. The page is shown as below.
Clicking the hyperlink redirects you to this page.
Next, the page below asks you to choose the MySQL package to be installed.
Choose the second option from the top – mysql-installer-community which also includes MySQL connector.
Choose Download. Redirecting the user to proceed with download with Login or without Login.
Choose “No thanks, just start my download”. The download starts with the pop up below.
Once downloaded, go to the folder in which the msi package is downloaded. If you do not know the folder, just search with the keyword, mysql. You shall see it like this.
Before executing the MySQL package, do the following.
Check MySQL connector is established in Python.
To do so, enter the IDLE keyword in the search bar and press ENTER.
Enter the command – “help (‘modules’). Press ENTER.
The results are displayed. Check whether the MySQL module is available or not. In the results that are displayed, MySQL is not available. This means MySQL connector is not available.
Next, check the path variables of Python installed in the system. To do so, go to the advanced system settings.
From the Advanced tab, click Environment Variables.
Choose Path from the User variables section on the upper side of the dialog box.
Click Edit. The list of paths will be shown.
Note all these paths in one notepad file and save it.
Now, right-click the msi package (of MySQL) and click install. The wizard is available.
Tick the I accept the license terms checkbox. Choose Next.
Keep the Developer Default option selected. Choose Next.
Choose Next without selecting any of the options shown in the above screen.
Click Yes on the pop up that is shown.
The Installation screen shall appear as below.
Click Execute to start installation of all the components stated in the screen above.
Wait for a while to complete execution.
You will observe Connector Python is also included in this. Press Next.
Press Next. The configuration settings to be applied.
Config Type = Set as Development Computer
Port = 3306 (If this port is being used by another component, please change it.)
Keep other settings as is.
Set the root password in the screen below.
Once the password is entered, the Next button is activated.
Untick the checkbox for “Start the MySQL Service at System Startup”.
Press Next. Click Execute.
Once the configuration is applied, click Finish.
You will redirect back to the Product Configuration step.
Skip the configuration for Server. You shall see the below screen.
Enter the root password you set for DB and click Check. You shall see success like this.
Click Next. You shall see the screen to apply configuration.
Click Execute. Once done, click Finish. It will be redirected back to the Product Configuration step.
Untick the two checkboxes and click Finish.
You have successfully installed MySQL.
You will see the path variables are overridden with MySQL as below.
Now copy this path and paste in the notepad file you saved.
Now, put sem-colon (;) at the end of each line and copy the entire text.
Paste the text in the path text field of Environment Variables.
Once pasted, click OK. All the paths will be shown.
Now, open the command prompt and enter the path where MySQL’s bin folder is located.
Type the command above and ENTER.
Type the command: mysql -u root -p. ENTER.
Enter the root password you set at the time of installation. Press ENTER>
Type the Show Databases command to check if the MySQL database is working.
You will see the databases available.
Go to the search bar of Windows and type IDLE to get the Python command line.
Type the command – help (‘modules’). Press ENTER. The modules list contains “mysql”.
This shows that you can connect python code to MySQL database successfully.
Create a python file with .py extension. In this example: transpire.py
import mysql.connector conn = mysql.connector.connect(user='root', password 'Plri@1234', host='localhost', database='world')
Now, enter the command on the command line: python transpire.py
Press ENTER. You will see the file will be compiled without any error.
It is always recommended to check path variables when connecting to different components in the development environment. Otherwise, wizard based installations are easy in the Windows system.