Python Database Connection with
MySQL
(Python -MySQL Connector)
Step-1: Install Python + pip
Step-2: Install MySQL
Step-3: Python <> MySQL Using MySQL Connector
Python Download & Installation:
Open web browser and type the url: www.python.org
Click the download menu and download latest version of
python.
After download complete then double click to install the
python.
Click on customize installation Next
Click install
Close the installation Process.
MySQL Download & Installation:
Install MySQL Server 8.0.28
Install MySQL Workbench 8.0.28
Install MySQL shell 8.0.28
MySQL Download
Open web browser and type the url: www.mysql.com
Click the download menu and download latest version of
MySQL.
Click on MySQL Community (GPL) Download
Click on MySQL Installer for Windows.
Click to download MySQL
After download complete then double click to install
the MySQL.
Click on Next
Click on MySQL Server-> MySQL Server-> MySQL
Server 8.0->MySQL Server 8.0.28
User name: root
Create a root password(own)
Before finish, we start path setting
Copy the path link: C:\Program Files\MySQL\MySQL Server 8.0\bin
Go to Start Environmental VariablesSystem variables-> Click
path New -> paste the path (C:\Program Files\MySQL\MySQL
Server) ok
To search MySQL Shell in your window search bar
To search MySQL Workbench 8.0 CE in your window search bar
To check Installation of MySQL is complete or not?
To Open command Prompt in your System
Type mysql --version then Enter.
Type mysql --version -u root -p then Enter. (u->user & p->
password)
Enter your password
Type in command prompt (show databases;)
Create a database (sisiradb)
Type in command prompt (create database sisiradb;)
Press Enter
Type show databases;
Go to MySQL Workbench
Click MySQL Connection -> Local instance MySQL 8.0
To check Installation of Python is complete or not?
To Open command Prompt in your System
Type python --version then Enter.
To check Installation of pip is complete or not?
To Open command Prompt in your System
Type pip --version then Enter.
Open Python IDLE
To search IDLE in your window search bar
Click IDLE
Type import mysql.connector in IDLE then Enter
Install MySQL Connector
To open Command Prompt & type “pip install mysql-connector-
python”
Then Enter
Open IDLE
Type import mysql.connector in IDLE then Enter successfully
install mysql-connector
Example-1: Create a Program to check a database connection
is Establish
Create a python file (like db1.py)
To write the program like this
Program:
import mysql.connector
conn=mysql.connector.connect(host='localhost', database='sisiradb',
password='Mother#1', user='root');
if conn.is_connected():
print("Connection Established...")