error issue on connecting mysql for django project - python

I am new to Django.i am learning Django for the past few weeks,i have some trouble on changing the databse to sqllite to mysql.
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': ' ',
'HOST': 'localhost',
'PORT': '3306',
}}
When i try to run the server using py manage.py runserver
It show me the error like
django.db.utils.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
SPECIFICATION
python-3.8
django-3
MySQL(xampp)
OS-windows

This is most likely an issue with your USER, PASSWORD or HOST fields. You are probably not providing correct values. Try executing mysql -u root -p -h localhost and enter the password when asked for it. You should get the same error.
My guess is your password is just empty. So delete the space character for password and it should work.
If that is the case, you should really set a password. Refer to this - How to reset or change the MySQL root password?

Related

postgresql auth django (password authentication failed for user "BlogAdmin2")

I followed the tutorial posted by Digital Ocean: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04#create-and-configure-a-new-django-project (How to set up django with postgres nginx and gunicorn on ubuntu)
I created a git hub private repo with the website and when I created the postgresql database with the folowing commands:
CREATE DATABASE PiaBlog;
CREATE USER BlogAdmin WITH PASSWORD 'Andrei1234';
ALTER ROLE BlogAdmin SET client_encoding TO 'utf8';
ALTER ROLE BlogAdmin SET default_transaction_isolation TO 'read committed';
ALTER ROLE BlogAdmin SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE PiaBlog TO BlogAdmin;
\q
and here is my django database config:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'PiaBlog',
'USER': 'BlogAdmin',
'PASSWORD': 'Andrei1234,',
'HOST': 'localhost',
'PORT': '',
}
}
When I run python manage.py migrate I get :
django.db.utils.OperationalError: FATAL: password authentication failed for user "BlogAdmin2"
FATAL: password authentication failed for user "BlogAdmin2"
Does somebody know why do I get this problem I spent an hour searching for a typing error and I never found it.
Then I created a new user and I had this error:
django.db.utils.OperationalError: FATAL: password authentication failed for user "blog_admin"
FATAL: password authentication failed for user "blog_admin"
This is what I did:
CREATE DATABASE blogdb;
CREATE USER blog_admin WITH PASSWORD 'testing1234';
ALTER ROLE blog_admin SET client_encoding TO 'utf8';
ALTER ROLE blog_admin SET default_transaction_isolation TO 'read committed';
ALTER ROLE blog_admin SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE blogDB TO blog_admin;
\q
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'blogdb',
'USER': 'blog_admin',
'PASSWORD': 'testing123',
'HOST': 'localhost',
'PORT': '',
}
}
Is this comma supposed to be in your password?
'PASSWORD': 'Andrei1234,',

Unable to connect to the localhost/phpmyadmin database through my django project

I am trying to connect to the localhost/phpmyadmin through xampp server for my django project. When i type
python manage.py runserver
on my command prompt, i'm getting error.
django.db.utils.OperationalError: (1044, "Access denied for user ''#'localhost' to database 'djangoproject'")
There is no password set on my phpmyadmin, it is just by default user 'root' and password [null]
I have written the below code in my manage.py file to connect to the database page.
DATABASES = {
'default' : {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangoproject',
'USER': ' root ',
'PASSWORD': '',
'HOST': 'localhost',
'PORT':''
}
}
There is also this in my phpmyadmin page on the top
A user account allowing any user from localhost to connect is present. This will prevent other users from connecting if the host part of their account allows a connection from any (%) host.
I do not understand what is wrong. I am a beginner in django framework.
Your user does not have an access to the database. Use the commands below to set up your database
DROP DATABASE IF EXISTS `mydb`;
CREATE DATABASE `mydb`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE 'mysql';
GRANT ALL PRIVILEGES ON mydb.* TO 'mydb_user'#'localhost' IDENTIFIED BY 'your_password'
WITH GRANT OPTION;
FLUSH PRIVILEGES;

django - use environment variables in debian not working

It might be simple thing but I just can't seem to get it running.
in my Django project's settings, I have these db settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PWD'),
'HOST': 'xxxx.yyyyy.eu-central-1.rds.amazonaws.com',
'PORT': '',
}
}
and in /etc/environment I have set the variables and also in .profile.
but this is just not working and I am getting:
(1045, "Access denied for user 'www-data'#'132.31.48.116' (using password: NO)")
If I remove and hardcode the credentials, it works. so obviously my env variables are located in wrong place. can someone please give some look at it?
I am using Debian 4.9.51-1 (2017-09-28) x86_64 in ec2 server

Django:permission denied for relation django_migrations, PostgreSql - django user?

I ve been struggling about figuring out relation between postgre users and linux users and the permissions between them.First question I want to ask is that ,let s assume I have tom linux user and tom postgre user which I created with psql command.What is the relation between those two? The reason I am asking is realated with following question:
I am using vagrant and ubuntu in it.There is 2 userss , first one is Linux "vagrant" user , second one is PostgreSql "vagrant" user which I created through following commands:
CREATE USER vagrant WITH PASSWORD '123.123';
GRANT ALL PRIVILEGES ON DATABASE cashier to vagrant;
which should means and also is that vagrant have all permissions on cashier database.
I should also mention as an aside note that I am using provisioning and vagrant uses root user when executing provisions , so root should also have all permissions on the cashier databse because I am importing dump into cashier database on provisioning.
so you can easily assume root have granted permissions through :
CREATE USER root WITH PASSWORD 'vagrant';
GRANT ALL PRIVILEGES ON DATABASE cashier to root;
Here is the problem: When I try to runserver in django python manage.py runserver
if I go with the following settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cashier',
'USER': 'vagrant',
'PASSWORD': '123.123',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
I get authentication error, and if I go with the following settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cashier',
'USER': 'vagrant',
'PASSWORD': 'vagrant',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Then i have Django:permission denied for relation django_migrations, problem.Simply my question is What is going on?What is the relation between users?Which user should i use ? Why django cant migrate with both user credentials?
Recall :
Linux user vagrant pass vagrant
Postgre user vagrant pass 123.123

1049, "Unknown database 'database' " django mysql can't connect

Exception Type: OperationalError at /
Exception Value: (1049, "Unknown database 'database'")
At the moment i tried this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '****', # Not used with sqlite3.
'HOST': '/var/lib/mysql/database/', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '80', # Set to empty string for default. Not used with sqlite3.
}
}
If i don't specify a host i get this error:
OperationalError at /
(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/database' (13)")
Can it be something with permissions?
thanks in advance :)
First, create the database on mysql.
Second, edit your default conection like this.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MY_DATABASE_NAME',
'USER': 'root',
'PASSWORD': 'MY_PASSWORD',
}
}
finally run your syncdb.
./manage.py syncdb
PORT is not the web server port, but the database port, which is 3306 for MySQL, and HOST is the database sever's IP or name. You probably want 127.0.0.1.
You should create the database beforehand with create database mydatabase charset utf8; from the mysql prompt.
Login to mysql on Terminal and create a DB
mysql -u root -p
CREATE DATABASE dbname;
USE dbname;
Then on setting.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'root', #Mysql username
'PASSWORD': 'password', #mysql password
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
I dont think you can create a database by the name database. So please check the following first...
1) Have installed mysql-server and created the database with proper grant all permissions? If you have done so then check next steps
2)I dont know about Amazon, but this error was common in older versions of Linux distros from Redhat and Fedora. An option is to do this
by setting the SELINUX line in /etc/sysconfig/selinux to Disabled:
SELINUX=Disabled

Categories