1

所以,我在 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)")

有人知道这里发生了什么吗?我尝试创建一个新项目并按照相同的说明进行操作,但结果相同。

4

1 回答 1

0

精确到@sdolan 的回答:确实,您必须使用正确的 OpenShift MySQL 参数。一种简单的方法是使用 OpenShift 环境变量,如下所示(这是我的应用程序的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':     '',                                     
        }
    }
于 2013-11-17T15:59:17.560 回答