Using python, you can connect and run queries against a MySQL database on your server.

In this example, we've created a test script named test_db.py and placed it in our cgi-bin folder. We also updated the file's permissions to 755.

We also used the following settings within the code:

  • database name: inmoti6_pytest
  • database user: inmoti6_pytest
  • database password: pytest
  • database host: localhost

We're connecting to the database and running SELECT VERSION(), which shows us which version of MySQL we are running.

#!/usr/bin/env python

import MySQLdb

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

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

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

# grab one result
data = cursor.fetchone()

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

print

print """\
<html>
<head>
<title>Python - Hello World</title>
</head>
<body>
"""

print "Database version : %s " % data

print"""\
</body>
</html>
"""

# close the mysql database connection
db.close()

When viewing this page in our browser, we see:

Database version : 5.0.92-community-log

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
Name

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

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!
Ask a Question
2012-09-20 10:22 am
Warning for other users on shared hosting (business) servers, as of 9/20/2012 this example appears to be broken due to MySQLdb library not being properly installed for the current version of python in /usr/lib/python2.4/site-packages on shared hosting servers.

Since you won't have SSH access as a business customer (you need to buy VPS or dedicated hosting for ssh access), you will not be able to install the setuptools-0.6c11 and MySQL-python-1.2.3 on your own. And the inMotion support folks said they are unwilling to add MySQLdb to the latest python version library directory on their shared servers.

I would encourage you to ask inMotion tech support to fix the python MySQLdb installation on your shared server. Maybe if enough customers ask for this they will eventually agree to do it.

The work-around that inMotion tech support suggested is to have my python CGI script call another external script (presumably written in PHP or Perl or some other scripting language that is able to access MySQL properly), and have the child script read/write data from/to my database on behalf of the python script. Very messy in my opinion.

I guess I will just stick with PHP or Perl for CGI for now whenever I need to access MySQL database since python is incapable of doing so on our server.
Staff
20,125 Points
2012-09-20 11:45 am
Hello noble,

I just responded to your original question with a resolution to your issue. Please check that to see if it will help you.

It seems that some select older servers do not have that module installed. As they are older servers and will likely soon be decommissioned, adding and updating them on the server wide level is not something the Systems team will do.

We can, however, move anyone who encounters this issue to newer servers that do have the capability. The newest servers also run an updated version of Python (2.6) and do have the MySQLdb module installed.

I do apologize for any misinformation you may have gotten with your support ticket. I will take measures to ensure a request of this nature is handled correctly in the future.

Best Regards,
Scott M
2012-10-29 2:15 pm
Is it possible to get something like bottle installed? I'm looking at bottle as my server engine and would really like to get it working here.

http://bottlepy.org/docs/dev/tutorial_app.html#server-setup

Thanks
Staff
17,716 Points
2012-10-29 3:22 pm
Hello Tmhudg,

If were only going to be affecting your, it may be possible to run it. But it appears to be more of a server-wide thing, which, since you're on a shared server account-type, is not going to be allowed. If you want to inquire with live support, you can email support@inmotionhosting.com and you'll get a response through email.

If you have any further questions, please contact technical support or leave a comment at the bottom of the page.

Regards,

Arnel C.

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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

4 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

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

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!