1

I am attempting to get my first Django project working, on Windows, but I get an error message that reads:

File "c:\users\[username]\appdata\local\temp\easy_install-pazcre\MySQL_python-1.2.3-py2.7-    win32.egg.tmp\MySQLdb\connections.py", line 187, in __init__     mysql_exceptions.OperationalError: (1045, "Access denied for user 'django_user'@'localhost'     (using password: YES)")

I created my database from the command line as:

-- create the database
CREATE DATABASE GlobalXdb CHARACTER SET utf8;

-- create user
CREATE USER 'django_user'@'localhost' IDENTIFIED BY 'thepassword';

-- give user permissions to db 
GRANT ALL ON django.* TO 'django_user'@'localhost'

My settings.py file contains:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'GlobalXdb',                      # Or path to database file if using sqlite3.
    'USER': 'django_user',                      # Not used with sqlite3.
    'PASSWORD': 'thepassword',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
}

}

Can anyone shed some light on what I have done incorrectly?

3 Answers 3

5

Your database is named GlobalXdb and yet in this line...

#give user permissions to db 
GRANT ALL ON django.* TO 'django_user'@'localhost'

you grant permissions to django_user on database named django.

Give permissions to the correct database GlobalXdb should solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Django allows you to define multiple database access permissions for a database. You can then specify the database access you want to use when running syncd db. See me answer below....
2

The error message says Access denied for user 'django_user'@'localhost' so my guess would be that the user 'django_user' doesn't exist or you typed the password wrong.

Another less likely suggestion would be to check the rights granted for 'django_user'. It's possible that this user doesn't have permission to create tables.

Comments

0

This is solved by defining two DATABASE profiles in your settings.py.

default - Used by django at runtime with limited permissions for that extra security.

sync - Used by syncdb, as user root, with extra create privileges.

DATABASES = {
  'default': {
    'ENGINE':'django.db.backends.mysql',
    'NAME':'database_name',
    'USER':'runtime',
    'PASSWORD':'runtime_password',
    'HOST':'',
    'PORT':'',
  },
  'sync': {
    'ENGINE':'django.db.backends.mysql',
    'NAME':'database_name',
    'USER':'root',
    'PASSWORD':'',
    'HOST':'',
    'PORT':'',
  },
}

You will also have to grant limited permissions for the default runtime access in MySQL:

GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'runtime'@'localhost' IDENTIFIED BY 'runtime_password';

Finally when you run syncdb specify your sync access profile:

python manage.py syncdb --database=sync

That should give you the security you want at runtime and the access you want on the command line.

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.