As a novice in the world of Python, I was searching for a way to connect to mysql db using python. And found the cool example from devshed that shows connectivity using MySqLdb module. Default installation of Python may not contain MySQLdb Module, in that case you have to install it manually.
First check whether its already installed ot not:
Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66
19990314/Linux
(egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import MySQLdb
Traceback (innermost last):
File “”, line 1, in ?
ImportError: No module named MySQLdb
>>>
ImportError: No module named MySQLdb >> indicate that you dont have MySQLdb installed.
According to devshed it just 4 line matter to install MySQLdb as follows:
$ tar -xzvf MySQL-python-0.9.2.tar.gz
$ cd MySQL-python-0.9.2
$ python setup.py build
$ python setup.py install
I was trying to build it in My dedicated server running on CentOS.
Every time I am trying to run
>> python setup.py build
it end up with following error.
error: command ‘gcc’ failed with exit status 1
After some googling, some forum suggest to check whether gcc is installed or not,
Thats the problem, everyone was worried about only GCC, I installed it again and recheck, still the same
problem.
After spending some ‘faltoo’ time I found that the following things sould be installed within your system
to run MySQLdb properly
>> yum install python-devel
>> yum install MySQL-devel
>> yum install zlib-devel
>> yum install openssl-devel
Now its working fine
Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66
19990314/Linux
(egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import MySQLdb
>>>
(Gives no error)
Hi, thanks for your post, you saved my day after 1.5 hour research and messing around trying to install MySQL-python.
welcome Henry
Thx very much. It was very helpful
I have installed MySQLdb, but now I want to use the connect() function. I am not sure about the by default username and pass.
Any help will be highly appreciated.
Thanks a lot. This post saved my day, I was about to give up installing the driver.
Thanks for this.
You saved me at least half a day’s worth of work.
Im trying to use a specific python script with the Asterisk PBX and i found that i was lacking mySQLdb support, so glad i got it working with this.
Welcome Khuram.
[...] http://oligoli.wordpress.com/2009/03/21/problem-with-installing-mysqldb-in-python/ [...]
fantastic, this did the trick. Thanks!
Thanks alot…you saved me a lot of stress…but does anyone know what really is wrong with installing python setup.py install? I’ll really love to know.
Thanks once again
Thank you, this saved me a bunch of time, just wish I had found it 3 days ago
)