Forums

Error 2002 Mysql

Hi I do this tutorial

http://django.pl/documentation/tutorial01/

and I am in this place now where I write on dashboard

python manage.py syncdb

and I have this error

pythonanywhere _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

why ?

You need to set up your database and use the settings that are provided on the Databases tab on PythonAnywhere.

but I set file settings.py like in tutorial

https://www.pythonanywhere.com/wiki/DjangoTutorial#Creating_an_app_inside_the_Django_project

but when I change only database from sqlite3 to mysql

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

and It generate error like I said...

Exactly. For MySQL, you must specify NAME, USER, PASSWORD, HOST. Those details are on the Databases tab.

But what is Name and Engine ? name "mysql" not work, Host I set empty like on comment Port also empty. What is Name an Engine this database???

NAME is the name of the database you want to use. You can see the list of available databases (and create a new one if you want to) on the databases tab under "Existing databases"

For ENGINE, 'django.db.backends.mysql' is correct.

I go to tab MySQL: daro822$default - kill and I have

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33594521
Server version: 5.1.63-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> Existing databases
-> ;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 34169614
Current database: daro822$default
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server     version for the right syntax to use near 'Existing databases' at line 1
mysql>

In my project mysite in settings.py I have

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

And this Error like on my first post is all the time

edit by admin for formatting

NAME needs to be the name of a database. On PythonAnywhere it will look like username$database_name. They are listed on the databases tab.

Host needs to be mysql.server. That's also shown on the databases tab.

Also, the query you're looking for in the MySQL console is show databases;

I mad it, thank You!!!! :)

I write this like below

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'daro822$mysitedb', # Or path to database file if using sqlite3. 'USER': 'daro822', # Not used with sqlite3. 'PASSWORD': 'xxxxxx', # Not used with sqlite3. 'HOST': 'mysql.server', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } }

python manage.py syncdb

I get the following errors

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 429, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 102, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 338, in get_server_version
    self.cursor()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 250, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 322, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/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)")

This is what Databases looks like in settings.py

DATABASES = {
 93     'default': {
 94         'ENGINE': 'django.db.backends.mysql',
 95         'NAME': 'amritha$costtool',
 96         'USER': 'amritha',
 97         'PASSWORD': 'pass',
 98         'HOST' :'mysql.server',                                                                                                       
 99         'PORT':''
100     }
101 }

Does anybody have any ideas on how to fix this?

[edit by admin- formatting]

Hi Amritha,

Quick check- have you tried reloading your webapp after updating settings.py?

Hi Conrad, am assuming by "reloading your webs app", you mean pressing the Reload button on the Dashboard? I did that. Still getting the same error.

Hi Amritha, I took a look at your error log and it seems that you are missing the python module to connect to mysequel (MySQLdb).

Please see our wiki page here about how to install this for python 3.4

Thanks Conrad.

Are you talking of the connector?

pip3.3 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.1.tar.gz

yes. That is what I am talking about. Did that help?

No, it didn't. Am getting the same sock error in bash. When I checked the error logs for the latest error, this is what I found.

2014-12-04 15:28:33,965 :Traceback (most recent call last):
2014-12-04 15:28:33,965 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2014-12-04 15:28:33,965 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-12-04 15:28:33,965 :  File "/usr/lib/python3.4/logging/__init__.py", line 1296, in exception
2014-12-04 15:28:33,990 :    self.error(msg, *args, **kwargs)
2014-12-04 15:28:33,990 :  File "/usr/lib/python3.4/logging/__init__.py", line 1289, in error
2014-12-04 15:28:33,991 :    self._log(ERROR, msg, args, **kwargs)
2014-12-04 15:28:33,991 :  File "/usr/lib/python3.4/logging/__init__.py", line 1395, in _log
2014-12-04 15:28:33,991 :    self.handle(record)
2014-12-04 15:28:33,991 :  File "/usr/lib/python3.4/logging/__init__.py", line 1404, in handle
2014-12-04 15:28:33,992 :    if (not self.disabled) and self.filter(record):
2014-12-04 15:28:33,992 :  File "/usr/lib/python3.4/logging/__init__.py", line 692, in filter
2014-12-04 15:28:33,992 :    for f in self.filters:
2014-12-04 15:28:33,992 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2014-12-04 15:28:33,993 :    app_iterator = self.app(environ, start_response)
2014-12-04 15:28:33,993 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-12-04 15:28:34,018 :    self.load_middleware()
2014-12-04 15:28:34,018 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
2014-12-04 15:28:34,018 :    mw_class = import_by_path(middleware_path)
2014-12-04 15:28:34,018 :  File "/usr/local/lib/python3.4/dist-packages/django/utils/module_loading.py", line 21, in import_by_path
2014-12-04 15:28:34,033 :    module = import_module(module_path)
2014-12-04 15:28:34,033 :  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
2014-12-04 15:28:34,040 :    return _bootstrap._gcd_import(name[level:], package, level)
2014-12-04 15:28:34,040 :  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
2014-12-04 15:28:34,042 :  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
2014-12-04 15:28:34,042 :  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
2014-12-04 15:28:34,042 :  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
2014-12-04 15:28:34,042 :  File "<frozen importlib._bootstrap>", line 1129, in _exec
2014-12-04 15:28:34,043 :  File "<frozen importlib._bootstrap>", line 1448, in exec_module
2014-12-04 15:28:34,043 :  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
2014-12-04 15:28:34,043 :  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/middleware.py", line 3, in <module>
2014-12-04 15:28:34,045 :    from django.contrib.auth.backends import RemoteUserBackend
2014-12-04 15:28:34,045 :  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/backends.py", line 3, in <module>
2014-12-04 15:28:34,046 :    from django.contrib.auth.models import Permission
2014-12-04 15:28:34,046 :  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/models.py", line 48, in <module>
2014-12-04 15:28:34,046 :    class Permission(models.Model):
2014-12-04 15:28:34,046 :  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 96, in __new__
2014-12-04 15:28:34,062 :    new_class.add_to_class('_meta', Options(meta, **kwargs))
2014-12-04 15:28:34,062 :  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 264, in add_to_class
2014-12-04 15:28:34,063 :    value.contribute_to_class(cls, name)
2014-12-04 15:28:34,063 :  File "/usr/local/lib/python3.4/dist-packages/django/db/models/options.py", line 124, in contribute_to_class
2014-12-04 15:28:34,063 :    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
2014-12-04 15:28:34,063 :  File "/usr/local/lib/python3.4/dist-packages/django/db/__init__.py", line 34, in __getattr__
2014-12-04 15:28:34,072 :    return getattr(connections[DEFAULT_DB_ALIAS], item)
2014-12-04 15:28:34,072 :  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 198, in __getitem__
2014-12-04 15:28:34,073 :    backend = load_backend(db['ENGINE'])
2014-12-04 15:28:34,073 :  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 113, in load_backend
2014-12-04 15:28:34,073 :    return import_module('%s.base' % backend_name)
2014-12-04 15:28:34,073 :  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
2014-12-04 15:28:34,073 :    return _bootstrap._gcd_import(name[level:], package, level)
2014-12-04 15:28:34,073 :  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 17, in <module>
2014-12-04 15:28:34,073 :    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
2014-12-04 15:28:34,074 :django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

[edited by admin: formatting]

Oh. Try installing MySQLdb using

pip3.4 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.1.tar.gz

instead of

pip3.3 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.1.tar.gz

This is because your site is running python 3.4 from when you set it up.

Also when you say you are getting errors in bash, I believe that bash runs python 2.7. You can check this by running

python -V

in the commandline.

Installed pip3.4. My bash is running python2.7. Should I change it to 3.4? Please tell me how to do it.

Here are the latest errors from the log file.

2014-12-04 17:56:32,236 :Traceback (most recent call last):
2014-12-04 17:56:32,236 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2014-12-04 17:56:32,236 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-12-04 17:56:32,236 :  File "/usr/lib/python3.4/logging/__init__.py", line 1296, in exception
2014-12-04 17:56:32,237 :    self.error(msg, *args, **kwargs)
2014-12-04 17:56:32,237 :  File "/usr/lib/python3.4/logging/__init__.py", line 1289, in error
2014-12-04 17:56:32,238 :    self._log(ERROR, msg, args, **kwargs)
2014-12-04 17:56:32,238 :  File "/usr/lib/python3.4/logging/__init__.py", line 1395, in _log
2014-12-04 17:56:32,239 :    self.handle(record)
2014-12-04 17:56:32,239 :  File "/usr/lib/python3.4/logging/__init__.py", line 1404, in handle
2014-12-04 17:56:32,239 :    if (not self.disabled) and self.filter(record):
2014-12-04 17:56:32,239 :  File "/usr/lib/python3.4/logging/__init__.py", line 692, in filter
2014-12-04 17:56:32,240 :    for f in self.filters:
2014-12-04 17:56:32,240 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2014-12-04 17:56:32,240 :    app_iterator = self.app(environ, start_response)
2014-12-04 17:56:32,240 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-12-04 17:56:32,241 :    self.load_middleware()
2014-12-04 17:56:32,241 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
2014-12-04 17:56:32,242 :    mw_class = import_by_path(middleware_path)
2014-12-04 17:56:32,242 :  File "/usr/local/lib/python3.4/dist-packages/django/utils/module_loading.py", line 31, in import_by_path
2014-12-04 17:56:32,242 :    error_prefix, module_path, class_name))
2014-12-04 17:56:32,242 :django.core.exceptions.ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class

[edited by admin: formatting]

To run python 3.4 from bash, you can type

python3.4

intead of just

python

I think the error in your logs might be related to this Stack Overflow post...?

am now running python3.4 manage.py syncdb

I get the error

File "/home/amritha/.virtualenvs/costtool/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module> raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: /home/amritha/.virtualenvs/costtool/lib/python2.7/site-packages /_mysql.so: undefined symbol: _Py_ZeroStruct

You need to be clear about which version of Python you're using. The error message above mentions python2.7 and all the others mention 3.3.

I was asked to install pip3.4 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.1.tar.gz

Instead of that, should I install pip2.7 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.1.tar.gz?

What is the best way to keep all the versions of Python consistent?

Your last error is running in a virtualenv, but the previous ones are not. Make sure that you actually want to be running in that virtualenv and that you've installed all the packages you need into that virtualenv for the right version of Python.

Still stuck. Sorry. (1) Deleted the web app. Created new one with same name with python 2.7. (2) In the bash, I updated the settings.py with

DATABASES = {
 93     'default': {
 94         'ENGINE': 'django.db.backends.mysql',                                                                                         
 95         'NAME': 'amritha$costtool',
 96         'USER': 'amritha',
 97         'PASSWORD': 'pass',
 98         'HOST' :'mysql.server',
 99         'PORT':''
100     }
101 }

(3) ran pip2.7 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.1.tar.gz

(4) tried both python manage.py syncdb and python2.7 manage.py syncdb

still getting the same error

File "/usr/local/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)")

edited by admin for formatting

Delete the PORT line, that's confusing Django into thinking you want to connect to a MySQL instance through a local socket.

Deleted the PORT line.

These are the latest lines in the error log file.

2014-12-05 21:22:41,379 :AssertionError: You must run this like execfile('path/to/activate_this.py', dict(file='path/to/activate_this.py')) 2014-12-05 21:34:20,894 :/usr/lib/python2.7/threading.py:1160: RuntimeWarning: tp_compare didn't return -1 or -2 for exception 2014-12-05 21:34:20,894 : return _active[_get_ident()] 2014-12-05 21:34:20,894 :Traceback (most recent call last): 2014-12-05 21:34:20,894 : File "/bin/user_wsgi_wrapper.py", line 130, in call 2014-12-05 21:34:20,894 : self.error_log_file.logger.exception("Error running WSGI application") 2014-12-05 21:34:20,894 : File "/usr/lib/python2.7/logging/init.py", line 1185, in exception 2014-12-05 21:34:20,895 : self.error(msg, args, kwargs) 2014-12-05 21:34:20,895 : File "/usr/lib/python2.7/logging/init.py", line 1178, in error 2014-12-05 21:34:20,895 : self._log(ERROR, msg, args, *kwargs) 2014-12-05 21:34:20,895 : File "/usr/lib/python2.7/logging/init.py", line 1270, in _log 2014-12-05 21:34:20,895 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra) 2014-12-05 21:34:20,895 : File "/usr/lib/python2.7/logging/init.py", line 1244, in makeRecord 2014-12-05 21:34:20,896 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func) 2014-12-05 21:34:20,896 : File "/usr/lib/python2.7/logging/init.py", line 284, in init 2014-12-05 21:34:20,896 : self.threadName = threading.current_thread().name 2014-12-05 21:34:20,896 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread 2014-12-05 21:34:20,896 : return _active[_get_ident()] 2014-12-05 21:34:20,896 : File "/bin/user_wsgi_wrapper.py", line 122, in call 2014-12-05 21:34:20,896 : app_iterator = self.app(environ, start_response) 2014-12-05 21:34:20,896 : File "/bin/user_wsgi_wrapper.py", line 136, in import_error_application 2014-12-05 21:34:20,896 : raise e 2014-12-05 21:34:20,897 :AssertionError: You must run this like execfile('path/to/activate_this.py', dict(file='path/to/activate_this.py'))

Try changing the way the activate_this call works:

# for python 2
activate_this = '/home/myusername/.virtualenvs/myvirtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango

Thanks Harry. That was a big help.

I am almost there. I have one last question. I am getting the below error. Can you please confirm that models.py, forms.py etc. should be in the /home/amritha/costtool/costtool folder and NOT IN /home/amritha/costtool?

2014-12-06 13:37:07,617 :RuntimeError: Conflicting 'projects' models in application 'costtool': <class 'costtool.models.Projects'> and <class 'costtool.costtool.models.Projects'>.

Now getting

2014-12-06 14:36:16,219 : File "/home/amritha/.virtualenvs/costtool/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 189, in _fetch 2014-12-06 14:36:16,220 : "The translation infrastructure cannot be initialized before the " 2014-12-06 14:36:16,220 :django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

YIPPEE ITS WORKING Made the following changes to my wsgi file

activate_this = '/home/amritha/.virtualenvs/costtool/bin/activate_this.py' execfile(activate_this, dict(file=activate_this))

import os import sys

path = '/home/amritha/costtool' if path not in sys.path: sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'costtool.settings'

from django.core.wsgi import get_wsgi_application application = get_wsgi_application()

Thanks for all your help

Hooray! I'm glad you figured it out. Happy coding :)

Hi,

can anybody help me on this. i am getting the following error.

django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

also please have look at my database settings and let me know if it is correct or not.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'hozefanaseem$msmanagement', 'USER': 'hozefanaseem', 'PASSWORD': 'xxxxxxxxxxxx', 'host' : "mysql.server"
} }

You are using the wrong host. Go to your pythonanywhere database page to find the correct host.

ya that also i tried. but still i am getting the same error

here is the updated database configuration.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'hozefanaseem$msmanagement', 'USER': 'hozefanaseem', 'PASSWORD': 'xxxxxxxxxxxx', 'host': 'hozefanaseem.mysql.pythonanywhere-services.com', } }

You should not be trying to connect to a local server. If you are seeing that error, then it is not connecting to the correct host. Did you reload your webapp after making those changes?

yes i did the same....i reload the webapp while pressing on the button 'reload <app url>' under the web.

read what you typed in to databases['default'] carefully.

i read it and i dont thing there is any issue in configuration. please let me know if i am missing something here.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'hozefanaseem$msmanagement', 'USER': 'hozefanaseem', 'PASSWORD': 'xxxxxxxxxxxx', 'host': 'hozefanaseem.mysql.pythonanywhere-services.com'} }

Got it...it was a very silly mistake. we should write name of the keys into upper case under DATABASES['default'].

Yup. Good job! :D

Make sure you're using HOST as described in Database tab. In my case MYUSERNAME.mysql.pythonanywhere-services.com.