While most users will not have a need to use Python to connect to their MySQL database, there have been a few times when we've had questions about how to do that with Python.

At this point in the process you should already created a MySQL database and have a user assigned to that database.

Database Settings for Python Script

You will need the following settings for your script. Please note that the settings we are including below are for our testing purposes, please replace these with your settings (database host should stay the same).

database name: whhsup5_python
database user: whhsup5_python
database password: pytest
database host: localhost

Once we have the database setup, we can create our script and save it to the cgi-bin folder as pythontest.py (name your file however you like, just remember the .py extension). Once the file has been created, change the fle permissions to 755 (as all files in the cgi-bin should be).

This script will connect to and run the SELECT VERSION(), command which will show us what version of MySQL our database is running.

Python Script

Note: You must have the following line of code at the top of your your .htaccess file to be able to run Python scripts:
AddHandler cgi-script .cgi .pl .py

#!/usr/bin/env python

import MySQLdb

# connect to the database
db = MySQLdb.connect("localhost","whhsup5_python","pytest","whhsup5_python" )

# setup a cursor object using cursor() method
cursor = db.cursor()

# run a sql question
cursor.execute("SELECT VERSION()")

# grab one result
data = cursor.fetchone()

# begin printing data to the screen
print "Content-Type: text/html"


print """\
<title>Python - Hello World</title>

print "Database version : %s " % data


# close the mysql database connection

Now, let's bring up pythontest.py in a browser and we should see:


Congratulations, you've just used Python to connect to your MySQL database!

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Did you find this article helpful?


n/a Points
2015-05-17 7:43 pm

This was very helpful but I'm still having issues.  After following the directions above I am getting the following error.  Any ideas?

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@bitcoax.flintknap.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

14,905 Points
2015-05-18 5:04 pm
Hello Jason,

A 500 error can be caused by a number of things. You may want to check out our article on 500 errors to see if that helps you.

Kindest Regards,
Scott M

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.