In This Article or tutorial we will learn about, how to connect Python with MySQL database ( Python – MySQL Setup & Connectivity).
As you know to build real world applications or software, databases connectivity is compulsory. Python Programming language also support databases like MySQL, SQLite, MonogoDB, GadFly and Many others..
First of all you need to install MySQL in your system. You can download MySQL free from following link
Install MySQL Driver [MySQL Connector]
Python programming language must needs “MySQL Connector” driver to access or connect with database.
You can install “MySQL Connector” using PIP. PIP already installed in Python environment.
To install “MySQL Connector”, open your command prompt, goto the location of PIP.
And type the following command and press enter to install MySQL Driver
python -m pip install mysql-connector-python
Check the following screenshot to help
Create Python Connection with MySQL
To connect the Python with MySQL database you need username and password of your localhost. You set this username and password during installation of MySQL database. In my case username is “root” and password is “123456”.
Check the following example to understand
Example (Python – MySQL Connectivity)
import mysql.connector as mc
mydb = mc.connect(host='localhost', user='root', password= '123456')
<mysql.connector.connection.MySQLConnection object at 0x02BD2598>