Connect to mysql from Python

Kailash Chandra Behera | Wednesday, July 22, 2020

Introduction

python mysqldb module provides access to python mysqlclient to connect with Mysql Server Database. This blog provides mysqldb python install guidelines to download mysql connector for pythonto and install python mysqlclient library in local machine.

Getting Started

To connect mysql from python the mysql_python required to install on the local machine, here 1st we will see how to install that then will see the code example of python connect to mysql.

Installing PyMySQL is not a big task for Python developers. A single line of command installs PyMySQL library into a machine. Before installing PyMySQL make sure that Python and pip are installed in your machine.

If you are new in Python, refer my articles to install Python and pip. These two articles are helping to install Python and pip. Pip is a Python Package Manager tool that helps to install Python packages or modules into the machine. Hence before installing any packages or modules pip must have installed in the machine.

The below command is for pip install mysql connector in local machine.

PIP Install Windows

 pip install PyMySQL  

install pip ubuntu

 sudo -H pip install PyMySQL  

pip install mysql connector

This demonstration is conducted in Microsoft Windows 10 and applicable in Windows only, to install the PyMySQL, follow the below steps.

  1. Press Windows Key+R key
  2. Enter cmd.exe and press Enter
  3. The command prompt will be appeared
  4. Use below command and press Enter.
     pip install PyMySQL  
    
  5. The installation process will be started, if everything going fine then the package or module will be installed successfully.
    download mysql connector for python

    mysqldb python install(mysql_python)

Python Connect to Mysql

To connect the MySQL database, you must know the details database you want to connect. Below are the parameters required to connect to mysql from Python.

  1. Host Name:- Server name or Ip address on which MySQL is running. if you are running on localhost, then you can use localhost, or it’s IP
  2. Database Name:- Database name to which you want to connect.
  3. Username:- The username that you use to work with MySQL Server. The default username for the MySQL database is a root
  4. Password:- Password is given by the user at the time of installing the MySQL database. If you are using root then you won’t need the password.
 import mysql.connector  
 from mysql.connector import Error  
 try:  
   connection = mysql.connector.connect(host='local_host',  
                      database='database_name',  
                      user='user_name',  
                      password='login_password')  
   if connection.is_connected():  
     db_Info = connection.get_server_info()  
     print("Connected to MySQL Server version ", db_Info)  
     cursor = connection.cursor()  
     cursor.execute("SQL Query")  
     record = cursor.fetchone()  
     print("You're connected to database: ", record)  
 except Error as e:  
   print("Error while connecting to MySQL", e)  
 finally:  
   if (connection.is_connected()):  
     cursor.close()  
     connection.close()  
     print("MySQL connection is closed")  

python mysql connector example

Thanks