OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

9,537 views
Skip to first unread message

ANKIT BAGARIA

unread,
Jan 6, 2013, 5:42:21 AM1/6/13
to django...@googlegroups.com

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'django',                      # Or path to database file if using sqlite3.
        'USER': 'name',                      # Not used with sqlite3.
        'PASSWORD': 'pass',                  # Not used with sqlite3.
        'HOST': '/var/run/mysql',           # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}


this is my setting in settings.py file. I have Mysql server running .Now I run python manage.py shell to start python interpreter. Then in the interreter I type the following command to check the connection to database:
>>>from django.db import connection
>>>cursor = connection.cursor()

and it gives me the following error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 306,
 in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 38
7, in _cursor
    self.connection = Database.connect(**kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __in
it__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

 I am using windows 7.
please help me fix it. it is driving me crazy. I have googled it, gone through different forums but am not able to fix it. Please help me.
thankyou.
Message has been deleted

Diederik van der Boor

unread,
Jan 8, 2013, 4:04:38 AM1/8/13
to django...@googlegroups.com
The hostname "localhost" has a special meaning for MySQL, at least on Linux it has.
When you ask the mysql driver to connect to localhost, it will use the socket.
To avoid this issue, better use 127.0.0.1.

Also check whether the MySQL server is running on that port (netstat -a ?),
whether the service is up (see services panel),
and whether your Windows Firewall allows connections to it.

Greetings,
Diederik

Op 6 jan. 2013, om 18:28 heeft Александр Соколов <sashkac...@gmail.com> het volgende geschreven:

Hi! Recently I had the same problem and this site has helped me: http://stackoverflow.com/questions/11121819/mysqldb-in-python-cant-connect-to-mysql-server-on-localhost
I've set the next properties:
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'yourdbname',                      
    'USER': 'root',                      
    'PASSWORD': 'your password',         
    'HOST': '127.0.0.1',                 
    'PORT': '3306', 

Hope, it will help you.

P.S. By the way in the beginning, I've installed the latest version of MySQLdb. But this is unlikely has been causing of my problem.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/dFs9n5ZCuDEJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Javier Guerra Giraldez

unread,
Jan 8, 2013, 9:53:41 AM1/8/13
to django...@googlegroups.com
On Tue, Jan 8, 2013 at 4:04 AM, Diederik van der Boor <vdb...@gmail.com> wrote:
> When you ask the mysql driver to connect to localhost, it will use the
> socket.
> To avoid this issue, better use 127.0.0.1.

which issue? what's wrong with using socket?

--
Javier
Reply all
Reply to author
Forward
0 new messages