Django Operational Error 1405 Upon syncdb - python

I'm trying to move over from SQLite3 to MySQL, after much difficulty I finally got MySQL-python working however when I try to run ./manage.py syncdb I get an error
OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
Right now I'm running MySQL through MAMP. I tried creating a new user instead of using root but I get the same error. Any help/advice would be greatly appreciated, thanks.
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db17', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '8889', # Set to empty string for default.
}
}
Those are the settings given by MAMP.

In addition to creating a new user, did you also grant that user permissions on the tables in your project?
GRANT ALL PRIVILEGES ON db17.* TO 'root'#'localhost';

Related

Cannot connect MySQL database from django

I am trying to connect my MySql server from Django application , but whenever I am running the command python manage.py runserver it showing the error:
File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'user'#'localhost' (using password: NO)")
But my settings.py connection string is like :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_personnel', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'personnel', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
But I can easily connect the database from mysql shell :
mysql -u root -ppersonnel django_personnel
Please guide me. Thanks in advance.
It seems that your current user is not having permissions to connect to DB.
Re installing mysql will help
and also
'HOST': '',
'PORT': '',
Leave these two fields empty

Is there a way to encrypt the MySQL password stored in Django?

I just started on a Django project and in the settings.py file of the project, the database section looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'blogengine', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'blogadmin',
'PASSWORD': 'blog#123',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
Is there any way in which I don't have to enter the password as plaintext but maybe enter it in some encrypted form?
Another thing you could do is not to store your password/token in your settings.py, it is a bad practice for security, instead of that, you should create an environment variable in the user that runs your app let's say:
export MYSQL_PASSWORD=1234
And read it from your django app as follows
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'blogengine', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'blogadmin',
'PASSWORD': os.getenv('MYSQL_PASSWORD'),
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
You should do this for all your "sensible data" like EMAIL_HOST_PASSWORD, AWS tokens and secrets and that kind of stuff, this way you separate the configuration from the environment and you don't have to change those parameters in your testing server or local environment, you just have to ensure that your environment variables are the same but points to the correct location according to your environment.
There is no point in trying to protect that password.
Any token in that file that can be used to access the database can be used by anyone else to access the database. That's how shared secret security works. Replace the password by a randomly generated token, and you still have to communicate that token to settings.py, for example.
Your better bet is to restrict what computers can connect to your MySQL database using that username and password, adding an additional layer of security. Oh, and making sure no one can access settings.py by securing your webserver and source control systems properly.

Django testing MySQL permission

I am going through the Django tutorial and I've run into some trouble because I'm using MySQL as my database.
When I run
python manage.py test polls
I get
Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'USER123'#'%' to database 'test_USER123'")
Type 'yes' if you would like to try deleting the test database 'test_USER123', or 'no' to cancel:
This is my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'USER123', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'USER123',
'PASSWORD': 'PASSWORD',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
I was wondering if anyone would explain to me what's wrong and what's going on. Thank you!

Django OperationalError at / (1045, "Access denied for user 'root'#'localhost' (using password: YES)")

Well, it refuses to work with root, but my settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'stroiset74', # Or path to database file if using sqlite3.
'USER': 'stroiset74', # Not used with sqlite3.
'PASSWORD': '*****', # 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.
}
}
Interestingly, manage.py validate and manage.py syncdb still run without errors.
I faced the same problem, the issue was i had a local_settings.py with database configuration. changed the DB password.
I solved the problem on MY windows10 by searching the username globally in the project, and I found that both settings.py and views.py contains the username and password.
I only changed the configuration in settings.py, after changing the password in the views.py, the django worked.

python manage.py syncdb error

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?
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.
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.
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.

Categories

Resources