How to install MySQL connector in Python Ubuntu?

Python provides several ways to connect to a MySQL database and process data. This article describes three methods.

The MySQL databases and users must already exist before you can use any of the following methods. For information about how to manage MySQL databases using cPanel, please see this article.

Connecting to MySQL using Python

Before you can access MySQL databases using Python, you must install one (or more) of the following packages in a virtual environment:

  • mysqlclient: This package contains the MySQLdb module. It is written in C, and is one of the most commonly used Python packages for MySQL.
  • mysql-connector-python: This package contains the mysql.connector module. It is written entirely in Python.
  • PyMySQL: This package contains the pymysql module. It is written entirely in Python.

All three of these packages use Python's portable SQL database API. This means that if you switch from one module to another, you can reuse almost all of your existing code (the code sample below demonstrates how to do this).

Setting up the Python virtual environment and installing a MySQL package

To set up the Python virtual environment and install a MySQL package, follow these steps:

  1. Log in to your account using SSH.
  2. To create a virtual environment, type the following commands:
    cd ~
    virtualenv sqlenv
    

    • The virtualenv command creates a virtual environment named sqlenv, and subsequent commands in this procedure assume that the environment is named sqlenv. You can use any environment name you want, but make sure you replace all occurrences of sqlenv with your own environment name.
    • If you are running an alternate Python version (for example, you manually configured a newer Python version for your account as described in this article), you can specify that version for the virtual environment. For example, to install a user-configured version of Python 3.8 in the virtual environment, you could use the following command:
      virtualenv -p /home/username/bin/python3.8 sqlenv
      

  3. To activate the virtual environment, type the following command:

    source sqlenv/bin/activate

    The command prompt now starts with (sqlenv) to indicate that you are working in a Python virtual environment. All of the following commands in this procedure assume that you are working within the virtual environment. If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following the steps below and running the sample code.

  4. To update pip in the virtual environment, type the following command:

    pip install -U pip
  5. Type the command for the package you want to install:

    • To install the mysqlclient package, type the following command:
      pip install mysqlclient
    • To install the mysql-connector-python package, type the following command:

      pip install mysql-connector-python
    • To install the pymysql package, type the following command:

      pip install pymysql
Code sample

After you install a MySQL package in the virtual environment, you are ready to work with actual databases. The following sample Python code demonstrates how to do this, as well as just how easy it is to switch between the different SQL package implementations. The sample code works with Python 2.7 and Python 3.x.

In your own code, replace username with the MySQL database username, password with the database user's password, and dbname with the database name:

#!/usr/bin/python

from __future__ import print_function

hostname = 'localhost'
username = 'username'
password = 'password'
database = 'dbname'

# Simple routine to run a query on a database and print the results:
def doQuery( conn ) :
    cur = conn.cursor()

    cur.execute( "SELECT fname, lname FROM employee" )

    for firstname, lastname in cur.fetchall() :
        print( firstname, lastname )


print( "Using mysqlclient (MySQLdb):" )
import MySQLdb
myConnection = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database )
doQuery( myConnection )
myConnection.close()

print( "Using mysql.connector:" )
import mysql.connector
myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
doQuery( myConnection )
myConnection.close()

print( "Using pymysql:" )
import pymysql
myConnection = pymysql.connect( host=hostname, user=username, passwd=password, db=database )
doQuery( myConnection )
myConnection.close()

This example creates a series of Connection objects that opens the same database using different MySQL modules. Because all three MySQL modules use the portable SQL database API interface, they are able to use the code in the doQuery() function without any modifications.

When you have a Connection object associated with a database, you can create a Cursor object. The Cursor object enables you to run the execute() method, which in turn enables you to run raw SQL statements (in this case, a SELECT query on a table named employee).

As you can see, Python's portable SQL database API makes it very easy to switch between MySQL modules in your code. In the sample above, the only code changes necessary to use a different module are to the import and connect statements.

How to install Python MySQL connector in Ubuntu?

Install MySQL Connector Python on Ubuntu.
Go to download MySQL Connector python for Ubuntu from here..
Select the Operating system Ubuntu Linux from the drop-down..
Select the OS version as an architecture-independent. ... .
You should get two entries fro DEB Package python for MySQL Connector Python (For python 2 and Python 3)..

How to install Python Connector for MySQL?

Installation: To install Python-mysql-connector module, one must have Python and PIP, preinstalled on their system. To check if your system already contains Python, go through the following instructions: Open the Command line(search for cmd in the Run dialog( + R).

How to install MySQL JDBC connector in Ubuntu?

Install the driver on a Linux system.
Log in to the server hosting Web Help Desk..
Navigate to the targeted directory. If you are running Web Help Desk 12.6 and below, navigate to: ... .
Copy the JDBC driver into the lib directory..
Restart Web Help Desk. The MySQL JDBC driver is installed on your system..

How can I install MySQL connector?

Installing Connector/Python from source.
A C/C++ compiler, such as gcc..
Protobuf C++ (version >= 3.6. ... .
Python development files..
MySQL Connector/C or MySQL Server installed, including development files to compile the optional C Extension that interfaces with the MySQL C client library..