Django testing MySQL permission - python

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!

Related

Django Database Migration

Hi have a django project a full project now I want to migrate to mysql from the default Sqlite3 which is the default database. I am on a Mac OS and I don't know how to achieve this process. Any one with a complete guide on how to make the switch would be appreciated.
Go to your project's settings file and edit DATABASES with proper database connection
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}
Now open mysql and create database as you give in DATABASE settings.
Go to your project -
./manage makemigrations
./manage migrate
This will create all the tables in the specified database name.

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 Operational Error 1405 Upon syncdb

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';

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.

Categories

Resources