2012-10-02 83 views
1

我玩過Django,python和MySQL;並且在導入django時安裝並使它們工作,通過python命令行導入MySQLdb。python manage.py syncdb無法正常工作

但是當我嘗試在項目的根目錄執行Python manage.py執行syncdb,我得到了以下錯誤信息:

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)") 

什麼是可能出現的問題?似乎連接錯誤的東西?我在這裏是新手,感謝您的幫助:)

我重視我的settings.py文件:

# 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*[email protected]=nrnt(v&amp;3kf)fx=co*[email protected]' 

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, 
     }, 
    } 
} 

我可以成功登錄到MySQL數據庫使用命令行:mysql的 本地主機-u

使得主機=「127.0.0.1」後,我得到了一個更新的錯誤消息:

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') 
+1

它不能連接到數據庫。請檢查最後一行 – HungryCoder

+0

你試圖連接到一個MySQL數據庫,當你不是這個mysql服務器的時候,你正在引用「localhost」 –

+0

更新這個問題並添加你的'settings.py'文件 –

回答

0

您在數據庫中缺少HOST設置。請使用localhost127.0.0.1

顯然,當未指定主機時,mysqldb默認使用unix域套接字而不使用常規主機名/端口。

+0

Hi Plaes,HOST的空白表示它是本地主機。我補充說,即使是爲了嘗試的目的,但仍然無法正常工作。不管怎麼說,還是要謝謝你。 – leoooo

+0

@leoooo當錯誤信息顯示:'''無法通過socket'/opt/local/var/run/mysql5/mysqld.sock'(13)「)連接到本地MySQL服務器時, 「HOST」值有問題 – plaes

+0

我將HOST的空字符串替換爲「127.0.0.1」,並得到了我在原始文章中編輯的上述錯誤... – leoooo

0

您需要先在MySQL中創建一個模式(讓我們將其命名爲my_db_schema),然後在NAME下指定它的名稱。

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

我正面臨完全相同的問題。 settings.py中的DATABASES需要爲HOSTNAME和PORT設置值。

在我的情況下,mysql使用主機「localhost:3306」。當我將PORT(在settings.py中)的值設置爲3306時,它可以工作。

1

只需添加數據庫名和端口號..

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