Install MySQLdb for Python 2.6 in Windows 7 64-bit
Getting this to work took me about 5 hours (lots of trial and error, lots of patience), there might be a better/faster/easier way to get this adapter to work but i couldn’t find it (and believe me, i searched like crazy).
These are the steps i took to get MySQLdb to work:
- Download and install MySQL Community Server 5.1.49 Windows (x86, 64-bit), MSI Installer (mysql-5.1.49-winx64.msi)
IMPORTANT: you must get the non-essentials version of the installer, this version contains the C header files you’ll need later on to compile the MySQL adapter. Also, even though this installer has these headers they are not installed by default. Do a custom setup and make sure to mark the ‘C Include Files / Lib Files’ under ‘Developer Components’. Once installed you should have a ‘include’ folder with a bunch of .h files in your MySQL installation folder.
- Download and install Visual C++ 2008 Express Edition
- Download Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1. * When installing this, under ‘Developer Tools’ you only need to check ‘Windows Headers and Libraries’ and ‘Visual C++ Compilers’.
After installing Visual C++ 2008 Express Edition and Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 you should have ‘amd64’, ‘ia64’, ‘x86_amd64’ and ‘x86_ia64’ folders under ‘C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin’.
- Download and install Python 2.6.5 X86-64 (python-2.6.5.amd64.msi)
This should install Python in ‘C:\Python26’, make sure this folder is referenced in your PATH variable (hint). While you are in there also add ‘C:\Python26\Scripts’ to the PATH variable.
- Download and un-tar/un-gzip MySQL for Python (MySQL-python-1.2.3.tar.gz)
Open a command prompt and cd into that folder and run:
python ez_setup.py
This will download and install setuptools, since you already added the scripts subfolder to the PATH (you did add it, right?) run:
easy_install -b C:\temp\mysqldb mysql-python
This should fail, you still need to tweak some files. First, go into the ‘C:\temp\mysqldb’ folder, open the ‘site.cfg’ and change the ‘registry_key’ to:
registry_key = SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.1
By default MySQL creates the registry string in ‘SOFTWARE\MySQL AB\MySQL Server 5.1’, but for the 64-bit version it goes into ‘Wow6432Node’.
Now, you need to apply a diff patch to ‘msvc9compiler.py’ under ‘C:\Python26\Lib\distutils’, once you have your favorite patch tool ready download the diff patch (it will also try and patch ‘Lib/distutils/tests/test_msvc9compiler.py’ but you can skip this). After this patch is applied search for the line that contains “ld_args.append(‘/MANIFESTFILE:’ + temp_manifest)” and after it add another line:
ld_args.append(‘/MANIFEST’)
Now, you can finally cd into ‘C:\temp\mysqldb’ and run:
python setup.py clean
python setup.py install
And that’s it. The mysql adapter should compile and the mysql python package should be installed and ready to use. Test this by writing ‘import MySQLdb’ in python interactive, no errors should pop up. Also you can test connectivity to a MySQL server with this:
import MySQLdb
conn = MySQLdb.connect (host = "localhost",
user = "root",
passwd = "",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()
Good Luck. Hope this works for you!
These are all the links i used to research the steps and debug all the errors i got while trying to get this to run:
http://stackoverflow.com/questions/645943/mysql-for-python-in-windows/817760#817760
http://www.fuyun.org/2009/12/install-mysql-for-python-on-windows/
http://bugs.python.org/issue7511
http://www.mathworks.com/support/solutions/en/data/1-6IJJ3L/index.html?solution=1-6IJJ3L