Yesterday, I posted an article about the way to installing and configuring mod wsgi. After application hooked up successfully, I faced new problem. Because I use MySQL in XAMPP for Linux package, the MySQLdb for python didn’t find mysql sock on the default path. So, you need to manually specify the unix socket.
If you run the connection manually, probably the example is just like this :
import MySQLdb db = MySQLdb.connect( host = 'localhost', user = 'root', passwd = '', db = 'djangodb')
Then I got error like this :
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 188, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
So that means I need to specify unix socket. The syntax will be like this :
db = MySQLdb.connect( host = 'localhost', user = 'root', passwd = '', db = 'djangodb', unix_socket = '/opt/lampp/var/mysql/mysql.sock')
How I found mysql sock file path ? Just type this at terminal. And I got the path
ps -aux | grep mysqld nobody 6959 0.0 1.3 187900 27788 pts/4 Sl 01:41 0:14 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/work085.err --pid-file=/opt/lampp/var/mysql/work085.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306 root 11925 0.0 0.0 4448 796 pts/4 S+ 16:04 0:00 grep --color=auto mysqld
So, I need to change django settings too. I just put socket path (/opt/lampp/var/mysql/mysql.sock) as database host. Seems ridiculous but it works properly.
Cheers,
Source :
http://passingcuriosity.com/2009/specifying-a-unix-socket-using-mysql-with-django/