How to Use MySQLdb with Python and Django in Osx 10.6

How to use MySQLdb with Python and Django in OSX 10.6?

This issue was the result of an incomplete / incorrect installation of the MySQL for Python adapter. Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config - discussed in greater detail in this article: http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

Python mysqldb on Mac OSX 10.6 not working

I eventually managed to solve the problem by Installing python 2.7 with Mac Ports and installing mysqldb using Mac Ports - was pretty simple after that.

Django + MySQL on Mac OS 10.6.2 Snow Leopard

I have ultimately solved my own problem, with of course, the subconscious and conscious help from the many posts, blogs, and mail logs I've read. I would give links if I could remember.

In a nutshell, I reinstalled EVERYTHING using MacPorts.

After editing ~/.bash_profile and commenting out all the previous modifications to ${PATH}, I downloaded the dmg for Snow Leopard and ran through its installation.

Then opened the terminal and ran the self update.

sudo port selfupdate
sudo port install python26

That second part, installing Python 2.6, took forever. But when it completed it prompted me with the following:

To fully complete your installation and make python 2.6 the default, please run

sudo port install python_select
sudo python_select python26

I did both and they went quick.

I forgot to mention how handy 'port search ' command is. I searched for 'mysql' and similar to find the thing to type after 'install'. But I proceeded with reinstalling both the client and server for MySQL. Perhaps I did this in reverse order, but the end result worked fine.

sudo port install mysql5
...
---> Installing mysql5 @5.1.41_0
The MySQL client has been installed.
If you also want a MySQL server, install the mysql5-server port.

So naturally:

sudo port install mysql5-server

I love how the so many of the macports installations give you feedback as to what to do next. At the end of the server installation, it said the following:

******************************************************
* In order to setup the database, you might want to run
* sudo -u _mysql mysql_install_db5
* if this is a new install
******************************************************

It was a new install for me (didn't have any local schemas). For completeness, and for my own reference, here is the output of running that command:

Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h iMac.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/

Almost done. Earlier in my 'port search'ing I came across this interesting port:

py26-mysql @1.2.2 (python, devel, databases)
Python interface to mysql

With much, much hope that this would provide me with MySQLdb package, I installed it (and it did).

sudo port install py26-mysql

Afterwords I cranked up the python interpreter attempted to import MySQLdb, the very thing in my way all this time.

iMac:~ drhoden$ python
Python 2.6.4 (r264:75706, Dec 15 2009, 18:00:14)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet
>>>

A warning, but It worked!!

Just one more thing:

sudo port install py26-django

After all of this I was finally able to crank up my Django project and remotely connect to my company's MySQL server!! It may not have been necessary to reinstall Django using MacPorts, but I wasn't going to risk complications.

Having an issue with setting up MySQLdb on Mac OS X Lion in order to support Django

Easy,

edit your .bash_profile (vi ~/.bash_profile) somewhere in that add the following line:

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib"

This line assumes your mysql install directory is in /usr/local/mysql/.

This will solve executing via python interrupter launched in shell (the .bash_profile exports the path needed by the MySQLdb module to load the ' libmysqlclient.18.dylib').

If you are having this issue with a Python IDE like PyCharm add the DYLD_LIBRARY_PATH variable to the launching module configuration.

I hope this helps :)

Also,

To fully understand this problem, read the following section:

http://mysql-python.sourceforge.net/FAQ.html#importerror

This explains this error in detail

MySQLdb error when running python server on MacOSX10.6

It looks like you have everything installed right, but it can't find libmysqlclient. Have you tried the following?

> sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
> sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

Installing MySQLdb on Mac OS X

Ack. I feel your pain. I spent a really long time also trying to get MySQL working with Python 2.6 on Snow Leopard using a Macbook Air and had lots of architecture problems. What ended up solving it for me, was making sure both my Python and MySQL installations were using a 32 bit architecture like my Snow Leopard was.

I wrote about my solution here, so maybe that'll help:

http://www.markliu.me/2010/jun/09/mysql-and-python-on-32-bit-snow-leopard/

Good luck...



Related Topics



Leave a reply



Submit