_mysql_exceptions.OperationalError 1044使用Django的用户访问被拒绝

时间:2012-02-22 21:09:47

标签: python mysql django

我试图使用Python Django从服务器A访问服务器“apple”的mysql数据库。相关陈述是

import settings
os.environ['DJANGO_SETTINGS_MODULE'] = runconfig.django_settings_module
from django.db import connection

class Writer(object):
  def __init__(self):

  def submission(self,pars):
    d = connection.cursor()

并运行此操作会给出如下错误:

_mysql_exceptions.OperationalError: (1044, "Access denied for user 'username'@'ice.cse.connell.edu' to database 'mydatabase'")

runco​​nfig.py的内容是

# Django setting module 
django_settings_module = 'settings'

奇怪的是,我能够远程登录服务器“ice”到服务器“apple”的mysql:

$ mysql -u username -ppassword -h apple

我一直在从SO中挖掘这个问题,但仍无法找到答案。这个问题让我疯狂!

以下是我的settings.py的前几行。因为它太长了,我没有发布所有的内容。但如果有人需要更多,我可以粘贴所有内容。

# Django settings for mydatabase project.

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASE_ENGINE = 'mysql'          # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'mydatabase'  # Or path to database file if using sqlite3.
DATABASE_USER = 'username'             # Not used with sqlite3.
DATABASE_PASSWORD = 'password'         # Not used with sqlite3.
DATABASE_HOST = 'apple'            # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

----------------------------------------------- ------------------------------------------

APPENDED POST

根据Sid的建议修改settings.py后:

#DATABASE_ENGINE = 'mysql'           
#DATABASE_NAME = 'mydatabase'   
#DATABASE_USER = 'username'             
#DATABASE_PASSWORD = 'password'         
#DATABASE_HOST = 'apple'  
#DATABASE_PORT = ''     

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'mydatabase',
    'USER': 'username',
    'PASSWORD': 'password',
    'HOST': 'apple',
    'PORT': '',
  }
}

命令行输入和错误消息是

>>> import runconfig
>>> 
>>> import os
>>> import sys
>>> import settings
>>> #from django.conf import settings
... 
>>> os.environ['DJANGO_SETTINGS_MODULE'] = runconfig.django_settings_module
>>> 
>>> from django.db import connection
>>> c = connection.cursor()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/site-packages/django/db/backends/dummy/base.py", line 15, in complain
    raise ImproperlyConfigured, "You haven't set the DATABASE_ENGINE setting yet."
django.core.exceptions.ImproperlyConfigured: You haven't set the DATABASE_ENGINE setting yet.

1 个答案:

答案 0 :(得分:0)

检查您的数据库名称,它可能区分大小写。

http://groups.google.com/group/django-users/browse_thread/thread/8717661c908b836e

尝试使用以下格式:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'mydatabase', 
        'USER': 'username',     
        'PASSWORD': 'password', 
        'HOST': 'apple',
        'PORT': '',     
    }
 }

此外,您使用的用户名是否具有此数据库的权限?