Python - Django Manage.py Syncdb失败?

时间:2013-10-27 02:44:47

标签: python mysql django

所以,我在OpenShift上使用django快速入门,安装了Mysql 5.1和PhpMyAdmin 4.0。我已经设置了一个数据库并且已经按照教程了解了这个错误。当我执行“python manage.py syncdb”时,会出现以下内容:

python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/core/management/__init__.py", line 459, in execute_manager
    utility.execute()
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/core/management/base.py", line 231, in execute
    self.validate()
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/core/management/validation.py", line 103, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/backends/mysql/base.py", line 411, in get_server_version
    self.cursor()
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/backends/__init__.py", line 308, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "/var/lib/openshift/526c6afd50044654c4000145/python/virtenv/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/backends/mysql/base.py", line 387, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib64/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 '/var/lib/mysql/mysql.sock' (2)")

任何人都知道这里发生了什么?我尝试创建一个新项目并遵循相同的说明,但结果是一样的。

1 个答案:

答案 0 :(得分:0)

准确地说@ sdolan的答案:确实你必须使用正确的OpenShift MySQL参数。一个简单的方法是使用OpenShift env变量,这样(这是我的应用程序的settings模块):

# a setting to determine whether we are running on OpenShift
ON_OPENSHIFT = False
if os.environ.has_key('OPENSHIFT_REPO_DIR'):
    ON_OPENSHIFT = True

if ON_OPENSHIFT:
    # os.environ['OPENSHIFT_MYSQL_DB_*'] variables can be used with databases created
    # with rhc cartridge add (see /README in this git repo)
    DATABASES = {
        'default': {
            'ENGINE':   'django.db.backends.mysql',  
            'NAME':     'myapp',               
            'USER':     os.environ['OPENSHIFT_MYSQL_DB_USERNAME'],     
            'PASSWORD': os.environ['OPENSHIFT_MYSQL_DB_PASSWORD'], 
            'HOST':     os.environ['OPENSHIFT_MYSQL_DB_HOST'],         
            'PORT':     os.environ['OPENSHIFT_MYSQL_DB_PORT'],         
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE':   'django.db.backends.sqlite3',           
            'NAME':     os.path.join(PROJECT_DIR, 'sqlite3.db'),
            'USER':     '',                                     
            'PASSWORD': '',                                     
            'HOST':     '',                                     
            'PORT':     '',                                     
        }
    }