1

I played around with Django, python and MySQL; and have installed and made them work when do import django, import MySQLdb through python command line.

But when I try to execute python manage.py syncdb in the root directory of the project, I got the following error msg:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 231, in execute
    self.validate()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 415, in get_server_version
    self.cursor().close()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-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 '/opt/local/var/run/mysql5/mysqld.sock' (13)")

What would be the possible problem? Seems like connection error stuff? I am newbie here, thanks for any help :)

I attached my settings.py file:

  # Django settings for mysite project.
   DEBUG = True
   TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', '[email protected]'),
)

MANAGERS = ADMINS

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': '',                      
        'USER': 'localhost',                 
        'PASSWORD': '',                 
        'HOST': '',                     
        'PORT': '',                     
    }
}

  TIME_ZONE = 'America/Chicago'

  LANGUAGE_CODE = 'en-us'

  SITE_ID = 1

  USE_I18N = True

  USE_L10N = True

  USE_TZ = True

  MEDIA_ROOT = ''

  MEDIA_URL = ''

  STATIC_ROOT = ''

  STATIC_URL = '/static/'

  STATICFILES_DIRS = (
  )

  STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  )


SECRET_KEY = 'wke73b4pm&amp;s-g(ra*to+ykxe*3@0h=nrnt(v&amp;3kf)fx=co*72@'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'

TEMPLATE_DIRS = (
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

I could successfully log into the mysql database using the command line: mysql -u localhost

After making the HOST = "127.0.0.1", I got an updated error msg:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 60, in handle_noargs
    tables = connection.introspection.table_names()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/__init__.py", line 896, in table_names
    return self.get_table_list(cursor)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/introspection.py", line 33, in get_table_list
    cursor.execute("SHOW TABLES")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 114, in execute
    return self.cursor.execute(query, args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.DatabaseError: (1046, 'No database selected')
6
  • 1
    it can't connect to your database. please check the last line Commented Oct 2, 2012 at 4:03
  • you are trying to connect to a mysql db that you are referencing as "localhost" when this is not the mysql server for you Commented Oct 2, 2012 at 4:04
  • Update the question and add your settings.py file Commented Oct 2, 2012 at 4:07
  • do this - in the shell, do mysql -u <username> -p -h<host> <appname>. See if you can get in. If not, you have problem in the credentials to access the database server or the database instance Commented Oct 2, 2012 at 11:06
  • Hi Karthikr, I could get in using mysql -u localhost Commented Oct 2, 2012 at 14:22

4 Answers 4

1

Just add database name and port number ..

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'Database_name',                 
        'USER': 'root',                    
        'PASSWORD': 'root',                
        'HOST': '',                     
        'PORT': '3306',                  
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing HOST in the database settings. Either use localhost or 127.0.0.1.

Apparently when host is not specified, mysqldb defaults to unix domain sockets instead of using regular hostname/port.

3 Comments

Hi Plaes, the empty '' for HOST indicates it is a localhost. I added that even for the try purpose, but still not working. Thanks anyway.
@leoooo When the error message says that: Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (13)"), there must be something wrong with the HOST value.
I replace the empty string of HOST to "127.0.0.1" and got the above error I edited in my original post...
0

You need to create a schema (let's name it my_db_schema) in MySQL first and then specify it's name under NAME.

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'my_db_schema',                      
        'USER': 'localhost',                 
        'PASSWORD': '',                 
        'HOST': '',                     
        'PORT': '',                     
    }
}

Comments

0

I was facing exactly same problem. DATABASES in settings.py need to set value for both HOSTNAME and PORT.

In my case, mysql uses host "localhost:3306". When I set PORT(in settings.py) value to 3306 it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.