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
Related
It's the first time I'm deploying a site using MySQL instead of SQLite3, so I'm quite new at this, after reading some documentation I found out that I have to change some files like this :
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_site_db',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '?', #currently using pythonanywhere (not on localhost)
'PORT': '?',
}
}
manage.py
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
(PyMySQL==0.7.10, Django 1.9, Python 3.5)
When I run the Bash Console using python manage.py migrate I get this error message :
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
I thought it has something to do with the HOST and the PORT from the Database configurations, I don't know what I should add in these, does it have to do with MySQL, PythonAnywhere or even my own domain ? How can I resolve this problem and set up MySQL correctly with Django 3.5 ?
Check this guide. I used it to deploy my webapp.
You can use in this way
Username : johndoe
Database name : socialdatabase
Host : xyz.mysql.pythonanywhere-services.com
User : johndoe
Name : johndoe$socialdatabase
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_site_db', # Username$database name
'USER': 'username', #Username:
'PASSWORD': 'password',
'HOST': '?', # Database host address
}
}
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.
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';
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!
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.