1

I am a newbie to Ubuntu Os and Xampp related stuff but has a good knowledge of python and django.I recently shifted from windows where i used wamp to run mysql.I installed Xampp on my ubuntu desktop instead of separately installing Mysql.The problem is that i cannot make python to connect to Mysql server running on localhost. I have MYSQLdb connector module installed with my python 2.7.4. Xampp starts fine and i can run SQL by typing mysql -u root in the terminal.So as,the django manage.py utility shows the database client shell while running the following code:

black_perl@ank:~/Desktop/testproject$ python manage.py dbshell
Welcome to the MySQL monitor.  

mysql>

But when i do :

>>> import MySQLdb as m
>>> a=m.connect(user="root",passwd="",db="test_project")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, 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)")

I cannot find any directory /var/run/mysqld. I have my mysql at /opt/lampp/bin/mysql.I tried the other solutions as mentioned on the other related questions here on the stackoverflow but no help. Correct me if i'm wrong somewhere. Regards.

darxtrix
  • 2,032
  • 2
  • 23
  • 30
  • http://stackoverflow.com/questions/11990708/error-cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysq – Grijesh Chauhan Jan 26 '14 at 07:51
  • https://www.google.co.in/search?q=Can%27t+connect+to+local+MySQL+server+through+socket+%27/var/run/mysqld/mysqld.sock%27&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-beta&channel=sb&gws_rd=cr&ei=XL7kUs2XI4uFrgeKo4GgDQ – Grijesh Chauhan Jan 26 '14 at 07:51

1 Answers1

0

Maybe you missed host, port argument.

Arguments to MySQLdb.connect should match DATABASES in settings.py.

For example, assuming following is in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_project',
        'USER': 'root',
        'PASSWORD': 'secret',
        'HOST': '192.168.0.1',
        'PORT': 1234,
    }
}

connect(..) should read as:

import MySQLdb
connection = MySQLdb.connect(user='root', passwd='secret', db='test_project',
                             host='192.168.0.1', port=1234)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I know that the connect function takes two keyword arguments host and port but the real problem was that i had set the host to `localhost` which works on windows but not working here,don't know why.Now i pointed it to `127.0.0.1` , it is working nicely.But, thanks for pointing out once again. – darxtrix Jan 26 '14 at 08:28